blob: 190121955b36b2d4e67b2097e60ff09d40c7dc74 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
[email protected]b6eb8e332013-09-10 00:51:018#include <limits>
[email protected]ac7c7f52012-11-08 06:26:509
[email protected]ad5d1422012-10-19 13:40:2910#include "base/basictypes.h"
[email protected]34806722013-08-09 23:51:2111#include "base/containers/hash_tables.h"
[email protected]4a23c374c2012-12-08 08:38:5512#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1413#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3414#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4815#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0916#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4017#include "cc/animation/timing_function.h"
[email protected]681ccff2013-03-18 06:13:5218#include "cc/base/math_util.h"
19#include "cc/base/util.h"
[email protected]6e84de22013-03-18 06:54:2720#include "cc/debug/debug_rect_history.h"
21#include "cc/debug/frame_rate_counter.h"
22#include "cc/debug/overdraw_metrics.h"
23#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4324#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2225#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2126#include "cc/input/page_scale_animation.h"
27#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5228#include "cc/layers/append_quads_data.h"
29#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2830#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1732#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2833#include "cc/layers/render_surface_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3034#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4535#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3036#include "cc/output/delegating_renderer.h"
37#include "cc/output/gl_renderer.h"
38#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5639#include "cc/quads/render_pass_draw_quad.h"
40#include "cc/quads/shared_quad_state.h"
41#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1242#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4043#include "cc/resources/memory_history.h"
44#include "cc/resources/picture_layer_tiling.h"
45#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1146#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5747#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3148#include "cc/scheduler/delay_based_time_source.h"
49#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0450#include "cc/trees/damage_tracker.h"
51#include "cc/trees/layer_tree_host.h"
52#include "cc/trees/layer_tree_host_common.h"
53#include "cc/trees/layer_tree_impl.h"
54#include "cc/trees/quad_culler.h"
55#include "cc/trees/single_thread_proxy.h"
56#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0657#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1358#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1459
[email protected]94f206c12012-08-25 00:09:1460namespace {
61
[email protected]c1bb5af2013-03-13 19:06:2762void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
63 if (visible) {
64 TRACE_EVENT_ASYNC_BEGIN1("webkit",
65 "LayerTreeHostImpl::SetVisible",
66 id,
67 "LayerTreeHostImpl",
68 id);
69 return;
70 }
[email protected]94f206c12012-08-25 00:09:1471
[email protected]c1bb5af2013-03-13 19:06:2772 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1473}
74
[email protected]b6eb8e332013-09-10 00:51:0175size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
76 if (context_provider) {
77 // We want to make sure the default transfer buffer size is equal to the
78 // amount of data that can be uploaded by the compositor to avoid stalling
79 // the pipeline.
80 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
81 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
82 // Assuming a two frame deep pipeline between CPU and GPU and we are
83 // drawing 60 frames per second which would require us to draw one
84 // frame in 16 milliseconds.
85 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
86 return std::min(
87 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
88 kMaxTransferBufferUsageBytes);
89 } else {
90 // Software compositing should not use this value in production. Just use a
91 // default value when testing uploads with the software compositor.
92 return std::numeric_limits<size_t>::max();
93 }
94}
95
[email protected]c1bb5af2013-03-13 19:06:2796} // namespace
[email protected]94f206c12012-08-25 00:09:1497
[email protected]9c88e562012-09-14 22:21:3098namespace cc {
[email protected]94f206c12012-08-25 00:09:1499
[email protected]96baf3e2012-10-22 23:09:55100class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27101 public:
102 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
103 LayerTreeHostImpl* layer_tree_host_impl,
104 scoped_refptr<DelayBasedTimeSource> time_source) {
105 return make_scoped_ptr(
106 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
107 time_source));
108 }
109 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26110 time_source_->SetClient(NULL);
111 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27112 }
113
[email protected]6d0e69d2013-03-20 14:53:26114 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27115 // In single threaded mode we attempt to simulate changing the current
116 // thread by maintaining a fake thread id. When we switch from one
117 // thread to another, we construct DebugScopedSetXXXThread objects that
118 // update the thread id. This lets DCHECKS that ensure we're on the
119 // right thread to work correctly in single threaded mode. The problem
120 // here is that the timer tasks are run via the message loop, and when
121 // they run, we've had no chance to construct a DebugScopedSetXXXThread
122 // object. The result is that we report that we're running on the main
123 // thread. In multi-threaded mode, this timer is run on the compositor
124 // thread, so to keep this consistent in single-threaded mode, we'll
125 // construct a DebugScopedSetImplThread object. There is no need to do
126 // this in multi-threaded mode since the real thread id's will be
127 // correct. In fact, setting fake thread id's interferes with the real
128 // thread id's and causes breakage.
129 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
130 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
131 set_impl_thread.reset(
132 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14133 }
134
[email protected]94bf75c2013-06-12 13:20:04135 // TODO(enne): This should probably happen post-animate.
136 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38137 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
138 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04139 }
140
[email protected]fb7425a2013-04-22 16:28:55141 layer_tree_host_impl_->Animate(
142 layer_tree_host_impl_->CurrentFrameTimeTicks(),
143 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18144 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
145 bool start_ready_animations = true;
146 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38147 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27148 }
[email protected]373974232013-01-10 22:20:50149
[email protected]c1bb5af2013-03-13 19:06:27150 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26151 if (active != time_source_->Active())
152 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27153 }
[email protected]94f206c12012-08-25 00:09:14154
[email protected]d9fce6722013-08-30 01:10:01155 bool Active() const { return time_source_->Active(); }
156
[email protected]c1bb5af2013-03-13 19:06:27157 private:
158 LayerTreeHostImplTimeSourceAdapter(
159 LayerTreeHostImpl* layer_tree_host_impl,
160 scoped_refptr<DelayBasedTimeSource> time_source)
161 : layer_tree_host_impl_(layer_tree_host_impl),
162 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26163 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27164 }
[email protected]94f206c12012-08-25 00:09:14165
[email protected]c1bb5af2013-03-13 19:06:27166 LayerTreeHostImpl* layer_tree_host_impl_;
167 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14168
[email protected]c1bb5af2013-03-13 19:06:27169 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14170};
171
[email protected]96baf3e2012-10-22 23:09:55172LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20173 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27174
175LayerTreeHostImpl::FrameData::~FrameData() {}
176
177scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
178 const LayerTreeSettings& settings,
179 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43180 Proxy* proxy,
181 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
182 return make_scoped_ptr(
183 new LayerTreeHostImpl(settings,
184 client,
185 proxy,
186 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10187}
188
[email protected]372bad5f2013-03-21 16:38:43189LayerTreeHostImpl::LayerTreeHostImpl(
190 const LayerTreeSettings& settings,
191 LayerTreeHostImplClient* client,
192 Proxy* proxy,
193 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27194 : client_(client),
195 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37196 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27197 did_lock_scrolling_layer_(false),
198 should_bubble_scrolls_(false),
199 wheel_scrolling_(false),
[email protected]c48536a52013-09-14 00:02:08200 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47201 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27202 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27203 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07204 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34205 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27206 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
207 0,
[email protected]3f2ff112013-08-03 02:41:07208 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING,
209 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27210 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13211 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36212 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29213 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27214 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02215 debug_rect_history_(DebugRectHistory::Create()),
[email protected]ea468c6c2013-09-10 08:25:11216 texture_mailbox_deleter_(new TextureMailboxDeleter),
[email protected]d7626ffd2013-03-29 00:17:42217 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27218 last_sent_memory_visible_bytes_(0),
219 last_sent_memory_visible_and_nearby_bytes_(0),
220 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55221 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32222 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12223 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32224 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28225 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17226 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43227 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19228 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]8bb6cfa2013-07-23 00:11:19229 need_to_update_visible_tiles_before_draw_(false) {
[email protected]c1bb5af2013-03-13 19:06:27230 DCHECK(proxy_->IsImplThread());
231 DidVisibilityChange(this, visible_);
232
[email protected]8e0176d2013-03-21 03:14:52233 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27234
[email protected]8e0176d2013-03-21 03:14:52235 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27236 top_controls_manager_ =
237 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52238 settings.top_controls_height,
239 settings.top_controls_show_threshold,
240 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27241 }
242
[email protected]8e0176d2013-03-21 03:14:52243 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27244
245 // LTHI always has an active tree.
246 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51247 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
248 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10249}
250
[email protected]c1bb5af2013-03-13 19:06:27251LayerTreeHostImpl::~LayerTreeHostImpl() {
252 DCHECK(proxy_->IsImplThread());
253 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51254 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
255 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27256
[email protected]200a9c062013-05-20 04:34:37257 if (input_handler_client_) {
258 input_handler_client_->WillShutdown();
259 input_handler_client_ = NULL;
260 }
261
[email protected]75deb742013-06-24 20:19:18262 // The layer trees must be destroyed before the layer tree host. We've
263 // made a contract with our animation controllers that the registrar
264 // will outlive them, and we must make good.
265 recycle_tree_.reset();
266 pending_tree_.reset();
267 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14268}
269
[email protected]c1bb5af2013-03-13 19:06:27270void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29271
[email protected]c1bb5af2013-03-13 19:06:27272void LayerTreeHostImpl::CommitComplete() {
273 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08274
[email protected]8e0176d2013-03-21 03:14:52275 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25276 // Impl-side painting needs an update immediately post-commit to have the
277 // opportunity to create tilings. Other paths can call UpdateDrawProperties
278 // more lazily when needed prior to drawing.
279 pending_tree()->ApplyScrollDeltasSinceBeginFrame();
[email protected]c1bb5af2013-03-13 19:06:27280 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04281 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26282 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08283 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38284 NotifyReadyToActivate();
285 else
286 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27287 } else {
288 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01289 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
290 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27291 }
[email protected]3ba4cae2013-01-16 03:58:38292
[email protected]c1bb5af2013-03-13 19:06:27293 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14294}
295
[email protected]6133cc232013-07-30 18:47:07296bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27297 // Note: If you are changing this function or any other function that might
298 // affect the result of CanDraw, make sure to call
299 // client_->OnCanDrawStateChanged in the proper places and update the
300 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14301
[email protected]6133cc232013-07-30 18:47:07302 if (!renderer_) {
303 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
304 TRACE_EVENT_SCOPE_THREAD);
305 return false;
306 }
307
308 // Must have an OutputSurface if |renderer_| is not NULL.
309 DCHECK(output_surface_);
310
311 // TODO(boliu): Make draws without root_layer work and move this below
312 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27313 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42314 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
315 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22316 return false;
[email protected]c1bb5af2013-03-13 19:06:27317 }
[email protected]6133cc232013-07-30 18:47:07318
319 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
320 return true;
321
[email protected]54af03522013-09-05 00:43:28322 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42323 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
324 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27325 return false;
326 }
327 if (active_tree_->ViewportSizeInvalid()) {
328 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42329 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
330 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27331 return false;
332 }
[email protected]c1bb5af2013-03-13 19:06:27333 if (active_tree_->ContentsTexturesPurged()) {
334 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42335 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
336 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27337 return false;
338 }
[email protected]127bdc1a2013-09-11 01:44:48339 if (EvictedUIResourcesExist()) {
340 TRACE_EVENT_INSTANT0(
341 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
342 TRACE_EVENT_SCOPE_THREAD);
343 return false;
344 }
[email protected]c1bb5af2013-03-13 19:06:27345 return true;
[email protected]2f1acc262012-11-16 21:42:22346}
347
[email protected]c1bb5af2013-03-13 19:06:27348void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
349 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37350 if (input_handler_client_)
351 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27352 AnimatePageScale(monotonic_time);
353 AnimateLayers(monotonic_time, wall_clock_time);
354 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37355 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14356}
357
[email protected]c1bb5af2013-03-13 19:06:27358void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26359 if (!tile_manager_)
360 return;
[email protected]c48536a52013-09-14 00:02:08361 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26362 return;
[email protected]54af03522013-09-05 00:43:28363 if (!device_viewport_valid_for_tile_management_)
364 return;
365
[email protected]c48536a52013-09-14 00:02:08366 tile_priorities_dirty_ = false;
[email protected]c1bb5af2013-03-13 19:06:27367 tile_manager_->ManageTiles();
368
369 size_t memory_required_bytes;
370 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03371 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27372 size_t memory_used_bytes;
373 tile_manager_->GetMemoryStats(&memory_required_bytes,
374 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03375 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27376 &memory_used_bytes);
377 SendManagedMemoryStats(memory_required_bytes,
378 memory_nice_to_have_bytes,
379 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15380}
381
[email protected]c1bb5af2013-03-13 19:06:27382void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
383 bool anchor_point,
384 float page_scale,
385 base::TimeTicks start_time,
386 base::TimeDelta duration) {
387 if (!RootScrollLayer())
388 return;
389
390 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47391 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27392 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28393 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27394
395 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40396
397 // Easing constants experimentally determined.
398 scoped_ptr<TimingFunction> timing_function =
399 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
400
[email protected]c1bb5af2013-03-13 19:06:27401 page_scale_animation_ =
402 PageScaleAnimation::Create(scroll_total,
403 active_tree_->total_page_scale_factor(),
404 viewport_size,
405 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40406 start_time_seconds,
407 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27408
409 if (anchor_point) {
410 gfx::Vector2dF anchor(target_offset);
411 page_scale_animation_->ZoomWithAnchor(anchor,
412 page_scale,
413 duration.InSecondsF());
414 } else {
415 gfx::Vector2dF scaled_target_offset = target_offset;
416 page_scale_animation_->ZoomTo(scaled_target_offset,
417 page_scale,
418 duration.InSecondsF());
419 }
420
421 client_->SetNeedsRedrawOnImplThread();
422 client_->SetNeedsCommitOnImplThread();
423 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15424}
425
[email protected]c1bb5af2013-03-13 19:06:27426void LayerTreeHostImpl::ScheduleAnimation() {
427 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15428}
429
[email protected]c1bb5af2013-03-13 19:06:27430bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
431 if (!EnsureRenderSurfaceLayerList())
432 return false;
[email protected]f57bbc02012-11-21 07:02:15433
[email protected]c1bb5af2013-03-13 19:06:27434 gfx::PointF device_viewport_point =
435 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15436
[email protected]c1bb5af2013-03-13 19:06:27437 // First find out which layer was hit from the saved list of visible layers
438 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39439 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27440 device_viewport_point,
441 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15442
[email protected]c1bb5af2013-03-13 19:06:27443 // Walk up the hierarchy and look for a layer with a touch event handler
444 // region that the given point hits.
445 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39446 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27447 layer_impl))
448 return true;
449 }
[email protected]f57bbc02012-11-21 07:02:15450
[email protected]c1bb5af2013-03-13 19:06:27451 return false;
452}
453
[email protected]b76329d2013-06-11 04:15:08454void LayerTreeHostImpl::SetLatencyInfoForInputEvent(
455 const ui::LatencyInfo& latency_info) {
456 active_tree()->SetLatencyInfo(latency_info);
457}
458
[email protected]c1bb5af2013-03-13 19:06:27459void LayerTreeHostImpl::TrackDamageForAllSurfaces(
460 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28461 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27462 // For now, we use damage tracking to compute a global scissor. To do this, we
463 // must compute all damage tracking before drawing anything, so that we know
464 // the root damage rect. The root damage rect is then used to scissor each
465 // surface.
466
467 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06468 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27469 --surface_index) {
470 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
471 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
472 DCHECK(render_surface);
473 render_surface->damage_tracker()->UpdateDamageTrackingState(
474 render_surface->layer_list(),
475 render_surface_layer->id(),
476 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
477 render_surface->content_rect(),
478 render_surface_layer->mask_layer(),
479 render_surface_layer->filters(),
480 render_surface_layer->filter().get());
481 }
482}
483
[email protected]34806722013-08-09 23:51:21484scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
485 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
486 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
487 value->SetBoolean("has_no_damage", has_no_damage);
488
489 // Quad data can be quite large, so only dump render passes if we select
490 // cc.debug.quads.
491 bool quads_enabled;
492 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
493 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
494 if (quads_enabled) {
495 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
496 for (size_t i = 0; i < render_passes.size(); ++i)
497 render_pass_list->Append(render_passes[i]->AsValue().release());
498 value->Set("render_passes", render_pass_list.release());
499 }
500 return value.PassAs<base::Value>();
501}
502
[email protected]c1bb5af2013-03-13 19:06:27503void LayerTreeHostImpl::FrameData::AppendRenderPass(
504 scoped_ptr<RenderPass> render_pass) {
505 render_passes_by_id[render_pass->id] = render_pass.get();
506 render_passes.push_back(render_pass.Pass());
507}
508
[email protected]ffbb2212013-06-02 23:47:59509static DrawMode GetDrawMode(OutputSurface* output_surface) {
510 if (output_surface->ForcedDrawToSoftwareDevice()) {
511 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09512 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59513 return DRAW_MODE_HARDWARE;
514 } else {
515 DCHECK(output_surface->software_device());
516 return DRAW_MODE_SOFTWARE;
517 }
518}
519
[email protected]c1bb5af2013-03-13 19:06:27520static void AppendQuadsForLayer(RenderPass* target_render_pass,
521 LayerImpl* layer,
522 const OcclusionTrackerImpl& occlusion_tracker,
523 AppendQuadsData* append_quads_data) {
524 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49525 QuadCuller quad_culler(&target_render_pass->quad_list,
526 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27527 layer,
528 occlusion_tracker,
529 layer->ShowDebugBorders(),
530 for_surface);
531 layer->AppendQuads(&quad_culler, append_quads_data);
532}
533
534static void AppendQuadsForRenderSurfaceLayer(
535 RenderPass* target_render_pass,
536 LayerImpl* layer,
537 const RenderPass* contributing_render_pass,
538 const OcclusionTrackerImpl& occlusion_tracker,
539 AppendQuadsData* append_quads_data) {
540 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49541 QuadCuller quad_culler(&target_render_pass->quad_list,
542 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27543 layer,
544 occlusion_tracker,
545 layer->ShowDebugBorders(),
546 for_surface);
547
548 bool is_replica = false;
549 layer->render_surface()->AppendQuads(&quad_culler,
550 append_quads_data,
551 is_replica,
552 contributing_render_pass->id);
553
554 // Add replica after the surface so that it appears below the surface.
555 if (layer->has_replica()) {
556 is_replica = true;
557 layer->render_surface()->AppendQuads(&quad_culler,
558 append_quads_data,
559 is_replica,
560 contributing_render_pass->id);
561 }
562}
563
564static void AppendQuadsToFillScreen(
[email protected]9f4f6a32013-09-04 21:35:12565 ResourceProvider::ResourceId resource_id,
566 gfx::SizeF resource_scaled_size,
[email protected]c1bb5af2013-03-13 19:06:27567 RenderPass* target_render_pass,
568 LayerImpl* root_layer,
569 SkColor screen_background_color,
570 const OcclusionTrackerImpl& occlusion_tracker) {
571 if (!root_layer || !SkColorGetA(screen_background_color))
572 return;
573
574 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
575 if (fill_region.IsEmpty())
576 return;
577
578 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49579 QuadCuller quad_culler(&target_render_pass->quad_list,
580 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27581 root_layer,
582 occlusion_tracker,
583 root_layer->ShowDebugBorders(),
584 for_surface);
585
586 // Manually create the quad state for the gutter quads, as the root layer
587 // doesn't have any bounds and so can't generate this itself.
588 // TODO(danakj): Make the gutter quads generated by the solid color layer
589 // (make it smarter about generating quads to fill unoccluded areas).
590
591 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
592 float opacity = 1.f;
593 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49594 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27595 shared_quad_state->SetAll(root_layer->draw_transform(),
596 root_target_rect.size(),
597 root_target_rect,
598 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01599 false,
[email protected]f57bbc02012-11-21 07:02:15600 opacity);
601
[email protected]c1bb5af2013-03-13 19:06:27602 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17603
[email protected]c1bb5af2013-03-13 19:06:27604 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
605 bool did_invert = root_layer->screen_space_transform().GetInverse(
606 &transform_to_layer_space);
607 DCHECK(did_invert);
608 for (Region::Iterator fill_rects(fill_region);
609 fill_rects.has_rect();
610 fill_rects.next()) {
611 // The root layer transform is composed of translations and scales only,
612 // no perspective, so mapping is sufficient (as opposed to projecting).
613 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21614 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]9f4f6a32013-09-04 21:35:12615 if (resource_id) {
616 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
617 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
618 tex_quad->SetNew(
619 shared_quad_state,
620 layer_rect,
621 layer_rect,
622 resource_id,
623 false,
624 gfx::PointF(layer_rect.x() / resource_scaled_size.width(),
625 layer_rect.y() / resource_scaled_size.height()),
626 gfx::PointF(layer_rect.right() / resource_scaled_size.width(),
627 layer_rect.bottom() / resource_scaled_size.height()),
628 screen_background_color,
629 vertex_opacity,
630 false);
631 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
632 } else {
633 // Skip the quad culler and just append the quads directly to avoid
634 // occlusion checks.
635 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
636 quad->SetNew(
637 shared_quad_state, layer_rect, screen_background_color, false);
638 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
639 }
[email protected]c1bb5af2013-03-13 19:06:27640 }
[email protected]467b3612012-08-28 07:41:16641}
642
[email protected]c1bb5af2013-03-13 19:06:27643bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
644 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14645
[email protected]c1bb5af2013-03-13 19:06:27646 if (!CanDraw() || !active_tree_->root_layer())
647 return false;
[email protected]2d692992012-12-19 01:19:32648
[email protected]c1bb5af2013-03-13 19:06:27649 TrackDamageForAllSurfaces(active_tree_->root_layer(),
650 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14651
[email protected]e0341352013-04-06 05:01:20652 // If the root render surface has no visible damage, then don't generate a
653 // frame at all.
654 RenderSurfaceImpl* root_surface =
655 active_tree_->root_layer()->render_surface();
656 bool root_surface_has_no_visible_damage =
657 !root_surface->damage_tracker()->current_damage_rect().Intersects(
658 root_surface->content_rect());
659 bool root_surface_has_contributing_layers =
660 !root_surface->layer_list().empty();
661 if (root_surface_has_contributing_layers &&
662 root_surface_has_no_visible_damage) {
663 TRACE_EVENT0("cc",
664 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
665 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45666 // A copy request should cause damage, so we should not have any copy
667 // requests in this case.
668 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07669 DCHECK(!output_surface_->capabilities()
670 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20671 return true;
672 }
673
[email protected]c1bb5af2013-03-13 19:06:27674 TRACE_EVENT1("cc",
675 "LayerTreeHostImpl::CalculateRenderPasses",
676 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06677 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14678
[email protected]c1bb5af2013-03-13 19:06:27679 // Create the render passes in dependency order.
680 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06681 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27682 --surface_index) {
683 LayerImpl* render_surface_layer =
684 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45685 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
686
687 bool should_draw_into_render_pass =
688 render_surface_layer->parent() == NULL ||
689 render_surface->contributes_to_drawn_surface() ||
690 render_surface_layer->HasCopyRequest();
691 if (should_draw_into_render_pass)
692 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27693 }
[email protected]94f206c12012-08-25 00:09:14694
[email protected]c1bb5af2013-03-13 19:06:27695 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52696 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27697 base::debug::TraceLog::GetInstance() &&
698 base::debug::TraceLog::GetInstance()->IsEnabled();
699 OcclusionTrackerImpl occlusion_tracker(
700 active_tree_->root_layer()->render_surface()->content_rect(),
701 record_metrics_for_frame);
702 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52703 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14704
[email protected]846f455b2013-03-18 19:07:41705 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27706 occlusion_tracker.set_occluding_screen_space_rects_container(
707 &frame->occluding_screen_space_rects);
708 }
[email protected]846f455b2013-03-18 19:07:41709 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27710 occlusion_tracker.set_non_occluding_screen_space_rects_container(
711 &frame->non_occluding_screen_space_rects);
712 }
[email protected]94f206c12012-08-25 00:09:14713
[email protected]c1bb5af2013-03-13 19:06:27714 // Add quads to the Render passes in FrontToBack order to allow for testing
715 // occlusion and performing culling during the tree walk.
716 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28717 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27718 RenderSurfaceImpl,
719 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14720
[email protected]c1bb5af2013-03-13 19:06:27721 // Typically when we are missing a texture and use a checkerboard quad, we
722 // still draw the frame. However when the layer being checkerboarded is moving
723 // due to an impl-animation, we drop the frame to avoid flashing due to the
724 // texture suddenly appearing in the future.
725 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25726 // When we have a copy request for a layer, we need to draw no matter
727 // what, as the layer may disappear after this frame.
728 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14729
[email protected]372bad5f2013-03-21 16:38:43730 int layers_drawn = 0;
731
[email protected]ffbb2212013-06-02 23:47:59732 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
733
[email protected]c1bb5af2013-03-13 19:06:27734 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09735 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27736 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09737 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27738 it != end;
739 ++it) {
740 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09741 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27742 RenderPass* target_render_pass =
743 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14744
[email protected]0e5f7142013-05-24 06:45:36745 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23746 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14747
[email protected]30fe19ff2013-07-04 00:54:45748 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30749
[email protected]18a70192013-04-26 16:18:25750 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36751 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25752 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11753 it->TakeCopyRequestsAndTransformToTarget(
754 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25755 }
[email protected]30fe19ff2013-07-04 00:54:45756 } else if (it.represents_contributing_render_surface() &&
757 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27758 RenderPass::Id contributing_render_pass_id =
759 it->render_surface()->RenderPassId();
760 RenderPass* contributing_render_pass =
761 frame->render_passes_by_id[contributing_render_pass_id];
762 AppendQuadsForRenderSurfaceLayer(target_render_pass,
763 *it,
764 contributing_render_pass,
765 occlusion_tracker,
766 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09767 } else if (it.represents_itself() &&
768 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27769 bool has_occlusion_from_outside_target_surface;
770 bool impl_draw_transform_is_unknown = false;
771 if (occlusion_tracker.Occluded(
772 it->render_target(),
773 it->visible_content_rect(),
774 it->draw_transform(),
775 impl_draw_transform_is_unknown,
776 it->is_clipped(),
777 it->clip_rect(),
778 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09779 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27780 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59781 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27782 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59783
[email protected]c1bb5af2013-03-13 19:06:27784 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57785
[email protected]c1bb5af2013-03-13 19:06:27786 if (it->HasContributingDelegatedRenderPasses()) {
787 RenderPass::Id contributing_render_pass_id =
788 it->FirstContributingRenderPassId();
789 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
790 frame->render_passes_by_id.end()) {
791 RenderPass* render_pass =
792 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14793
[email protected]c1bb5af2013-03-13 19:06:27794 AppendQuadsData append_quads_data(render_pass->id);
795 AppendQuadsForLayer(render_pass,
796 *it,
797 occlusion_tracker,
798 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57799
[email protected]c1bb5af2013-03-13 19:06:27800 contributing_render_pass_id =
801 it->NextContributingRenderPassId(contributing_render_pass_id);
802 }
[email protected]94f206c12012-08-25 00:09:14803 }
804
[email protected]c1bb5af2013-03-13 19:06:27805 AppendQuadsForLayer(target_render_pass,
806 *it,
807 occlusion_tracker,
808 &append_quads_data);
809 }
[email protected]89228202012-08-29 03:20:30810
[email protected]372bad5f2013-03-21 16:38:43811 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14812 }
813
[email protected]e1e768f2013-03-26 08:48:09814 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27815 target_render_pass->has_occlusion_from_outside_target_surface = true;
816
[email protected]e1e768f2013-03-26 08:48:09817 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43818 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09819 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27820 bool layer_has_animating_transform =
821 it->screen_space_transform_is_animating() ||
822 it->draw_transform_is_animating();
823 if (layer_has_animating_transform)
824 draw_frame = false;
825 }
826
[email protected]e1e768f2013-03-26 08:48:09827 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27828 frame->contains_incomplete_tile = true;
829
830 occlusion_tracker.LeaveLayer(it);
831 }
832
[email protected]6133cc232013-07-30 18:47:07833 if (have_copy_request ||
834 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25835 draw_frame = true;
836
[email protected]372bad5f2013-03-21 16:38:43837 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
838
[email protected]1d993172012-10-18 18:15:04839#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27840 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
841 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
842 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
843 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
844 != frame->render_passes_by_id.end());
845 }
[email protected]94f206c12012-08-25 00:09:14846#endif
[email protected]c1bb5af2013-03-13 19:06:27847 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14848
[email protected]c1bb5af2013-03-13 19:06:27849 if (!active_tree_->has_transparent_background()) {
850 frame->render_passes.back()->has_transparent_background = false;
[email protected]9f4f6a32013-09-04 21:35:12851 AppendQuadsToFillScreen(ResourceIdForUIResource(overhang_ui_resource_id_),
852 gfx::ScaleSize(overhang_ui_resource_size_,
853 device_scale_factor_),
854 frame->render_passes.back(),
[email protected]c1bb5af2013-03-13 19:06:27855 active_tree_->root_layer(),
856 active_tree_->background_color(),
857 occlusion_tracker);
858 }
[email protected]94f206c12012-08-25 00:09:14859
[email protected]c1bb5af2013-03-13 19:06:27860 if (draw_frame)
861 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25862 else
863 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14864
[email protected]c1bb5af2013-03-13 19:06:27865 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37866 if (!output_surface_->ForcedDrawToSoftwareDevice())
867 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]d15d9622013-06-21 02:49:15868 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
869 frame);
[email protected]94f206c12012-08-25 00:09:14870
[email protected]30fe19ff2013-07-04 00:54:45871 // Any copy requests left in the tree are not going to get serviced, and
872 // should be aborted.
873 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
874 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
875 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
876 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
877 }
878 for (size_t i = 0; i < requests_to_abort.size(); ++i)
879 requests_to_abort[i]->SendEmptyResult();
880
[email protected]e0341352013-04-06 05:01:20881 // If we're making a frame to draw, it better have at least one render pass.
882 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27883 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14884}
885
[email protected]200a9c062013-05-20 04:34:37886void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
887 if (input_handler_client_)
888 input_handler_client_->MainThreadHasStoppedFlinging();
889}
890
[email protected]3d9f7432013-04-06 00:35:18891void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
892 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15893 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01894 if (should_background_tick)
895 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15896
[email protected]3d9f7432013-04-06 00:35:18897 bool enabled = should_background_tick &&
898 !animation_registrar_->active_animation_controllers().empty();
899
[email protected]c1bb5af2013-03-13 19:06:27900 // Lazily create the time_source adapter so that we can vary the interval for
901 // testing.
902 if (!time_source_client_adapter_) {
903 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
904 this,
[email protected]810d40b72013-06-20 18:26:15905 DelayBasedTimeSource::Create(
906 LowFrequencyAnimationInterval(),
907 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
908 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27909 }
[email protected]94f206c12012-08-25 00:09:14910
[email protected]c1bb5af2013-03-13 19:06:27911 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14912}
913
[email protected]b9d4a362013-04-23 05:36:27914void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02915 viewport_damage_rect_.Union(damage_rect);
916}
917
[email protected]c1bb5af2013-03-13 19:06:27918static inline RenderPass* FindRenderPassById(
919 RenderPass::Id render_pass_id,
920 const LayerTreeHostImpl::FrameData& frame) {
921 RenderPassIdHashMap::const_iterator it =
922 frame.render_passes_by_id.find(render_pass_id);
923 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14924}
925
[email protected]c1bb5af2013-03-13 19:06:27926static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
927 LayerTreeHostImpl::FrameData* frame) {
928 RenderPass* remove_render_pass =
929 FindRenderPassById(remove_render_pass_id, *frame);
930 // The pass was already removed by another quad - probably the original, and
931 // we are the replica.
932 if (!remove_render_pass)
933 return;
934 RenderPassList& render_passes = frame->render_passes;
935 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
936 render_passes.end(),
937 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14938
[email protected]c1bb5af2013-03-13 19:06:27939 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14940
[email protected]c1bb5af2013-03-13 19:06:27941 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
942 frame->render_passes.erase(to_remove);
943 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14944
[email protected]c1bb5af2013-03-13 19:06:27945 // Now follow up for all RenderPass quads and remove their RenderPasses
946 // recursively.
947 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29948 QuadList::ConstBackToFrontIterator quad_list_iterator =
949 quad_list.BackToFrontBegin();
950 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27951 ++quad_list_iterator) {
952 DrawQuad* current_quad = (*quad_list_iterator);
953 if (current_quad->material != DrawQuad::RENDER_PASS)
954 continue;
[email protected]94f206c12012-08-25 00:09:14955
[email protected]c1bb5af2013-03-13 19:06:27956 RenderPass::Id next_remove_render_pass_id =
957 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
958 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
959 }
[email protected]94f206c12012-08-25 00:09:14960}
961
[email protected]c1bb5af2013-03-13 19:06:27962bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
963 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
964 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24965 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27966 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
967 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06968 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27969 if (quad_has_damage) {
970 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
971 return false;
972 } else if (!quad_has_cached_resource) {
973 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
974 return false;
975 }
976 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
977 return true;
[email protected]94f206c12012-08-25 00:09:14978}
979
[email protected]c1bb5af2013-03-13 19:06:27980bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
981 const RenderPassDrawQuad& quad, const FrameData& frame) const {
982 const RenderPass* render_pass =
983 FindRenderPassById(quad.render_pass_id, frame);
984 if (!render_pass)
985 return false;
[email protected]94f206c12012-08-25 00:09:14986
[email protected]c1bb5af2013-03-13 19:06:27987 // If any quad or RenderPass draws into this RenderPass, then keep it.
988 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29989 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
990 quad_list.BackToFrontBegin();
991 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27992 ++quad_list_iterator) {
993 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14994
[email protected]c1bb5af2013-03-13 19:06:27995 if (current_quad->material != DrawQuad::RENDER_PASS)
996 return false;
[email protected]94f206c12012-08-25 00:09:14997
[email protected]c1bb5af2013-03-13 19:06:27998 const RenderPass* contributing_pass = FindRenderPassById(
999 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1000 if (contributing_pass)
1001 return false;
1002 }
1003 return true;
[email protected]94f206c12012-08-25 00:09:141004}
1005
1006// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271007template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
1008 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
1009 CullRenderPassesWithCachedTextures culler, FrameData* frame);
1010template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
1011 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1012 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141013
1014// static
[email protected]c1bb5af2013-03-13 19:06:271015template <typename RenderPassCuller>
1016void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1017 FrameData* frame) {
1018 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1019 it != culler.RenderPassListEnd(frame->render_passes);
1020 it = culler.RenderPassListNext(it)) {
1021 const RenderPass* current_pass = frame->render_passes[it];
1022 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291023 QuadList::ConstBackToFrontIterator quad_list_iterator =
1024 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141025
[email protected]ed511b8d2013-03-25 03:29:291026 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271027 ++quad_list_iterator) {
1028 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141029
[email protected]c1bb5af2013-03-13 19:06:271030 if (current_quad->material != DrawQuad::RENDER_PASS)
1031 continue;
[email protected]94f206c12012-08-25 00:09:141032
[email protected]c1bb5af2013-03-13 19:06:271033 const RenderPassDrawQuad* render_pass_quad =
1034 RenderPassDrawQuad::MaterialCast(current_quad);
1035 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1036 continue;
[email protected]94f206c12012-08-25 00:09:141037
[email protected]c1bb5af2013-03-13 19:06:271038 // We are changing the vector in the middle of iteration. Because we
1039 // delete render passes that draw into the current pass, we are
1040 // guaranteed that any data from the iterator to the end will not
1041 // change. So, capture the iterator position from the end of the
1042 // list, and restore it after the change.
1043 size_t position_from_end = frame->render_passes.size() - it;
1044 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1045 it = frame->render_passes.size() - position_from_end;
1046 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141047 }
[email protected]c1bb5af2013-03-13 19:06:271048 }
[email protected]94f206c12012-08-25 00:09:141049}
1050
[email protected]e0341352013-04-06 05:01:201051bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
1052 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471053 TRACE_EVENT1("cc",
1054 "LayerTreeHostImpl::PrepareToDraw",
1055 "SourceFrameNumber",
1056 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141057
[email protected]425e5332013-09-21 04:10:031058 if (need_to_update_visible_tiles_before_draw_ &&
1059 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1060 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191061 }
[email protected]425e5332013-09-21 04:10:031062 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191063
[email protected]7d19dc342013-05-02 22:02:041064 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271065
[email protected]c1bb5af2013-03-13 19:06:271066 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1067 frame->render_passes.clear();
1068 frame->render_passes_by_id.clear();
1069 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201070 frame->contains_incomplete_tile = false;
1071 frame->has_no_damage = false;
1072
1073 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021074 device_viewport_damage_rect.Union(viewport_damage_rect_);
1075 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201076
1077 active_tree_->root_layer()->render_surface()->damage_tracker()->
1078 AddDamageNextUpdate(device_viewport_damage_rect);
1079 }
[email protected]94f206c12012-08-25 00:09:141080
[email protected]6133cc232013-07-30 18:47:071081 if (!CalculateRenderPasses(frame)) {
1082 DCHECK(!output_surface_->capabilities()
1083 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:271084 return false;
[email protected]6133cc232013-07-30 18:47:071085 }
[email protected]94f206c12012-08-25 00:09:141086
[email protected]c1bb5af2013-03-13 19:06:271087 // If we return true, then we expect DrawLayers() to be called before this
1088 // function is called again.
1089 return true;
[email protected]94f206c12012-08-25 00:09:141090}
1091
[email protected]0309ba5e02013-06-26 04:11:081092void LayerTreeHostImpl::EvictTexturesForTesting() {
1093 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1094}
1095
[email protected]4f48f6e2013-08-27 06:33:381096void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1097 NOTREACHED();
1098}
1099
[email protected]425e5332013-09-21 04:10:031100void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1101 DidInitializeVisibleTile();
1102}
1103
[email protected]c1bb5af2013-03-13 19:06:271104void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1105 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121106
[email protected]c1bb5af2013-03-13 19:06:271107 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:351108 visible_ ? policy.bytes_limit_when_visible
1109 : policy.bytes_limit_when_not_visible,
1110 ManagedMemoryPolicy::PriorityCutoffToValue(
1111 visible_ ? policy.priority_cutoff_when_visible
1112 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:271113 if (evicted_resources) {
1114 active_tree_->SetContentsTexturesPurged();
1115 if (pending_tree_)
1116 pending_tree_->SetContentsTexturesPurged();
1117 client_->SetNeedsCommitOnImplThread();
1118 client_->OnCanDrawStateChanged(CanDraw());
1119 client_->RenewTreePriority();
1120 }
1121 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431122
[email protected]8be1a9bf2013-05-01 03:45:191123 UpdateTileManagerMemoryPolicy(policy);
1124}
1125
1126void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1127 const ManagedMemoryPolicy& policy) {
1128 if (!tile_manager_)
1129 return;
1130
1131 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
1132 new_state.memory_limit_in_bytes = visible_ ?
1133 policy.bytes_limit_when_visible :
1134 policy.bytes_limit_when_not_visible;
1135 // TODO(reveman): We should avoid keeping around unused resources if
1136 // possible. crbug.com/224475
1137 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
1138 (static_cast<int64>(new_state.memory_limit_in_bytes) *
1139 settings_.max_unused_resource_memory_percentage) / 100);
1140 new_state.memory_limit_policy =
1141 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1142 visible_ ?
1143 policy.priority_cutoff_when_visible :
1144 policy.priority_cutoff_when_not_visible);
[email protected]3f2ff112013-08-03 02:41:071145 new_state.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081146
[email protected]8be1a9bf2013-05-01 03:45:191147 tile_manager_->SetGlobalState(new_state);
[email protected]c48536a52013-09-14 00:02:081148 DidModifyTilePriorities();
1149}
1150
1151void LayerTreeHostImpl::DidModifyTilePriorities() {
1152 DCHECK(settings_.impl_side_painting);
1153 // Mark priorities as dirty and schedule a ManageTiles().
1154 tile_priorities_dirty_ = true;
1155 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141156}
1157
[email protected]86126792013-03-16 20:07:541158void LayerTreeHostImpl::DidInitializeVisibleTile() {
1159 // TODO(reveman): Determine tiles that changed and only damage
1160 // what's necessary.
1161 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031162 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541163 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471164}
1165
[email protected]94bf75c2013-06-12 13:20:041166void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381167 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041168}
1169
[email protected]3f2ff112013-08-03 02:41:071170void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1171 SetManagedMemoryPolicy(policy, zero_budget_);
1172}
1173
1174void LayerTreeHostImpl::SetDiscardBackBufferWhenNotVisible(bool discard) {
1175 DCHECK(renderer_);
1176 renderer_->SetDiscardBackBufferWhenNotVisible(discard);
[email protected]fd32d122013-06-29 13:11:041177}
1178
[email protected]5f07afc2013-07-19 05:17:181179void LayerTreeHostImpl::SetTreeActivationCallback(
1180 const base::Closure& callback) {
1181 DCHECK(proxy_->IsImplThread());
1182 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261183 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181184}
1185
[email protected]c1bb5af2013-03-13 19:06:271186void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071187 const ManagedMemoryPolicy& policy, bool zero_budget) {
1188 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271189 return;
[email protected]61de5812012-11-08 07:03:441190
[email protected]3f2ff112013-08-03 02:41:071191 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1192
1193 cached_managed_memory_policy_ = policy;
1194 zero_budget_ = zero_budget;
1195 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1196
1197 if (old_policy == actual_policy)
1198 return;
1199
1200 if (!proxy_->HasImplThread()) {
1201 // In single-thread mode, this can be called on the main thread by
1202 // GLRenderer::OnMemoryAllocationChanged.
1203 DebugScopedSetImplThread impl_thread(proxy_);
1204 EnforceManagedMemoryPolicy(actual_policy);
1205 } else {
1206 DCHECK(proxy_->IsImplThread());
1207 EnforceManagedMemoryPolicy(actual_policy);
1208 }
[email protected]50644642013-06-20 13:58:551209
[email protected]d7626ffd2013-03-29 00:17:421210 // If there is already enough memory to draw everything imaginable and the
1211 // new memory limit does not change this, then do not re-commit. Don't bother
1212 // skipping commits if this is not visible (commits don't happen when not
1213 // visible, there will almost always be a commit when this becomes visible).
1214 bool needs_commit = true;
1215 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071216 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1217 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1218 actual_policy.priority_cutoff_when_visible ==
1219 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421220 needs_commit = false;
1221 }
1222
[email protected]d7626ffd2013-03-29 00:17:421223 if (needs_commit)
1224 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141225}
1226
[email protected]f224cc92013-06-06 23:23:321227void LayerTreeHostImpl::SetExternalDrawConstraints(
1228 const gfx::Transform& transform,
[email protected]54af03522013-09-05 00:43:281229 gfx::Rect viewport,
1230 gfx::Rect clip,
1231 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321232 external_transform_ = transform;
1233 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281234 external_clip_ = clip;
1235 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321236}
1237
[email protected]1cd9f5552013-04-26 04:22:031238void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1239 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1240}
1241
[email protected]df3c24c92013-06-19 03:54:351242void LayerTreeHostImpl::BeginFrame(const BeginFrameArgs& args) {
1243 client_->BeginFrameOnImplThread(args);
[email protected]7ed47512013-03-26 22:28:451244}
1245
[email protected]a7335e0b2013-09-18 09:34:511246void LayerTreeHostImpl::OnSwapBuffersComplete() {
1247 client_->OnSwapBuffersCompleteOnImplThread();
1248}
1249
1250void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271251 // TODO(piman): We may need to do some validation on this ack before
1252 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511253 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291254 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161255}
1256
[email protected]c1bb5af2013-03-13 19:06:271257void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1258 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291259}
1260
[email protected]c1bb5af2013-03-13 19:06:271261CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1262 CompositorFrameMetadata metadata;
1263 metadata.device_scale_factor = device_scale_factor_;
1264 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1265 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1266 metadata.root_layer_size = active_tree_->ScrollableSize();
1267 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1268 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501269 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271270 if (top_controls_manager_) {
1271 metadata.location_bar_offset =
1272 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1273 metadata.location_bar_content_translation =
1274 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011275 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271276 }
[email protected]bf189f62012-12-18 03:42:111277
[email protected]c1bb5af2013-03-13 19:06:271278 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111279 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271280
[email protected]ffb2720f2013-03-15 19:18:371281 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271282
1283 return metadata;
[email protected]bf189f62012-12-18 03:42:111284}
1285
[email protected]6f50b8fa2013-07-01 19:51:491286static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1287 layer->DidBeginTracing();
1288}
[email protected]37349bc2013-06-04 01:31:521289
[email protected]f0c2a242013-03-15 19:34:521290void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1291 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351292 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271293 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201294
[email protected]3519b872013-07-30 07:17:501295 if (frame->has_no_damage) {
1296 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071297 DCHECK(!output_surface_->capabilities()
1298 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201299 return;
[email protected]3519b872013-07-30 07:17:501300 }
[email protected]e0341352013-04-06 05:01:201301
[email protected]c1bb5af2013-03-13 19:06:271302 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141303
[email protected]a9dc0d0f2013-08-17 02:43:181304 int old_dropped_frame_count = fps_counter_->dropped_frame_count();
[email protected]213fcfc2013-08-24 14:59:271305 fps_counter_->SaveTimeStamp(frame_begin_time,
1306 !output_surface_->context_provider());
[email protected]94f206c12012-08-25 00:09:141307
[email protected]c1e6cc062013-08-24 03:35:351308 bool on_main_thread = false;
1309 rendering_stats_instrumentation_->IncrementScreenFrameCount(
1310 1, on_main_thread);
[email protected]a9dc0d0f2013-08-17 02:43:181311 rendering_stats_instrumentation_->IncrementDroppedFrameCount(
1312 fps_counter_->dropped_frame_count() - old_dropped_frame_count);
[email protected]372bad5f2013-03-21 16:38:431313
[email protected]c1bb5af2013-03-13 19:06:271314 if (tile_manager_) {
1315 memory_history_->SaveEntry(
1316 tile_manager_->memory_stats_from_last_assign());
1317 }
[email protected]1191d9d2013-02-02 06:00:331318
[email protected]846f455b2013-03-18 19:07:411319 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021320 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271321 active_tree_->root_layer(),
1322 *frame->render_surface_layer_list,
1323 frame->occluding_screen_space_rects,
1324 frame->non_occluding_screen_space_rects,
1325 debug_state_);
1326 }
[email protected]94f206c12012-08-25 00:09:141327
[email protected]a848c102013-03-26 08:59:091328 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1329 const RenderingStats& stats =
1330 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181331 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091332 }
1333
[email protected]37349bc2013-06-04 01:31:521334 bool is_new_trace;
1335 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1336 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491337 if (pending_tree_) {
1338 LayerTreeHostCommon::CallFunctionForSubtree(
1339 pending_tree_->root_layer(),
1340 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1341 }
1342 LayerTreeHostCommon::CallFunctionForSubtree(
1343 active_tree_->root_layer(),
1344 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521345 }
1346
[email protected]9197dbcb2013-05-15 20:28:511347 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211348 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1349 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1350 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081351
[email protected]c1bb5af2013-03-13 19:06:271352 // Because the contents of the HUD depend on everything else in the frame, the
1353 // contents of its texture are updated as the last thing before the frame is
1354 // drawn.
1355 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541356 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141357
[email protected]b09c1942013-05-10 00:06:371358 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321359 bool allow_partial_swap = false;
1360
[email protected]b09c1942013-05-10 00:06:371361 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321362 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
1363 temp_software_renderer->DrawFrame(
1364 &frame->render_passes, NULL, device_scale_factor_, allow_partial_swap);
[email protected]b09c1942013-05-10 00:06:371365 } else {
[email protected]573e8e32013-09-09 17:24:321366 // We don't track damage on the HUD layer (it interacts with damage tracking
1367 // visualizations), so disable partial swaps to make the HUD layer display
1368 // properly.
1369 bool allow_partial_swap = !debug_state_.ShowHudRects();
1370
[email protected]b5174d712013-08-28 08:10:431371 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321372 offscreen_context_provider_.get(),
1373 device_scale_factor_,
1374 allow_partial_swap);
[email protected]b09c1942013-05-10 00:06:371375 }
[email protected]c1bb5af2013-03-13 19:06:271376 // The render passes should be consumed by the renderer.
1377 DCHECK(frame->render_passes.empty());
1378 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141379
[email protected]c1bb5af2013-03-13 19:06:271380 // The next frame should start by assuming nothing has changed, and changes
1381 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321382 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1383 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541384 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271385 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1386 DidDrawDamagedArea();
1387 }
1388 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351389
1390 rendering_stats_instrumentation_->IssueTraceEventForImplThreadStats();
[email protected]a9dc0d0f2013-08-17 02:43:181391 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141392}
1393
[email protected]c1bb5af2013-03-13 19:06:271394void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1395 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1396 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521397
[email protected]c1bb5af2013-03-13 19:06:271398 // Once all layers have been drawn, pending texture uploads should no
1399 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151400 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141401}
1402
[email protected]c1bb5af2013-03-13 19:06:271403void LayerTreeHostImpl::FinishAllRendering() {
1404 if (renderer_)
1405 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141406}
1407
[email protected]c1bb5af2013-03-13 19:06:271408bool LayerTreeHostImpl::IsContextLost() {
1409 DCHECK(proxy_->IsImplThread());
1410 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141411}
1412
[email protected]c1bb5af2013-03-13 19:06:271413const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1414 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141415}
1416
[email protected]e0341352013-04-06 05:01:201417bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1418 if (frame.has_no_damage)
1419 return false;
[email protected]36e5ff12013-06-11 12:19:291420 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161421 active_tree_->ClearLatencyInfo();
1422 return true;
[email protected]94f206c12012-08-25 00:09:141423}
1424
[email protected]c8cbae72013-05-23 10:45:031425void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451426 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031427 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451428}
1429
[email protected]54af03522013-09-05 00:43:281430gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]4f0f43a2013-07-31 17:44:441431 // The container layer bounds should be used if non-overlay scrollbars may
1432 // exist since it adjusts for them.
1433 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]573e8e32013-09-09 17:24:321434 if (!settings_.solid_color_scrollbars && container_layer) {
[email protected]4f0f43a2013-07-31 17:44:441435 DCHECK(!top_controls_manager_);
1436 DCHECK_EQ(0, overdraw_bottom_height_);
1437 return container_layer->bounds();
1438 }
1439
[email protected]ffb2720f2013-03-15 19:18:371440 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281441 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371442
[email protected]ed511b8d2013-03-25 03:29:291443 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371444 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401445 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291446 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371447}
1448
[email protected]c1bb5af2013-03-13 19:06:271449void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011450 // TODO(jamesr): The renderer_ check is needed to make some of the
1451 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1452 // important) in production. We should adjust the test to not need this.
1453 if (renderer_)
1454 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141455}
1456
[email protected]c1bb5af2013-03-13 19:06:271457void LayerTreeHostImpl::Readback(void* pixels,
1458 gfx::Rect rect_in_device_viewport) {
1459 DCHECK(renderer_);
1460 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141461}
1462
[email protected]59adb112013-04-09 04:48:441463bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341464 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011465}
1466
[email protected]c1bb5af2013-03-13 19:06:271467LayerImpl* LayerTreeHostImpl::RootLayer() const {
1468 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081469}
1470
[email protected]c1bb5af2013-03-13 19:06:271471LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1472 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081473}
1474
[email protected]c1bb5af2013-03-13 19:06:271475LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1476 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081477}
1478
[email protected]94f206c12012-08-25 00:09:141479// Content layers can be either directly scrollable or contained in an outer
1480// scrolling layer which applies the scroll transform. Given a content layer,
1481// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271482static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1483 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141484 return 0;
[email protected]c1bb5af2013-03-13 19:06:271485
1486 if (layer_impl->scrollable())
1487 return layer_impl;
1488
1489 if (layer_impl->DrawsContent() &&
1490 layer_impl->parent() &&
1491 layer_impl->parent()->scrollable())
1492 return layer_impl->parent();
1493
1494 return 0;
[email protected]94f206c12012-08-25 00:09:141495}
1496
[email protected]c1bb5af2013-03-13 19:06:271497void LayerTreeHostImpl::CreatePendingTree() {
1498 CHECK(!pending_tree_);
1499 if (recycle_tree_)
1500 recycle_tree_.swap(pending_tree_);
1501 else
1502 pending_tree_ = LayerTreeImpl::create(this);
1503 client_->OnCanDrawStateChanged(CanDraw());
[email protected]c1bb5af2013-03-13 19:06:271504 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1505 TRACE_EVENT_ASYNC_STEP0("cc",
1506 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271507}
1508
[email protected]8bb6cfa2013-07-23 00:11:191509void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191510 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1511 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191512 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481513}
1514
[email protected]c1bb5af2013-03-13 19:06:271515void LayerTreeHostImpl::ActivatePendingTree() {
1516 CHECK(pending_tree_);
1517 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351518
[email protected]4f48f6e2013-08-27 06:33:381519 need_to_update_visible_tiles_before_draw_ = true;
1520
[email protected]1960a712013-04-30 17:06:471521 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271522 active_tree_->PushPersistedState(pending_tree_.get());
1523 if (pending_tree_->needs_full_tree_sync()) {
1524 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331525 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271526 active_tree_->DetachLayerTree(),
1527 active_tree_.get()));
1528 }
[email protected]b5651c22013-03-14 15:06:331529 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271530 active_tree_->root_layer());
1531 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511532
[email protected]c9280762013-08-01 06:28:571533 // Process any requests in the UI resource queue. The request queue is given
1534 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1535 // the swap.
1536 pending_tree_->ProcessUIResourceRequestQueue();
1537
[email protected]c1bb5af2013-03-13 19:06:271538 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511539
[email protected]c1bb5af2013-03-13 19:06:271540 // Now that we've synced everything from the pending tree to the active
1541 // tree, rename the pending tree the recycle tree so we can reuse it on the
1542 // next sync.
1543 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511544
[email protected]1960a712013-04-30 17:06:471545 active_tree_->SetRootLayerScrollOffsetDelegate(
1546 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271547 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221548
[email protected]c1bb5af2013-03-13 19:06:271549 // Reduce wasted memory now that unlinked resources are guaranteed not
1550 // to be used.
1551 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411552
[email protected]c1bb5af2013-03-13 19:06:271553 client_->OnCanDrawStateChanged(CanDraw());
[email protected]c1bb5af2013-03-13 19:06:271554 client_->SetNeedsRedrawOnImplThread();
1555 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241556
[email protected]a848c102013-03-26 08:59:091557 if (debug_state_.continuous_painting) {
1558 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431559 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091560 paint_time_counter_->SavePaintTime(
[email protected]a9dc0d0f2013-08-17 02:43:181561 stats.main_stats.paint_time + stats.main_stats.record_time +
1562 stats.impl_stats.rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271563 }
[email protected]2a61ad52013-05-13 14:01:291564
1565 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261566 if (!tree_activation_callback_.is_null())
1567 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011568
1569 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1570 DCHECK(active_tree_->root_layer());
[email protected]2e7ca422012-12-20 02:57:271571}
1572
[email protected]c1bb5af2013-03-13 19:06:271573void LayerTreeHostImpl::SetVisible(bool visible) {
1574 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141575
[email protected]c1bb5af2013-03-13 19:06:271576 if (visible_ == visible)
1577 return;
1578 visible_ = visible;
1579 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121580 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141581
[email protected]127bdc1a2013-09-11 01:44:481582 if (!visible_)
1583 EvictAllUIResources();
1584
[email protected]0fa42e02013-08-20 01:16:051585 // Evict tiles immediately if invisible since this tab may never get another
1586 // draw or timer tick.
1587 if (!visible_)
1588 ManageTiles();
1589
[email protected]c1bb5af2013-03-13 19:06:271590 if (!renderer_)
1591 return;
[email protected]94f206c12012-08-25 00:09:141592
[email protected]c1bb5af2013-03-13 19:06:271593 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141594}
1595
[email protected]206a3922013-05-17 06:34:121596ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071597 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1598 if (debug_state_.rasterize_only_visible_content) {
1599 actual.priority_cutoff_when_not_visible =
1600 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1601 actual.priority_cutoff_when_visible =
1602 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1603 }
[email protected]206a3922013-05-17 06:34:121604
[email protected]3f2ff112013-08-03 02:41:071605 if (zero_budget_) {
1606 actual.bytes_limit_when_visible = 0;
1607 actual.bytes_limit_when_not_visible = 0;
1608 }
1609
[email protected]206a3922013-05-17 06:34:121610 return actual;
1611}
1612
[email protected]3f2ff112013-08-03 02:41:071613size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1614 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1615}
1616
[email protected]990e050a2013-09-23 18:50:211617int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1618 return ManagedMemoryPolicy::PriorityCutoffToValue(
1619 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1620}
1621
[email protected]50644642013-06-20 13:58:551622void LayerTreeHostImpl::ReleaseTreeResources() {
1623 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551624 SendReleaseResourcesRecursive(active_tree_->root_layer());
1625 if (pending_tree_ && pending_tree_->root_layer())
1626 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1627 if (recycle_tree_ && recycle_tree_->root_layer())
1628 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571629
[email protected]127bdc1a2013-09-11 01:44:481630 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551631}
1632
1633void LayerTreeHostImpl::CreateAndSetRenderer(
1634 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201635 ResourceProvider* resource_provider,
1636 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551637 DCHECK(!renderer_);
1638 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321639 renderer_ = DelegatingRenderer::Create(
1640 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091641 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551642 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321643 &settings_,
[email protected]50644642013-06-20 13:58:551644 output_surface,
1645 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111646 texture_mailbox_deleter_.get(),
[email protected]50644642013-06-20 13:58:551647 settings_.highp_threshold_min,
1648 settings_.force_direct_layer_drawing);
1649 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321650 renderer_ = SoftwareRenderer::Create(
1651 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551652 }
1653
[email protected]84975832013-06-29 00:24:111654 if (renderer_) {
[email protected]50644642013-06-20 13:58:551655 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111656 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561657
1658 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1659 // initialized to get max texture size. Also, after releasing resources,
1660 // trees need another update to generate new ones.
1661 active_tree_->set_needs_update_draw_properties();
1662 if (pending_tree_)
1663 pending_tree_->set_needs_update_draw_properties();
[email protected]84975832013-06-29 00:24:111664 }
[email protected]50644642013-06-20 13:58:551665}
1666
[email protected]50af3c22013-07-13 03:50:201667void LayerTreeHostImpl::CreateAndSetTileManager(
1668 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011669 ContextProvider* context_provider,
[email protected]50af3c22013-07-13 03:50:201670 bool using_map_image) {
1671 DCHECK(settings_.impl_side_painting);
1672 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011673 tile_manager_ =
1674 TileManager::Create(this,
1675 resource_provider,
1676 settings_.num_raster_threads,
1677 rendering_stats_instrumentation_,
1678 using_map_image,
1679 GetMaxTransferBufferUsageBytes(context_provider));
1680
[email protected]50af3c22013-07-13 03:50:201681 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191682 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201683}
1684
[email protected]50644642013-06-20 13:58:551685void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071686 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551687}
1688
[email protected]c1bb5af2013-03-13 19:06:271689bool LayerTreeHostImpl::InitializeRenderer(
1690 scoped_ptr<OutputSurface> output_surface) {
1691 // Since we will create a new resource provider, we cannot continue to use
1692 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1693 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551694 ReleaseTreeResources();
[email protected]7ba3ca72013-04-11 06:37:251695 if (resource_provider_)
1696 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401697
[email protected]c1bb5af2013-03-13 19:06:271698 // Note: order is important here.
1699 renderer_.reset();
1700 tile_manager_.reset();
1701 resource_provider_.reset();
1702 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141703
[email protected]c1bb5af2013-03-13 19:06:271704 if (!output_surface->BindToClient(this))
1705 return false;
[email protected]be3181652012-09-25 13:02:131706
[email protected]50644642013-06-20 13:58:551707 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
[email protected]27a41fe2013-09-19 05:05:141708 output_surface.get(),
1709 settings_.highp_threshold_min,
1710 settings_.use_rgba_4444_textures);
[email protected]50644642013-06-20 13:58:551711 if (!resource_provider)
1712 return false;
[email protected]2b154b22013-06-07 09:03:271713
[email protected]50644642013-06-20 13:58:551714 if (output_surface->capabilities().deferred_gl_initialization)
1715 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131716
[email protected]50af3c22013-07-13 03:50:201717 bool skip_gl_renderer = false;
1718 CreateAndSetRenderer(
1719 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131720
[email protected]50644642013-06-20 13:58:551721 if (!renderer_)
1722 return false;
[email protected]6123930c2013-05-23 06:48:371723
[email protected]50644642013-06-20 13:58:551724 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201725 CreateAndSetTileManager(resource_provider.get(),
[email protected]b6eb8e332013-09-10 00:51:011726 output_surface->context_provider().get(),
[email protected]50af3c22013-07-13 03:50:201727 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241728 }
1729
[email protected]049fc7a2013-06-18 12:32:351730 // Setup BeginFrameEmulation if it's not supported natively
1731 if (!settings_.begin_frame_scheduling_enabled) {
1732 const base::TimeDelta display_refresh_interval =
1733 base::TimeDelta::FromMicroseconds(
1734 base::Time::kMicrosecondsPerSecond /
1735 settings_.refresh_rate);
1736
1737 output_surface->InitializeBeginFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151738 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351739 settings_.throttle_frame_production,
1740 display_refresh_interval);
1741 }
1742
1743 int max_frames_pending =
1744 output_surface->capabilities().max_frames_pending;
1745 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351746 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351747 output_surface->SetMaxFramesPending(max_frames_pending);
1748
[email protected]50644642013-06-20 13:58:551749 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271750 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141751
[email protected]c1bb5af2013-03-13 19:06:271752 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211753
[email protected]c1bb5af2013-03-13 19:06:271754 return true;
[email protected]94f206c12012-08-25 00:09:141755}
1756
[email protected]2b154b22013-06-07 09:03:271757bool LayerTreeHostImpl::DeferredInitialize(
1758 scoped_refptr<ContextProvider> offscreen_context_provider) {
1759 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551760 DCHECK(settings_.impl_side_painting);
1761 DCHECK(settings_.solid_color_scrollbars);
[email protected]0634cdd42013-08-16 00:46:091762 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271763
[email protected]50644642013-06-20 13:58:551764 ReleaseTreeResources();
1765 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271766
[email protected]d9ca99e2013-08-28 21:49:481767 bool resource_provider_success = resource_provider_->InitializeGL();
1768
1769 bool success = resource_provider_success;
1770 if (success) {
1771 bool skip_gl_renderer = false;
1772 CreateAndSetRenderer(
1773 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1774 if (!renderer_)
1775 success = false;
1776 }
1777
1778 if (success) {
1779 if (offscreen_context_provider.get() &&
1780 !offscreen_context_provider->BindToCurrentThread())
1781 success = false;
1782 }
1783
[email protected]50644642013-06-20 13:58:551784 if (success) {
1785 EnforceZeroBudget(false);
1786 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481787 } else {
1788 if (offscreen_context_provider.get()) {
1789 if (offscreen_context_provider->BindToCurrentThread())
1790 offscreen_context_provider->VerifyContexts();
1791 offscreen_context_provider = NULL;
1792 }
1793
1794 client_->DidLoseOutputSurfaceOnImplThread();
1795
1796 if (resource_provider_success) {
1797 // If this fails the context provider will be dropped from the output
1798 // surface and destroyed. But the GLRenderer expects the output surface
1799 // to stick around - and hold onto the context3d - as long as it is alive.
1800 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1801 renderer_.reset();
1802
1803 // The resource provider can't stay in GL mode or it tries to clean up GL
1804 // stuff, but the context provider is going away on the output surface
1805 // which contradicts being in GL mode.
1806 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1807 resource_provider_->InitializeSoftware();
1808 }
[email protected]50644642013-06-20 13:58:551809 }
[email protected]b5174d712013-08-28 08:10:431810
1811 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271812 return success;
1813}
1814
[email protected]50af3c22013-07-13 03:50:201815void LayerTreeHostImpl::ReleaseGL() {
1816 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1817 DCHECK(settings_.impl_side_painting);
1818 DCHECK(settings_.solid_color_scrollbars);
[email protected]0634cdd42013-08-16 00:46:091819 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201820
1821 ReleaseTreeResources();
1822 renderer_.reset();
1823 tile_manager_.reset();
1824 resource_provider_->InitializeSoftware();
1825
1826 bool skip_gl_renderer = true;
1827 CreateAndSetRenderer(
1828 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1829 DCHECK(renderer_);
1830
1831 EnforceZeroBudget(true);
1832 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011833 NULL,
[email protected]50af3c22013-07-13 03:50:201834 GetRendererCapabilities().using_map_image);
1835 DCHECK(tile_manager_);
1836
[email protected]b5174d712013-08-28 08:10:431837 SetOffscreenContextProvider(NULL);
1838
[email protected]50af3c22013-07-13 03:50:201839 client_->SetNeedsCommitOnImplThread();
1840}
1841
[email protected]18ce59702013-04-09 04:58:401842void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1843 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271844 return;
[email protected]94f206c12012-08-25 00:09:141845
[email protected]54af03522013-09-05 00:43:281846 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271847 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271848
[email protected]c1bb5af2013-03-13 19:06:271849 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141850
[email protected]c1bb5af2013-03-13 19:06:271851 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141852
[email protected]c1bb5af2013-03-13 19:06:271853 if (renderer_)
1854 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211855
[email protected]c1bb5af2013-03-13 19:06:271856 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441857 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141858}
1859
[email protected]d9083762013-03-24 01:36:401860void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1861 if (overdraw_bottom_height == overdraw_bottom_height_)
1862 return;
1863 overdraw_bottom_height_ = overdraw_bottom_height;
1864
1865 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441866 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401867}
1868
[email protected]9f4f6a32013-09-04 21:35:121869void LayerTreeHostImpl::SetOverhangUIResource(
1870 UIResourceId overhang_ui_resource_id,
1871 gfx::Size overhang_ui_resource_size) {
1872 overhang_ui_resource_id_ = overhang_ui_resource_id;
1873 overhang_ui_resource_size_ = overhang_ui_resource_size;
1874}
1875
[email protected]c1bb5af2013-03-13 19:06:271876void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1877 if (device_scale_factor == device_scale_factor_)
1878 return;
1879 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271880
[email protected]94c40e6292013-05-24 22:01:501881 if (renderer_)
1882 renderer_->ViewportChanged();
1883
[email protected]c1bb5af2013-03-13 19:06:271884 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441885 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141886}
1887
[email protected]54af03522013-09-05 00:43:281888gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1889 return DeviceViewport().size();
1890}
1891
[email protected]f224cc92013-06-06 23:23:321892gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1893 if (external_viewport_.IsEmpty())
1894 return gfx::Rect(device_viewport_size_);
1895
1896 return external_viewport_;
1897}
1898
[email protected]54af03522013-09-05 00:43:281899gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1900 if (external_clip_.IsEmpty())
1901 return DeviceViewport();
1902
1903 return external_clip_;
1904}
1905
1906const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321907 return external_transform_;
1908}
1909
[email protected]c1bb5af2013-03-13 19:06:271910void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1911 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141912}
1913
[email protected]59adb112013-04-09 04:48:441914void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271915 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441916 active_tree_->set_needs_update_draw_properties();
1917 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141918}
1919
[email protected]c1bb5af2013-03-13 19:06:271920bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041921 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341922 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141923}
1924
[email protected]200a9c062013-05-20 04:34:371925void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1926 DCHECK(input_handler_client_ == NULL);
1927 input_handler_client_ = client;
1928}
1929
[email protected]420fdf6e2013-08-26 20:36:381930static LayerImpl* NextScrollLayer(LayerImpl* layer) {
1931 if (LayerImpl* scroll_parent = layer->scroll_parent())
1932 return scroll_parent;
1933 return layer->parent();
1934}
1935
[email protected]5ff3c9782013-04-29 17:35:121936InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1937 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291938 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141939
[email protected]c1bb5af2013-03-13 19:06:271940 if (top_controls_manager_)
1941 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381942
[email protected]c1bb5af2013-03-13 19:06:271943 DCHECK(!CurrentlyScrollingLayer());
1944 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141945
[email protected]c1bb5af2013-03-13 19:06:271946 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141947 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141948
[email protected]c1bb5af2013-03-13 19:06:271949 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1950 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141951
[email protected]c1bb5af2013-03-13 19:06:271952 // First find out which layer was hit from the saved list of visible layers
1953 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391954 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271955 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521956
[email protected]c1bb5af2013-03-13 19:06:271957 // Walk up the hierarchy and look for a scrollable layer.
1958 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:381959 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:271960 // The content layer can also block attempts to scroll outside the main
1961 // thread.
1962 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1963 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431964 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271965 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1966 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141967 }
1968
[email protected]c1bb5af2013-03-13 19:06:271969 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1970 if (!scroll_layer_impl)
1971 continue;
[email protected]94f206c12012-08-25 00:09:141972
[email protected]c1bb5af2013-03-13 19:06:271973 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141974
[email protected]c1bb5af2013-03-13 19:06:271975 // If any layer wants to divert the scroll event to the main thread, abort.
1976 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431977 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271978 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1979 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141980 }
1981
[email protected]c1bb5af2013-03-13 19:06:271982 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1983 potentially_scrolling_layer_impl = scroll_layer_impl;
1984 }
1985
1986 // When hiding top controls is enabled and the controls are hidden or
1987 // overlaying the content, force scrolls to be enabled on the root layer to
1988 // allow bringing the top controls back into view.
1989 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1990 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521991 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271992 potentially_scrolling_layer_impl = RootScrollLayer();
1993 }
1994
1995 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201996 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271997 potentially_scrolling_layer_impl);
1998 should_bubble_scrolls_ = (type != NonBubblingGesture);
1999 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:432000 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:272001 client_->RenewTreePriority();
2002 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2003 return ScrollStarted;
2004 }
2005 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142006}
2007
[email protected]c1bb5af2013-03-13 19:06:272008gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2009 LayerImpl* layer_impl,
2010 float scale_from_viewport_to_screen_space,
2011 gfx::PointF viewport_point,
2012 gfx::Vector2dF viewport_delta) {
2013 // Layers with non-invertible screen space transforms should not have passed
2014 // the scroll hit test in the first place.
2015 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2016 gfx::Transform inverse_screen_space_transform(
2017 gfx::Transform::kSkipInitialization);
2018 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2019 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352020 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2021 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272022 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142023
[email protected]c1bb5af2013-03-13 19:06:272024 gfx::PointF screen_space_point =
2025 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142026
[email protected]c1bb5af2013-03-13 19:06:272027 gfx::Vector2dF screen_space_delta = viewport_delta;
2028 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2029
2030 // First project the scroll start and end points to local layer space to find
2031 // the scroll delta in layer coordinates.
2032 bool start_clipped, end_clipped;
2033 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2034 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212035 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272036 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212037 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272038 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212039 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272040 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212041 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272042
2043 // In general scroll point coordinates should not get clipped.
2044 DCHECK(!start_clipped);
2045 DCHECK(!end_clipped);
2046 if (start_clipped || end_clipped)
2047 return gfx::Vector2dF();
2048
2049 // local_start_point and local_end_point are in content space but we want to
2050 // move them to layer space for scrolling.
2051 float width_scale = 1.f / layer_impl->contents_scale_x();
2052 float height_scale = 1.f / layer_impl->contents_scale_y();
2053 local_start_point.Scale(width_scale, height_scale);
2054 local_end_point.Scale(width_scale, height_scale);
2055
2056 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472057 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272058 layer_impl->ScrollBy(local_end_point - local_start_point);
2059
2060 // Get the end point in the layer's content space so we can apply its
2061 // ScreenSpaceTransform.
2062 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472063 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272064 previous_delta;
2065 gfx::PointF actual_local_content_end_point =
2066 gfx::ScalePoint(actual_local_end_point,
2067 1.f / width_scale,
2068 1.f / height_scale);
2069
2070 // Calculate the applied scroll delta in viewport space coordinates.
2071 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212072 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272073 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212074 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272075 DCHECK(!end_clipped);
2076 if (end_clipped)
2077 return gfx::Vector2dF();
2078 gfx::PointF actual_viewport_end_point =
2079 gfx::ScalePoint(actual_screen_space_end_point,
2080 1.f / scale_from_viewport_to_screen_space);
2081 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142082}
2083
[email protected]c1bb5af2013-03-13 19:06:272084static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
2085 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:472086 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272087 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472088 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272089}
2090
2091bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
2092 gfx::Vector2dF scroll_delta) {
2093 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2094 if (!CurrentlyScrollingLayer())
2095 return false;
2096
2097 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532098 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292099 bool did_scroll_x = false;
2100 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:422101 bool consume_by_top_controls = top_controls_manager_ &&
2102 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062103
[email protected]c1bb5af2013-03-13 19:06:272104 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2105 layer_impl;
2106 layer_impl = layer_impl->parent()) {
2107 if (!layer_impl->scrollable())
2108 continue;
2109
[email protected]a2b5ded2013-05-20 21:32:532110 if (layer_impl == RootScrollLayer()) {
2111 // Only allow bubble scrolling when the scroll is in the direction to make
2112 // the top controls visible.
2113 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
2114 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
2115 UpdateMaxScrollOffset();
2116 }
2117 // Track root layer deltas for reporting overscroll.
2118 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422119 }
2120
[email protected]c1bb5af2013-03-13 19:06:272121 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272122 // Gesture events need to be transformed from viewport coordinates to local
2123 // layer coordinates so that the scrolling contents exactly follow the
2124 // user's finger. In contrast, wheel events represent a fixed amount of
2125 // scrolling so we can just apply them directly.
2126 if (!wheel_scrolling_) {
2127 float scale_from_viewport_to_screen_space = device_scale_factor_;
2128 applied_delta =
2129 ScrollLayerWithViewportSpaceDelta(layer_impl,
2130 scale_from_viewport_to_screen_space,
2131 viewport_point, pending_delta);
2132 } else {
2133 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142134 }
[email protected]94f206c12012-08-25 00:09:142135
[email protected]c1bb5af2013-03-13 19:06:272136 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292137 float move_threshold = 0.1f;
2138 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2139 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2140 did_scroll_x |= did_move_layer_x;
2141 did_scroll_y |= did_move_layer_y;
2142 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]c1bb5af2013-03-13 19:06:272143 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
2144 continue;
2145 else
2146 break;
[email protected]94f206c12012-08-25 00:09:142147 }
[email protected]a2b5ded2013-05-20 21:32:532148
2149 if (layer_impl == RootScrollLayer())
2150 unused_root_delta.Subtract(applied_delta);
2151
[email protected]c1bb5af2013-03-13 19:06:272152 did_lock_scrolling_layer_ = true;
2153 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202154 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272155 break;
[email protected]94f206c12012-08-25 00:09:142156 }
[email protected]94f206c12012-08-25 00:09:142157
[email protected]c1bb5af2013-03-13 19:06:272158 // If the applied delta is within 45 degrees of the input delta, bail out to
2159 // make it easier to scroll just one layer in one direction without
2160 // affecting any of its parents.
2161 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212162 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272163 applied_delta, pending_delta) < angle_threshold) {
2164 pending_delta = gfx::Vector2d();
2165 break;
[email protected]4a23c374c2012-12-08 08:38:552166 }
[email protected]c1bb5af2013-03-13 19:06:272167
2168 // Allow further movement only on an axis perpendicular to the direction in
2169 // which the layer moved.
2170 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212171 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272172
[email protected]a2b5ded2013-05-20 21:32:532173 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272174 break;
2175 }
2176
[email protected]2bd503f2013-07-23 05:35:292177 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272178 if (did_scroll) {
2179 client_->SetNeedsCommitOnImplThread();
2180 client_->SetNeedsRedrawOnImplThread();
2181 client_->RenewTreePriority();
2182 }
[email protected]a2b5ded2013-05-20 21:32:532183
[email protected]2bd503f2013-07-23 05:35:292184 // Scrolling along an axis resets accumulated root overscroll for that axis.
2185 if (did_scroll_x)
2186 accumulated_root_overscroll_.set_x(0);
2187 if (did_scroll_y)
2188 accumulated_root_overscroll_.set_y(0);
2189
[email protected]a2b5ded2013-05-20 21:32:532190 accumulated_root_overscroll_ += unused_root_delta;
2191 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2192 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112193 DidOverscrollParams params;
2194 params.accumulated_overscroll = accumulated_root_overscroll_;
2195 params.latest_overscroll_delta = unused_root_delta;
2196 params.current_fling_velocity = current_fling_velocity_;
2197 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532198 }
2199
[email protected]c1bb5af2013-03-13 19:06:272200 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552201}
2202
[email protected]be782f52013-03-23 21:36:142203// This implements scrolling by page as described here:
2204// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2205// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492206bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2207 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142208 DCHECK(wheel_scrolling_);
2209
2210 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2211 layer_impl;
2212 layer_impl = layer_impl->parent()) {
2213 if (!layer_impl->scrollable())
2214 continue;
2215
2216 if (!layer_impl->vertical_scrollbar_layer())
2217 continue;
2218
2219 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2220
2221 // These magical values match WebKit and are designed to scroll nearly the
2222 // entire visible content height but leave a bit of overlap.
2223 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492224 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142225 page = -page;
2226
2227 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2228
2229 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2230
2231 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142232 client_->SetNeedsCommitOnImplThread();
2233 client_->SetNeedsRedrawOnImplThread();
2234 client_->RenewTreePriority();
2235 return true;
2236 }
2237
2238 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2239 }
2240
2241 return false;
2242}
2243
[email protected]1960a712013-04-30 17:06:472244void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2245 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2246 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2247 active_tree_->SetRootLayerScrollOffsetDelegate(
2248 root_layer_scroll_offset_delegate_);
2249}
2250
2251void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2252 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2253 client_->SetNeedsCommitOnImplThread();
2254}
2255
[email protected]c1bb5af2013-03-13 19:06:272256void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2257 active_tree_->ClearCurrentlyScrollingLayer();
2258 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532259 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202260 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142261}
2262
[email protected]c1bb5af2013-03-13 19:06:272263void LayerTreeHostImpl::ScrollEnd() {
2264 if (top_controls_manager_)
2265 top_controls_manager_->ScrollEnd();
2266 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052267 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142268}
2269
[email protected]5ff3c9782013-04-29 17:35:122270InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462271 if (!active_tree_->CurrentlyScrollingLayer())
2272 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212273
[email protected]75147f52013-08-02 19:11:462274 if (settings_.ignore_root_layer_flings &&
2275 active_tree_->CurrentlyScrollingLayer() ==
2276 active_tree_->RootScrollLayer()) {
2277 ClearCurrentlyScrollingLayer();
2278 return ScrollIgnored;
2279 }
2280
2281 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212282}
2283
[email protected]6e921bd2013-04-29 21:10:202284void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2285 current_fling_velocity_ = velocity;
2286}
2287
[email protected]c1bb5af2013-03-13 19:06:272288void LayerTreeHostImpl::PinchGestureBegin() {
2289 pinch_gesture_active_ = true;
2290 previous_pinch_anchor_ = gfx::Point();
2291 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132292 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]9ec24df2013-08-09 23:32:572293 active_tree_->SetCurrentlyScrollingLayer(RootScrollLayer());
[email protected]94f206c12012-08-25 00:09:142294}
2295
[email protected]c1bb5af2013-03-13 19:06:272296void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2297 gfx::Point anchor) {
2298 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282299
[email protected]c1bb5af2013-03-13 19:06:272300 if (!RootScrollLayer())
2301 return;
[email protected]d3afa112012-12-08 06:24:282302
[email protected]c1bb5af2013-03-13 19:06:272303 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2304 // position over the course of the magnify.
2305 float page_scale_delta = active_tree_->page_scale_delta();
2306 gfx::PointF previous_scale_anchor =
2307 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2308 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2309 page_scale_delta = active_tree_->page_scale_delta();
2310 gfx::PointF new_scale_anchor =
2311 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2312 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2313
2314 previous_pinch_anchor_ = anchor;
2315
2316 move.Scale(1 / active_tree_->page_scale_factor());
2317
2318 RootScrollLayer()->ScrollBy(move);
2319
[email protected]c1bb5af2013-03-13 19:06:272320 client_->SetNeedsCommitOnImplThread();
2321 client_->SetNeedsRedrawOnImplThread();
2322 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282323}
2324
[email protected]c1bb5af2013-03-13 19:06:272325void LayerTreeHostImpl::PinchGestureEnd() {
2326 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132327 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2328 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2329 ClearCurrentlyScrollingLayer();
2330 }
[email protected]c1bb5af2013-03-13 19:06:272331 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142332}
2333
[email protected]c1bb5af2013-03-13 19:06:272334static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2335 LayerImpl* layer_impl) {
2336 if (!layer_impl)
2337 return;
[email protected]94f206c12012-08-25 00:09:142338
[email protected]c1bb5af2013-03-13 19:06:272339 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472340 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272341 if (!scroll_delta.IsZero()) {
2342 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392343 scroll.layer_id = layer_impl->id();
2344 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272345 scroll_info->scrolls.push_back(scroll);
2346 layer_impl->SetSentScrollDelta(scroll_delta);
2347 }
[email protected]94f206c12012-08-25 00:09:142348
[email protected]c1bb5af2013-03-13 19:06:272349 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2350 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142351}
2352
[email protected]c1bb5af2013-03-13 19:06:272353scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2354 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302355
[email protected]c1bb5af2013-03-13 19:06:272356 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392357 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2358 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302359
[email protected]c1bb5af2013-03-13 19:06:272360 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302361}
2362
[email protected]c1bb5af2013-03-13 19:06:272363void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282364 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102365}
2366
[email protected]c1bb5af2013-03-13 19:06:272367void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2368 if (!page_scale_animation_ || !RootScrollLayer())
2369 return;
2370
2371 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2372 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472373 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272374
2375 active_tree_->SetPageScaleDelta(
2376 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2377 active_tree_->page_scale_factor());
2378 gfx::Vector2dF next_scroll =
2379 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2380
2381 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2382 client_->SetNeedsRedrawOnImplThread();
2383
2384 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2385 page_scale_animation_.reset();
2386 client_->SetNeedsCommitOnImplThread();
2387 client_->RenewTreePriority();
2388 }
[email protected]829ad972013-01-28 23:36:102389}
2390
[email protected]ffb2720f2013-03-15 19:18:372391void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2392 if (!top_controls_manager_ || !RootScrollLayer())
2393 return;
2394 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2395 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172396 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2397 return;
[email protected]ffb2720f2013-03-15 19:18:372398 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2399 scroll, 1.f / active_tree_->total_page_scale_factor()));
2400}
2401
[email protected]c1bb5af2013-03-13 19:06:272402void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2403 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522404 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272405 animation_registrar_->active_animation_controllers().empty() ||
2406 !active_tree_->root_layer())
2407 return;
2408
2409 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2410
2411 last_animation_time_ = wall_clock_time;
2412 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2413
2414 AnimationRegistrar::AnimationControllerMap copy =
2415 animation_registrar_->active_animation_controllers();
2416 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2417 iter != copy.end();
2418 ++iter)
2419 (*iter).second->Animate(monotonic_seconds);
2420
2421 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082422}
2423
[email protected]3d9f7432013-04-06 00:35:182424void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522425 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272426 animation_registrar_->active_animation_controllers().empty() ||
2427 !active_tree_->root_layer())
2428 return;
2429
2430 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2431 scoped_ptr<AnimationEventsVector> events =
2432 make_scoped_ptr(new AnimationEventsVector);
2433 AnimationRegistrar::AnimationControllerMap copy =
2434 animation_registrar_->active_animation_controllers();
2435 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2436 iter != copy.end();
2437 ++iter)
[email protected]3d9f7432013-04-06 00:35:182438 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272439
2440 if (!events->empty()) {
2441 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2442 last_animation_time_);
2443 }
[email protected]131a0c22013-02-12 18:31:082444}
2445
[email protected]c1bb5af2013-03-13 19:06:272446base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2447 return base::TimeDelta::FromSeconds(1);
2448}
2449
[email protected]50644642013-06-20 13:58:552450void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352451 DCHECK(current);
[email protected]50644642013-06-20 13:58:552452 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352453 current->DidLoseOutputSurface();
2454 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552455 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352456 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552457 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352458 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552459 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352460}
2461
[email protected]b5174d712013-08-28 08:10:432462void LayerTreeHostImpl::SetOffscreenContextProvider(
2463 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2464 if (!offscreen_context_provider.get()) {
2465 offscreen_context_provider_ = NULL;
2466 return;
2467 }
2468
2469 if (!offscreen_context_provider->BindToCurrentThread()) {
2470 offscreen_context_provider_ = NULL;
2471 return;
2472 }
2473
2474 offscreen_context_provider_ = offscreen_context_provider;
2475}
2476
[email protected]c1bb5af2013-03-13 19:06:272477std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2478 std::string str;
2479 if (active_tree_->root_layer()) {
2480 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2481 base::JSONWriter::WriteWithOptions(
2482 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2483 }
2484 return str;
2485}
2486
[email protected]c1bb5af2013-03-13 19:06:272487int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362488 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272489}
2490
[email protected]c1bb5af2013-03-13 19:06:272491void LayerTreeHostImpl::SendManagedMemoryStats(
2492 size_t memory_visible_bytes,
2493 size_t memory_visible_and_nearby_bytes,
2494 size_t memory_use_bytes) {
2495 if (!renderer_)
2496 return;
2497
2498 // Round the numbers being sent up to the next 8MB, to throttle the rate
2499 // at which we spam the GPU process.
2500 static const size_t rounding_step = 8 * 1024 * 1024;
2501 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2502 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2503 rounding_step);
2504 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2505 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2506 last_sent_memory_visible_and_nearby_bytes_ ==
2507 memory_visible_and_nearby_bytes &&
2508 last_sent_memory_use_bytes_ == memory_use_bytes) {
2509 return;
2510 }
2511 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2512 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2513 last_sent_memory_use_bytes_ = memory_use_bytes;
2514
2515 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2516 last_sent_memory_visible_and_nearby_bytes_,
2517 last_sent_memory_use_bytes_);
2518}
2519
2520void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2521 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2522}
2523
2524void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2525 base::TimeTicks time) {
2526 if (!layer)
2527 return;
2528
2529 ScrollbarAnimationController* scrollbar_controller =
2530 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352531 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202532 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422533 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2534 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272535 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202536 }
[email protected]c1bb5af2013-03-13 19:06:272537
2538 for (size_t i = 0; i < layer->children().size(); ++i)
2539 AnimateScrollbarsRecursive(layer->children()[i], time);
2540}
2541
[email protected]21c9dee72013-06-15 01:20:052542void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202543 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052544 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202545}
2546
2547void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2548 base::TimeTicks time) {
2549 if (!layer)
2550 return;
2551
2552 ScrollbarAnimationController* scrollbar_controller =
2553 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352554 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2555 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202556 if (delay > base::TimeDelta())
2557 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352558 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202559 client_->SetNeedsRedrawOnImplThread();
2560 }
2561
2562 for (size_t i = 0; i < layer->children().size(); ++i)
2563 StartScrollbarAnimationRecursive(layer->children()[i], time);
2564}
2565
[email protected]c1bb5af2013-03-13 19:06:272566void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2567 if (!tile_manager_)
2568 return;
2569
2570 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2571 if (new_state.tree_priority == priority)
2572 return;
2573
2574 new_state.tree_priority = priority;
2575 tile_manager_->SetGlobalState(new_state);
[email protected]c48536a52013-09-14 00:02:082576 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272577}
2578
[email protected]8347d692013-05-17 23:22:382579void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552580 current_frame_timeticks_ = base::TimeTicks();
2581 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272582}
2583
[email protected]21c9dee72013-06-15 01:20:052584void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2585 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552586 if (ticks->is_null()) {
2587 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052588 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552589 *now = base::Time::Now();
2590 }
2591}
2592
2593base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2594 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2595 return current_frame_timeticks_;
2596}
2597
2598base::Time LayerTreeHostImpl::CurrentFrameTime() {
2599 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272600 return current_frame_time_;
2601}
2602
[email protected]21c9dee72013-06-15 01:20:052603base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
2604 return base::TimeTicks::Now();
2605}
2606
[email protected]34806722013-08-09 23:51:212607scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2608 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272609 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222610 if (this->pending_tree_)
2611 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272612 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212613 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272614 if (tile_manager_)
2615 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2616 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222617 if (pending_tree_)
2618 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212619 if (frame)
2620 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222621 return state.PassAs<base::Value>();
2622}
2623
2624scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222625 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2626 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472627 if (tile_manager_)
2628 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272629 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082630}
2631
[email protected]6e7fdeb2013-07-09 14:28:382632void LayerTreeHostImpl::SetDebugState(
2633 const LayerTreeDebugState& new_debug_state) {
2634 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2635 return;
2636 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272637 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242638
[email protected]6e7fdeb2013-07-09 14:28:382639 debug_state_ = new_debug_state;
2640 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022641}
2642
[email protected]741fba422013-09-20 03:34:142643void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2644 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572645 DCHECK_GT(uid, 0);
[email protected]741fba422013-09-20 03:34:142646 DCHECK_EQ(bitmap.GetFormat(), UIResourceBitmap::RGBA8);
[email protected]c9280762013-08-01 06:28:572647
[email protected]efa48412013-09-05 15:30:162648 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142649 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162650 case UIResourceBitmap::CLAMP_TO_EDGE:
2651 wrap_mode = GL_CLAMP_TO_EDGE;
2652 break;
2653 case UIResourceBitmap::REPEAT:
2654 wrap_mode = GL_REPEAT;
2655 break;
2656 }
2657
[email protected]c9280762013-08-01 06:28:572658 // Allow for multiple creation requests with the same UIResourceId. The
2659 // previous resource is simply deleted.
2660 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2661 if (id)
2662 DeleteUIResource(uid);
2663 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142664 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162665 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142666 ResourceProvider::TextureUsageAny,
2667 resource_provider_->best_texture_format());
[email protected]c9280762013-08-01 06:28:572668
2669 ui_resource_map_[uid] = id;
2670 resource_provider_->SetPixels(id,
[email protected]741fba422013-09-20 03:34:142671 bitmap.GetPixels(),
2672 gfx::Rect(bitmap.GetSize()),
2673 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572674 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482675 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572676}
2677
2678void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2679 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2680 if (id) {
2681 resource_provider_->DeleteResource(id);
2682 ui_resource_map_.erase(uid);
2683 }
[email protected]127bdc1a2013-09-11 01:44:482684 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572685}
2686
[email protected]127bdc1a2013-09-11 01:44:482687void LayerTreeHostImpl::EvictAllUIResources() {
2688 if (ui_resource_map_.empty())
2689 return;
2690
[email protected]5f4dc4f2013-09-05 14:58:212691 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2692 iter != ui_resource_map_.end();
2693 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482694 evicted_ui_resources_.insert(iter->first);
[email protected]5f4dc4f2013-09-05 14:58:212695 resource_provider_->DeleteResource(iter->second);
2696 }
2697 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482698
2699 client_->SetNeedsCommitOnImplThread();
2700 client_->OnCanDrawStateChanged(CanDraw());
2701 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212702}
2703
[email protected]c9280762013-08-01 06:28:572704ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2705 UIResourceId uid) const {
2706 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2707 if (iter != ui_resource_map_.end())
2708 return iter->second;
2709 return 0;
2710}
2711
[email protected]127bdc1a2013-09-11 01:44:482712bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2713 return !evicted_ui_resources_.empty();
2714}
2715
2716void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2717 std::set<UIResourceId>::iterator found_in_evicted =
2718 evicted_ui_resources_.find(uid);
2719 if (found_in_evicted == evicted_ui_resources_.end())
2720 return;
2721 evicted_ui_resources_.erase(found_in_evicted);
2722 if (evicted_ui_resources_.empty())
2723 client_->OnCanDrawStateChanged(CanDraw());
2724}
2725
[email protected]d3143c732012-10-05 19:17:592726} // namespace cc