blob: c61e6bec0d0c99f7f90801f421dd448b928bb9cb [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>
8
[email protected]ad5d1422012-10-19 13:40:299#include "base/basictypes.h"
[email protected]4a23c374c2012-12-08 08:38:5510#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1411#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3412#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4813#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0914#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4015#include "cc/animation/timing_function.h"
[email protected]681ccff2013-03-18 06:13:5216#include "cc/base/math_util.h"
17#include "cc/base/util.h"
[email protected]6e84de22013-03-18 06:54:2718#include "cc/debug/debug_rect_history.h"
19#include "cc/debug/frame_rate_counter.h"
20#include "cc/debug/overdraw_metrics.h"
21#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4322#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2223#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2124#include "cc/input/page_scale_animation.h"
25#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5226#include "cc/layers/append_quads_data.h"
27#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2828#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5229#include "cc/layers/layer_iterator.h"
[email protected]50761e92013-03-29 20:51:2830#include "cc/layers/render_surface_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/scrollbar_layer_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3032#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4533#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3034#include "cc/output/delegating_renderer.h"
35#include "cc/output/gl_renderer.h"
36#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5637#include "cc/quads/render_pass_draw_quad.h"
38#include "cc/quads/shared_quad_state.h"
39#include "cc/quads/solid_color_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4040#include "cc/resources/memory_history.h"
41#include "cc/resources/picture_layer_tiling.h"
42#include "cc/resources/prioritized_resource_manager.h"
[email protected]c9280762013-08-01 06:28:5743#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3144#include "cc/scheduler/delay_based_time_source.h"
45#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0446#include "cc/trees/damage_tracker.h"
47#include "cc/trees/layer_tree_host.h"
48#include "cc/trees/layer_tree_host_common.h"
49#include "cc/trees/layer_tree_impl.h"
50#include "cc/trees/quad_culler.h"
51#include "cc/trees/single_thread_proxy.h"
52#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0653#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1354#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1455
[email protected]94f206c12012-08-25 00:09:1456namespace {
57
[email protected]c1bb5af2013-03-13 19:06:2758void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
59 if (visible) {
60 TRACE_EVENT_ASYNC_BEGIN1("webkit",
61 "LayerTreeHostImpl::SetVisible",
62 id,
63 "LayerTreeHostImpl",
64 id);
65 return;
66 }
[email protected]94f206c12012-08-25 00:09:1467
[email protected]c1bb5af2013-03-13 19:06:2768 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1469}
70
[email protected]c1bb5af2013-03-13 19:06:2771} // namespace
[email protected]94f206c12012-08-25 00:09:1472
[email protected]9c88e562012-09-14 22:21:3073namespace cc {
[email protected]94f206c12012-08-25 00:09:1474
[email protected]96baf3e2012-10-22 23:09:5575class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:2776 public:
77 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
78 LayerTreeHostImpl* layer_tree_host_impl,
79 scoped_refptr<DelayBasedTimeSource> time_source) {
80 return make_scoped_ptr(
81 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
82 time_source));
83 }
84 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:2685 time_source_->SetClient(NULL);
86 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:2787 }
88
[email protected]6d0e69d2013-03-20 14:53:2689 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:2790 // In single threaded mode we attempt to simulate changing the current
91 // thread by maintaining a fake thread id. When we switch from one
92 // thread to another, we construct DebugScopedSetXXXThread objects that
93 // update the thread id. This lets DCHECKS that ensure we're on the
94 // right thread to work correctly in single threaded mode. The problem
95 // here is that the timer tasks are run via the message loop, and when
96 // they run, we've had no chance to construct a DebugScopedSetXXXThread
97 // object. The result is that we report that we're running on the main
98 // thread. In multi-threaded mode, this timer is run on the compositor
99 // thread, so to keep this consistent in single-threaded mode, we'll
100 // construct a DebugScopedSetImplThread object. There is no need to do
101 // this in multi-threaded mode since the real thread id's will be
102 // correct. In fact, setting fake thread id's interferes with the real
103 // thread id's and causes breakage.
104 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
105 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
106 set_impl_thread.reset(
107 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14108 }
109
[email protected]94bf75c2013-06-12 13:20:04110 // TODO(enne): This should probably happen post-animate.
111 if (layer_tree_host_impl_->pending_tree()) {
112 layer_tree_host_impl_->ActivatePendingTreeIfNeeded();
113
114 if (layer_tree_host_impl_->pending_tree()) {
115 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
116 layer_tree_host_impl_->ManageTiles();
117 }
118 }
119
[email protected]fb7425a2013-04-22 16:28:55120 layer_tree_host_impl_->Animate(
121 layer_tree_host_impl_->CurrentFrameTimeTicks(),
122 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18123 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
124 bool start_ready_animations = true;
125 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38126 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27127 }
[email protected]373974232013-01-10 22:20:50128
[email protected]c1bb5af2013-03-13 19:06:27129 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26130 if (active != time_source_->Active())
131 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27132 }
[email protected]94f206c12012-08-25 00:09:14133
[email protected]c1bb5af2013-03-13 19:06:27134 private:
135 LayerTreeHostImplTimeSourceAdapter(
136 LayerTreeHostImpl* layer_tree_host_impl,
137 scoped_refptr<DelayBasedTimeSource> time_source)
138 : layer_tree_host_impl_(layer_tree_host_impl),
139 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26140 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27141 }
[email protected]94f206c12012-08-25 00:09:14142
[email protected]c1bb5af2013-03-13 19:06:27143 LayerTreeHostImpl* layer_tree_host_impl_;
144 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14145
[email protected]c1bb5af2013-03-13 19:06:27146 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14147};
148
[email protected]96baf3e2012-10-22 23:09:55149LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20150 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27151
152LayerTreeHostImpl::FrameData::~FrameData() {}
153
154scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
155 const LayerTreeSettings& settings,
156 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43157 Proxy* proxy,
158 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
159 return make_scoped_ptr(
160 new LayerTreeHostImpl(settings,
161 client,
162 proxy,
163 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10164}
165
[email protected]372bad5f2013-03-21 16:38:43166LayerTreeHostImpl::LayerTreeHostImpl(
167 const LayerTreeSettings& settings,
168 LayerTreeHostImplClient* client,
169 Proxy* proxy,
170 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27171 : client_(client),
172 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37173 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27174 did_lock_scrolling_layer_(false),
175 should_bubble_scrolls_(false),
176 wheel_scrolling_(false),
[email protected]a23451e2013-06-07 20:58:26177 manage_tiles_needed_(false),
[email protected]1960a712013-04-30 17:06:47178 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27179 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27180 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07181 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34182 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27183 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
184 0,
[email protected]3f2ff112013-08-03 02:41:07185 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING,
186 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27187 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36188 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29189 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27190 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02191 debug_rect_history_(DebugRectHistory::Create()),
[email protected]d7626ffd2013-03-29 00:17:42192 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27193 last_sent_memory_visible_bytes_(0),
194 last_sent_memory_visible_and_nearby_bytes_(0),
195 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55196 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32197 device_scale_factor_(1.f),
198 overdraw_bottom_height_(0.f),
[email protected]c32a1962013-07-30 19:41:17199 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43200 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19201 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]8bb6cfa2013-07-23 00:11:19202 need_to_update_visible_tiles_before_draw_(false) {
[email protected]c1bb5af2013-03-13 19:06:27203 DCHECK(proxy_->IsImplThread());
204 DidVisibilityChange(this, visible_);
205
[email protected]8e0176d2013-03-21 03:14:52206 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27207
[email protected]8e0176d2013-03-21 03:14:52208 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27209 top_controls_manager_ =
210 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52211 settings.top_controls_height,
212 settings.top_controls_show_threshold,
213 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27214 }
215
[email protected]8e0176d2013-03-21 03:14:52216 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27217
218 // LTHI always has an active tree.
219 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51220 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
221 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10222}
223
[email protected]c1bb5af2013-03-13 19:06:27224LayerTreeHostImpl::~LayerTreeHostImpl() {
225 DCHECK(proxy_->IsImplThread());
226 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51227 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
228 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27229
[email protected]200a9c062013-05-20 04:34:37230 if (input_handler_client_) {
231 input_handler_client_->WillShutdown();
232 input_handler_client_ = NULL;
233 }
234
[email protected]75deb742013-06-24 20:19:18235 // The layer trees must be destroyed before the layer tree host. We've
236 // made a contract with our animation controllers that the registrar
237 // will outlive them, and we must make good.
238 recycle_tree_.reset();
239 pending_tree_.reset();
240 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14241}
242
[email protected]c1bb5af2013-03-13 19:06:27243void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29244
[email protected]c1bb5af2013-03-13 19:06:27245void LayerTreeHostImpl::CommitComplete() {
246 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08247
[email protected]c1bb5af2013-03-13 19:06:27248 // Impl-side painting needs an update immediately post-commit to have the
249 // opportunity to create tilings. Other paths can call UpdateDrawProperties
250 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52251 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27252 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04253 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26254 // Start working on newly created tiles immediately if needed.
255 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27256 } else {
257 active_tree_->set_needs_update_draw_properties();
258 }
[email protected]3ba4cae2013-01-16 03:58:38259
[email protected]c1bb5af2013-03-13 19:06:27260 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14261}
262
[email protected]6133cc232013-07-30 18:47:07263bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27264 // Note: If you are changing this function or any other function that might
265 // affect the result of CanDraw, make sure to call
266 // client_->OnCanDrawStateChanged in the proper places and update the
267 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14268
[email protected]6133cc232013-07-30 18:47:07269 if (!renderer_) {
270 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
271 TRACE_EVENT_SCOPE_THREAD);
272 return false;
273 }
274
275 // Must have an OutputSurface if |renderer_| is not NULL.
276 DCHECK(output_surface_);
277
278 // TODO(boliu): Make draws without root_layer work and move this below
279 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27280 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42281 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
282 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22283 return false;
[email protected]c1bb5af2013-03-13 19:06:27284 }
[email protected]6133cc232013-07-30 18:47:07285
286 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
287 return true;
288
[email protected]c1bb5af2013-03-13 19:06:27289 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42290 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
291 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27292 return false;
293 }
294 if (active_tree_->ViewportSizeInvalid()) {
295 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42296 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
297 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27298 return false;
299 }
[email protected]c1bb5af2013-03-13 19:06:27300 if (active_tree_->ContentsTexturesPurged()) {
301 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42302 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
303 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27304 return false;
305 }
306 return true;
[email protected]2f1acc262012-11-16 21:42:22307}
308
[email protected]c1bb5af2013-03-13 19:06:27309void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
310 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37311 if (input_handler_client_)
312 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27313 AnimatePageScale(monotonic_time);
314 AnimateLayers(monotonic_time, wall_clock_time);
315 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37316 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14317}
318
[email protected]c1bb5af2013-03-13 19:06:27319void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26320 if (!tile_manager_)
321 return;
322 if (!manage_tiles_needed_)
323 return;
324 manage_tiles_needed_ = false;
[email protected]c1bb5af2013-03-13 19:06:27325 tile_manager_->ManageTiles();
326
327 size_t memory_required_bytes;
328 size_t memory_nice_to_have_bytes;
329 size_t memory_used_bytes;
330 tile_manager_->GetMemoryStats(&memory_required_bytes,
331 &memory_nice_to_have_bytes,
332 &memory_used_bytes);
333 SendManagedMemoryStats(memory_required_bytes,
334 memory_nice_to_have_bytes,
335 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15336}
337
[email protected]c1bb5af2013-03-13 19:06:27338void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
339 bool anchor_point,
340 float page_scale,
341 base::TimeTicks start_time,
342 base::TimeDelta duration) {
343 if (!RootScrollLayer())
344 return;
345
346 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47347 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27348 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51349 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27350
351 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40352
353 // Easing constants experimentally determined.
354 scoped_ptr<TimingFunction> timing_function =
355 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
356
[email protected]c1bb5af2013-03-13 19:06:27357 page_scale_animation_ =
358 PageScaleAnimation::Create(scroll_total,
359 active_tree_->total_page_scale_factor(),
360 viewport_size,
361 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40362 start_time_seconds,
363 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27364
365 if (anchor_point) {
366 gfx::Vector2dF anchor(target_offset);
367 page_scale_animation_->ZoomWithAnchor(anchor,
368 page_scale,
369 duration.InSecondsF());
370 } else {
371 gfx::Vector2dF scaled_target_offset = target_offset;
372 page_scale_animation_->ZoomTo(scaled_target_offset,
373 page_scale,
374 duration.InSecondsF());
375 }
376
377 client_->SetNeedsRedrawOnImplThread();
378 client_->SetNeedsCommitOnImplThread();
379 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15380}
381
[email protected]c1bb5af2013-03-13 19:06:27382void LayerTreeHostImpl::ScheduleAnimation() {
383 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15384}
385
[email protected]c1bb5af2013-03-13 19:06:27386bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
387 if (!EnsureRenderSurfaceLayerList())
388 return false;
[email protected]f57bbc02012-11-21 07:02:15389
[email protected]c1bb5af2013-03-13 19:06:27390 gfx::PointF device_viewport_point =
391 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15392
[email protected]c1bb5af2013-03-13 19:06:27393 // First find out which layer was hit from the saved list of visible layers
394 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39395 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27396 device_viewport_point,
397 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15398
[email protected]c1bb5af2013-03-13 19:06:27399 // Walk up the hierarchy and look for a layer with a touch event handler
400 // region that the given point hits.
401 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39402 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27403 layer_impl))
404 return true;
405 }
[email protected]f57bbc02012-11-21 07:02:15406
[email protected]c1bb5af2013-03-13 19:06:27407 return false;
408}
409
[email protected]b76329d2013-06-11 04:15:08410void LayerTreeHostImpl::SetLatencyInfoForInputEvent(
411 const ui::LatencyInfo& latency_info) {
412 active_tree()->SetLatencyInfo(latency_info);
413}
414
[email protected]c1bb5af2013-03-13 19:06:27415void LayerTreeHostImpl::TrackDamageForAllSurfaces(
416 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28417 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27418 // For now, we use damage tracking to compute a global scissor. To do this, we
419 // must compute all damage tracking before drawing anything, so that we know
420 // the root damage rect. The root damage rect is then used to scissor each
421 // surface.
422
423 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06424 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27425 --surface_index) {
426 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
427 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
428 DCHECK(render_surface);
429 render_surface->damage_tracker()->UpdateDamageTrackingState(
430 render_surface->layer_list(),
431 render_surface_layer->id(),
432 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
433 render_surface->content_rect(),
434 render_surface_layer->mask_layer(),
435 render_surface_layer->filters(),
436 render_surface_layer->filter().get());
437 }
438}
439
440void LayerTreeHostImpl::FrameData::AppendRenderPass(
441 scoped_ptr<RenderPass> render_pass) {
442 render_passes_by_id[render_pass->id] = render_pass.get();
443 render_passes.push_back(render_pass.Pass());
444}
445
[email protected]ffbb2212013-06-02 23:47:59446static DrawMode GetDrawMode(OutputSurface* output_surface) {
447 if (output_surface->ForcedDrawToSoftwareDevice()) {
448 return DRAW_MODE_RESOURCELESS_SOFTWARE;
449 } else if (output_surface->context3d()) {
450 return DRAW_MODE_HARDWARE;
451 } else {
452 DCHECK(output_surface->software_device());
453 return DRAW_MODE_SOFTWARE;
454 }
455}
456
[email protected]c1bb5af2013-03-13 19:06:27457static void AppendQuadsForLayer(RenderPass* target_render_pass,
458 LayerImpl* layer,
459 const OcclusionTrackerImpl& occlusion_tracker,
460 AppendQuadsData* append_quads_data) {
461 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49462 QuadCuller quad_culler(&target_render_pass->quad_list,
463 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27464 layer,
465 occlusion_tracker,
466 layer->ShowDebugBorders(),
467 for_surface);
468 layer->AppendQuads(&quad_culler, append_quads_data);
469}
470
471static void AppendQuadsForRenderSurfaceLayer(
472 RenderPass* target_render_pass,
473 LayerImpl* layer,
474 const RenderPass* contributing_render_pass,
475 const OcclusionTrackerImpl& occlusion_tracker,
476 AppendQuadsData* append_quads_data) {
477 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49478 QuadCuller quad_culler(&target_render_pass->quad_list,
479 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27480 layer,
481 occlusion_tracker,
482 layer->ShowDebugBorders(),
483 for_surface);
484
485 bool is_replica = false;
486 layer->render_surface()->AppendQuads(&quad_culler,
487 append_quads_data,
488 is_replica,
489 contributing_render_pass->id);
490
491 // Add replica after the surface so that it appears below the surface.
492 if (layer->has_replica()) {
493 is_replica = true;
494 layer->render_surface()->AppendQuads(&quad_culler,
495 append_quads_data,
496 is_replica,
497 contributing_render_pass->id);
498 }
499}
500
501static void AppendQuadsToFillScreen(
502 RenderPass* target_render_pass,
503 LayerImpl* root_layer,
504 SkColor screen_background_color,
505 const OcclusionTrackerImpl& occlusion_tracker) {
506 if (!root_layer || !SkColorGetA(screen_background_color))
507 return;
508
509 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
510 if (fill_region.IsEmpty())
511 return;
512
513 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49514 QuadCuller quad_culler(&target_render_pass->quad_list,
515 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27516 root_layer,
517 occlusion_tracker,
518 root_layer->ShowDebugBorders(),
519 for_surface);
520
521 // Manually create the quad state for the gutter quads, as the root layer
522 // doesn't have any bounds and so can't generate this itself.
523 // TODO(danakj): Make the gutter quads generated by the solid color layer
524 // (make it smarter about generating quads to fill unoccluded areas).
525
526 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
527 float opacity = 1.f;
528 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49529 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27530 shared_quad_state->SetAll(root_layer->draw_transform(),
531 root_target_rect.size(),
532 root_target_rect,
533 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01534 false,
[email protected]f57bbc02012-11-21 07:02:15535 opacity);
536
[email protected]c1bb5af2013-03-13 19:06:27537 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17538
[email protected]c1bb5af2013-03-13 19:06:27539 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
540 bool did_invert = root_layer->screen_space_transform().GetInverse(
541 &transform_to_layer_space);
542 DCHECK(did_invert);
543 for (Region::Iterator fill_rects(fill_region);
544 fill_rects.has_rect();
545 fill_rects.next()) {
546 // The root layer transform is composed of translations and scales only,
547 // no perspective, so mapping is sufficient (as opposed to projecting).
548 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21549 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27550 // Skip the quad culler and just append the quads directly to avoid
551 // occlusion checks.
552 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]10a30b12013-05-02 16:42:11553 quad->SetNew(shared_quad_state, layer_rect, screen_background_color, false);
[email protected]c7e95b42013-03-18 01:13:49554 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27555 }
[email protected]467b3612012-08-28 07:41:16556}
557
[email protected]c1bb5af2013-03-13 19:06:27558bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
559 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14560
[email protected]c1bb5af2013-03-13 19:06:27561 if (!CanDraw() || !active_tree_->root_layer())
562 return false;
[email protected]2d692992012-12-19 01:19:32563
[email protected]c1bb5af2013-03-13 19:06:27564 TrackDamageForAllSurfaces(active_tree_->root_layer(),
565 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14566
[email protected]e0341352013-04-06 05:01:20567 // If the root render surface has no visible damage, then don't generate a
568 // frame at all.
569 RenderSurfaceImpl* root_surface =
570 active_tree_->root_layer()->render_surface();
571 bool root_surface_has_no_visible_damage =
572 !root_surface->damage_tracker()->current_damage_rect().Intersects(
573 root_surface->content_rect());
574 bool root_surface_has_contributing_layers =
575 !root_surface->layer_list().empty();
576 if (root_surface_has_contributing_layers &&
577 root_surface_has_no_visible_damage) {
578 TRACE_EVENT0("cc",
579 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
580 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45581 // A copy request should cause damage, so we should not have any copy
582 // requests in this case.
583 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07584 DCHECK(!output_surface_->capabilities()
585 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20586 return true;
587 }
588
[email protected]c1bb5af2013-03-13 19:06:27589 TRACE_EVENT1("cc",
590 "LayerTreeHostImpl::CalculateRenderPasses",
591 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06592 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14593
[email protected]c1bb5af2013-03-13 19:06:27594 // Create the render passes in dependency order.
595 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06596 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27597 --surface_index) {
598 LayerImpl* render_surface_layer =
599 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45600 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
601
602 bool should_draw_into_render_pass =
603 render_surface_layer->parent() == NULL ||
604 render_surface->contributes_to_drawn_surface() ||
605 render_surface_layer->HasCopyRequest();
606 if (should_draw_into_render_pass)
607 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27608 }
[email protected]94f206c12012-08-25 00:09:14609
[email protected]c1bb5af2013-03-13 19:06:27610 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52611 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27612 base::debug::TraceLog::GetInstance() &&
613 base::debug::TraceLog::GetInstance()->IsEnabled();
614 OcclusionTrackerImpl occlusion_tracker(
615 active_tree_->root_layer()->render_surface()->content_rect(),
616 record_metrics_for_frame);
617 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52618 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14619
[email protected]846f455b2013-03-18 19:07:41620 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27621 occlusion_tracker.set_occluding_screen_space_rects_container(
622 &frame->occluding_screen_space_rects);
623 }
[email protected]846f455b2013-03-18 19:07:41624 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27625 occlusion_tracker.set_non_occluding_screen_space_rects_container(
626 &frame->non_occluding_screen_space_rects);
627 }
[email protected]94f206c12012-08-25 00:09:14628
[email protected]c1bb5af2013-03-13 19:06:27629 // Add quads to the Render passes in FrontToBack order to allow for testing
630 // occlusion and performing culling during the tree walk.
631 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28632 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27633 RenderSurfaceImpl,
634 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14635
[email protected]c1bb5af2013-03-13 19:06:27636 // Typically when we are missing a texture and use a checkerboard quad, we
637 // still draw the frame. However when the layer being checkerboarded is moving
638 // due to an impl-animation, we drop the frame to avoid flashing due to the
639 // texture suddenly appearing in the future.
640 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25641 // When we have a copy request for a layer, we need to draw no matter
642 // what, as the layer may disappear after this frame.
643 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14644
[email protected]372bad5f2013-03-21 16:38:43645 int layers_drawn = 0;
646
[email protected]ffbb2212013-06-02 23:47:59647 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
648
[email protected]c1bb5af2013-03-13 19:06:27649 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09650 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27651 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09652 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27653 it != end;
654 ++it) {
655 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09656 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27657 RenderPass* target_render_pass =
658 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14659
[email protected]0e5f7142013-05-24 06:45:36660 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23661 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14662
[email protected]30fe19ff2013-07-04 00:54:45663 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30664
[email protected]18a70192013-04-26 16:18:25665 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36666 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25667 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11668 it->TakeCopyRequestsAndTransformToTarget(
669 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25670 }
[email protected]30fe19ff2013-07-04 00:54:45671 } else if (it.represents_contributing_render_surface() &&
672 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27673 RenderPass::Id contributing_render_pass_id =
674 it->render_surface()->RenderPassId();
675 RenderPass* contributing_render_pass =
676 frame->render_passes_by_id[contributing_render_pass_id];
677 AppendQuadsForRenderSurfaceLayer(target_render_pass,
678 *it,
679 contributing_render_pass,
680 occlusion_tracker,
681 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09682 } else if (it.represents_itself() &&
683 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27684 bool has_occlusion_from_outside_target_surface;
685 bool impl_draw_transform_is_unknown = false;
686 if (occlusion_tracker.Occluded(
687 it->render_target(),
688 it->visible_content_rect(),
689 it->draw_transform(),
690 impl_draw_transform_is_unknown,
691 it->is_clipped(),
692 it->clip_rect(),
693 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09694 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27695 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59696 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27697 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59698
[email protected]c1bb5af2013-03-13 19:06:27699 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57700
[email protected]c1bb5af2013-03-13 19:06:27701 if (it->HasContributingDelegatedRenderPasses()) {
702 RenderPass::Id contributing_render_pass_id =
703 it->FirstContributingRenderPassId();
704 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
705 frame->render_passes_by_id.end()) {
706 RenderPass* render_pass =
707 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14708
[email protected]c1bb5af2013-03-13 19:06:27709 AppendQuadsData append_quads_data(render_pass->id);
710 AppendQuadsForLayer(render_pass,
711 *it,
712 occlusion_tracker,
713 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57714
[email protected]c1bb5af2013-03-13 19:06:27715 contributing_render_pass_id =
716 it->NextContributingRenderPassId(contributing_render_pass_id);
717 }
[email protected]94f206c12012-08-25 00:09:14718 }
719
[email protected]c1bb5af2013-03-13 19:06:27720 AppendQuadsForLayer(target_render_pass,
721 *it,
722 occlusion_tracker,
723 &append_quads_data);
724 }
[email protected]89228202012-08-29 03:20:30725
[email protected]372bad5f2013-03-21 16:38:43726 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14727 }
728
[email protected]e1e768f2013-03-26 08:48:09729 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27730 target_render_pass->has_occlusion_from_outside_target_surface = true;
731
[email protected]e1e768f2013-03-26 08:48:09732 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43733 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09734 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27735 bool layer_has_animating_transform =
736 it->screen_space_transform_is_animating() ||
737 it->draw_transform_is_animating();
738 if (layer_has_animating_transform)
739 draw_frame = false;
740 }
741
[email protected]e1e768f2013-03-26 08:48:09742 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27743 frame->contains_incomplete_tile = true;
744
745 occlusion_tracker.LeaveLayer(it);
746 }
747
[email protected]6133cc232013-07-30 18:47:07748 if (have_copy_request ||
749 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25750 draw_frame = true;
751
[email protected]372bad5f2013-03-21 16:38:43752 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
753
[email protected]1d993172012-10-18 18:15:04754#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27755 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
756 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
757 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
758 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
759 != frame->render_passes_by_id.end());
760 }
[email protected]94f206c12012-08-25 00:09:14761#endif
[email protected]c1bb5af2013-03-13 19:06:27762 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14763
[email protected]c1bb5af2013-03-13 19:06:27764 if (!active_tree_->has_transparent_background()) {
765 frame->render_passes.back()->has_transparent_background = false;
766 AppendQuadsToFillScreen(frame->render_passes.back(),
767 active_tree_->root_layer(),
768 active_tree_->background_color(),
769 occlusion_tracker);
770 }
[email protected]94f206c12012-08-25 00:09:14771
[email protected]c1bb5af2013-03-13 19:06:27772 if (draw_frame)
773 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25774 else
775 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14776
[email protected]c1bb5af2013-03-13 19:06:27777 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37778 if (!output_surface_->ForcedDrawToSoftwareDevice())
779 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]d15d9622013-06-21 02:49:15780 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
781 frame);
[email protected]94f206c12012-08-25 00:09:14782
[email protected]30fe19ff2013-07-04 00:54:45783 // Any copy requests left in the tree are not going to get serviced, and
784 // should be aborted.
785 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
786 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
787 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
788 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
789 }
790 for (size_t i = 0; i < requests_to_abort.size(); ++i)
791 requests_to_abort[i]->SendEmptyResult();
792
[email protected]e0341352013-04-06 05:01:20793 // If we're making a frame to draw, it better have at least one render pass.
794 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27795 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14796}
797
[email protected]200a9c062013-05-20 04:34:37798void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
799 if (input_handler_client_)
800 input_handler_client_->MainThreadHasStoppedFlinging();
801}
802
[email protected]3d9f7432013-04-06 00:35:18803void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
804 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15805 DCHECK(proxy_->IsImplThread());
806
[email protected]3d9f7432013-04-06 00:35:18807 bool enabled = should_background_tick &&
808 !animation_registrar_->active_animation_controllers().empty();
809
[email protected]c1bb5af2013-03-13 19:06:27810 // Lazily create the time_source adapter so that we can vary the interval for
811 // testing.
812 if (!time_source_client_adapter_) {
813 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
814 this,
[email protected]810d40b72013-06-20 18:26:15815 DelayBasedTimeSource::Create(
816 LowFrequencyAnimationInterval(),
817 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
818 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27819 }
[email protected]94f206c12012-08-25 00:09:14820
[email protected]c1bb5af2013-03-13 19:06:27821 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14822}
823
[email protected]b9d4a362013-04-23 05:36:27824void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02825 viewport_damage_rect_.Union(damage_rect);
826}
827
[email protected]c1bb5af2013-03-13 19:06:27828static inline RenderPass* FindRenderPassById(
829 RenderPass::Id render_pass_id,
830 const LayerTreeHostImpl::FrameData& frame) {
831 RenderPassIdHashMap::const_iterator it =
832 frame.render_passes_by_id.find(render_pass_id);
833 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14834}
835
[email protected]c1bb5af2013-03-13 19:06:27836static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
837 LayerTreeHostImpl::FrameData* frame) {
838 RenderPass* remove_render_pass =
839 FindRenderPassById(remove_render_pass_id, *frame);
840 // The pass was already removed by another quad - probably the original, and
841 // we are the replica.
842 if (!remove_render_pass)
843 return;
844 RenderPassList& render_passes = frame->render_passes;
845 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
846 render_passes.end(),
847 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14848
[email protected]c1bb5af2013-03-13 19:06:27849 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14850
[email protected]c1bb5af2013-03-13 19:06:27851 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
852 frame->render_passes.erase(to_remove);
853 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14854
[email protected]c1bb5af2013-03-13 19:06:27855 // Now follow up for all RenderPass quads and remove their RenderPasses
856 // recursively.
857 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29858 QuadList::ConstBackToFrontIterator quad_list_iterator =
859 quad_list.BackToFrontBegin();
860 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27861 ++quad_list_iterator) {
862 DrawQuad* current_quad = (*quad_list_iterator);
863 if (current_quad->material != DrawQuad::RENDER_PASS)
864 continue;
[email protected]94f206c12012-08-25 00:09:14865
[email protected]c1bb5af2013-03-13 19:06:27866 RenderPass::Id next_remove_render_pass_id =
867 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
868 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
869 }
[email protected]94f206c12012-08-25 00:09:14870}
871
[email protected]c1bb5af2013-03-13 19:06:27872bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
873 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
874 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24875 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27876 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
877 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06878 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27879 if (quad_has_damage) {
880 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
881 return false;
882 } else if (!quad_has_cached_resource) {
883 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
884 return false;
885 }
886 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
887 return true;
[email protected]94f206c12012-08-25 00:09:14888}
889
[email protected]c1bb5af2013-03-13 19:06:27890bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
891 const RenderPassDrawQuad& quad, const FrameData& frame) const {
892 const RenderPass* render_pass =
893 FindRenderPassById(quad.render_pass_id, frame);
894 if (!render_pass)
895 return false;
[email protected]94f206c12012-08-25 00:09:14896
[email protected]c1bb5af2013-03-13 19:06:27897 // If any quad or RenderPass draws into this RenderPass, then keep it.
898 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29899 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
900 quad_list.BackToFrontBegin();
901 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27902 ++quad_list_iterator) {
903 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14904
[email protected]c1bb5af2013-03-13 19:06:27905 if (current_quad->material != DrawQuad::RENDER_PASS)
906 return false;
[email protected]94f206c12012-08-25 00:09:14907
[email protected]c1bb5af2013-03-13 19:06:27908 const RenderPass* contributing_pass = FindRenderPassById(
909 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
910 if (contributing_pass)
911 return false;
912 }
913 return true;
[email protected]94f206c12012-08-25 00:09:14914}
915
916// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27917template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
918 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
919 CullRenderPassesWithCachedTextures culler, FrameData* frame);
920template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
921 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
922 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14923
924// static
[email protected]c1bb5af2013-03-13 19:06:27925template <typename RenderPassCuller>
926void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
927 FrameData* frame) {
928 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
929 it != culler.RenderPassListEnd(frame->render_passes);
930 it = culler.RenderPassListNext(it)) {
931 const RenderPass* current_pass = frame->render_passes[it];
932 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29933 QuadList::ConstBackToFrontIterator quad_list_iterator =
934 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14935
[email protected]ed511b8d2013-03-25 03:29:29936 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27937 ++quad_list_iterator) {
938 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14939
[email protected]c1bb5af2013-03-13 19:06:27940 if (current_quad->material != DrawQuad::RENDER_PASS)
941 continue;
[email protected]94f206c12012-08-25 00:09:14942
[email protected]c1bb5af2013-03-13 19:06:27943 const RenderPassDrawQuad* render_pass_quad =
944 RenderPassDrawQuad::MaterialCast(current_quad);
945 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
946 continue;
[email protected]94f206c12012-08-25 00:09:14947
[email protected]c1bb5af2013-03-13 19:06:27948 // We are changing the vector in the middle of iteration. Because we
949 // delete render passes that draw into the current pass, we are
950 // guaranteed that any data from the iterator to the end will not
951 // change. So, capture the iterator position from the end of the
952 // list, and restore it after the change.
953 size_t position_from_end = frame->render_passes.size() - it;
954 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
955 it = frame->render_passes.size() - position_from_end;
956 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14957 }
[email protected]c1bb5af2013-03-13 19:06:27958 }
[email protected]94f206c12012-08-25 00:09:14959}
960
[email protected]e0341352013-04-06 05:01:20961bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
962 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:47963 TRACE_EVENT1("cc",
964 "LayerTreeHostImpl::PrepareToDraw",
965 "SourceFrameNumber",
966 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:14967
[email protected]8bb6cfa2013-07-23 00:11:19968 if (need_to_update_visible_tiles_before_draw_) {
[email protected]39643fb2013-07-09 17:28:19969 DCHECK(tile_manager_);
[email protected]8bb6cfa2013-07-23 00:11:19970 if (tile_manager_->UpdateVisibleTiles())
971 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:19972 }
973
[email protected]7d19dc342013-05-02 22:02:04974 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:27975
[email protected]c1bb5af2013-03-13 19:06:27976 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
977 frame->render_passes.clear();
978 frame->render_passes_by_id.clear();
979 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:20980 frame->contains_incomplete_tile = false;
981 frame->has_no_damage = false;
982
983 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:02984 device_viewport_damage_rect.Union(viewport_damage_rect_);
985 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:20986
987 active_tree_->root_layer()->render_surface()->damage_tracker()->
988 AddDamageNextUpdate(device_viewport_damage_rect);
989 }
[email protected]94f206c12012-08-25 00:09:14990
[email protected]6133cc232013-07-30 18:47:07991 if (!CalculateRenderPasses(frame)) {
992 DCHECK(!output_surface_->capabilities()
993 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:27994 return false;
[email protected]6133cc232013-07-30 18:47:07995 }
[email protected]94f206c12012-08-25 00:09:14996
[email protected]c1bb5af2013-03-13 19:06:27997 // If we return true, then we expect DrawLayers() to be called before this
998 // function is called again.
999 return true;
[email protected]94f206c12012-08-25 00:09:141000}
1001
[email protected]0309ba5e02013-06-26 04:11:081002void LayerTreeHostImpl::EvictTexturesForTesting() {
1003 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1004}
1005
[email protected]c1bb5af2013-03-13 19:06:271006void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1007 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121008
[email protected]c1bb5af2013-03-13 19:06:271009 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:351010 visible_ ? policy.bytes_limit_when_visible
1011 : policy.bytes_limit_when_not_visible,
1012 ManagedMemoryPolicy::PriorityCutoffToValue(
1013 visible_ ? policy.priority_cutoff_when_visible
1014 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:271015 if (evicted_resources) {
1016 active_tree_->SetContentsTexturesPurged();
1017 if (pending_tree_)
1018 pending_tree_->SetContentsTexturesPurged();
1019 client_->SetNeedsCommitOnImplThread();
1020 client_->OnCanDrawStateChanged(CanDraw());
1021 client_->RenewTreePriority();
1022 }
1023 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431024
[email protected]8be1a9bf2013-05-01 03:45:191025 UpdateTileManagerMemoryPolicy(policy);
1026}
1027
1028void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1029 const ManagedMemoryPolicy& policy) {
1030 if (!tile_manager_)
1031 return;
1032
1033 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
1034 new_state.memory_limit_in_bytes = visible_ ?
1035 policy.bytes_limit_when_visible :
1036 policy.bytes_limit_when_not_visible;
1037 // TODO(reveman): We should avoid keeping around unused resources if
1038 // possible. crbug.com/224475
1039 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
1040 (static_cast<int64>(new_state.memory_limit_in_bytes) *
1041 settings_.max_unused_resource_memory_percentage) / 100);
1042 new_state.memory_limit_policy =
1043 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1044 visible_ ?
1045 policy.priority_cutoff_when_visible :
1046 policy.priority_cutoff_when_not_visible);
[email protected]3f2ff112013-08-03 02:41:071047 new_state.num_resources_limit = policy.num_resources_limit;
[email protected]8be1a9bf2013-05-01 03:45:191048 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:261049 manage_tiles_needed_ = true;
[email protected]94f206c12012-08-25 00:09:141050}
1051
[email protected]c1bb5af2013-03-13 19:06:271052bool LayerTreeHostImpl::HasImplThread() const {
1053 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:441054}
1055
[email protected]86126792013-03-16 20:07:541056void LayerTreeHostImpl::DidInitializeVisibleTile() {
1057 // TODO(reveman): Determine tiles that changed and only damage
1058 // what's necessary.
1059 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:271060 if (client_)
[email protected]86126792013-03-16 20:07:541061 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471062}
1063
[email protected]94bf75c2013-06-12 13:20:041064void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]39643fb2013-07-09 17:28:191065 if (pending_tree_) {
[email protected]8bb6cfa2013-07-23 00:11:191066 need_to_update_visible_tiles_before_draw_ = true;
[email protected]94bf75c2013-06-12 13:20:041067 ActivatePendingTree();
[email protected]39643fb2013-07-09 17:28:191068 }
[email protected]94bf75c2013-06-12 13:20:041069}
1070
[email protected]c1bb5af2013-03-13 19:06:271071bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:521072 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:521073}
1074
[email protected]3f2ff112013-08-03 02:41:071075void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1076 SetManagedMemoryPolicy(policy, zero_budget_);
1077}
1078
1079void LayerTreeHostImpl::SetDiscardBackBufferWhenNotVisible(bool discard) {
1080 DCHECK(renderer_);
1081 renderer_->SetDiscardBackBufferWhenNotVisible(discard);
[email protected]fd32d122013-06-29 13:11:041082}
1083
[email protected]5f07afc2013-07-19 05:17:181084void LayerTreeHostImpl::SetTreeActivationCallback(
1085 const base::Closure& callback) {
1086 DCHECK(proxy_->IsImplThread());
1087 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261088 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181089}
1090
[email protected]c1bb5af2013-03-13 19:06:271091void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071092 const ManagedMemoryPolicy& policy, bool zero_budget) {
1093 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271094 return;
[email protected]61de5812012-11-08 07:03:441095
[email protected]3f2ff112013-08-03 02:41:071096 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1097
1098 cached_managed_memory_policy_ = policy;
1099 zero_budget_ = zero_budget;
1100 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1101
1102 if (old_policy == actual_policy)
1103 return;
1104
1105 if (!proxy_->HasImplThread()) {
1106 // In single-thread mode, this can be called on the main thread by
1107 // GLRenderer::OnMemoryAllocationChanged.
1108 DebugScopedSetImplThread impl_thread(proxy_);
1109 EnforceManagedMemoryPolicy(actual_policy);
1110 } else {
1111 DCHECK(proxy_->IsImplThread());
1112 EnforceManagedMemoryPolicy(actual_policy);
1113 }
[email protected]50644642013-06-20 13:58:551114
[email protected]d7626ffd2013-03-29 00:17:421115 // If there is already enough memory to draw everything imaginable and the
1116 // new memory limit does not change this, then do not re-commit. Don't bother
1117 // skipping commits if this is not visible (commits don't happen when not
1118 // visible, there will almost always be a commit when this becomes visible).
1119 bool needs_commit = true;
1120 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071121 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1122 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1123 actual_policy.priority_cutoff_when_visible ==
1124 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421125 needs_commit = false;
1126 }
1127
[email protected]d7626ffd2013-03-29 00:17:421128 if (needs_commit)
1129 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141130}
1131
[email protected]f224cc92013-06-06 23:23:321132void LayerTreeHostImpl::SetExternalDrawConstraints(
1133 const gfx::Transform& transform,
1134 gfx::Rect viewport) {
1135 external_transform_ = transform;
1136 external_viewport_ = viewport;
1137}
1138
[email protected]c32a1962013-07-30 19:41:171139void LayerTreeHostImpl::SetExternalStencilTest(bool enabled) {
1140 external_stencil_test_enabled_ = enabled;
1141}
1142
[email protected]1cd9f5552013-04-26 04:22:031143void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1144 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1145}
1146
[email protected]df3c24c92013-06-19 03:54:351147void LayerTreeHostImpl::BeginFrame(const BeginFrameArgs& args) {
1148 client_->BeginFrameOnImplThread(args);
[email protected]7ed47512013-03-26 22:28:451149}
1150
[email protected]36e5ff12013-06-11 12:19:291151void LayerTreeHostImpl::OnSwapBuffersComplete(
1152 const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271153 // TODO(piman): We may need to do some validation on this ack before
1154 // processing it.
[email protected]36e5ff12013-06-11 12:19:291155 if (ack && renderer_)
1156 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]bb1e2822013-04-17 22:06:011157
[email protected]36e5ff12013-06-11 12:19:291158 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]a46f32932012-12-07 21:43:161159}
1160
[email protected]c1bb5af2013-03-13 19:06:271161void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1162 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291163}
1164
[email protected]c1bb5af2013-03-13 19:06:271165CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1166 CompositorFrameMetadata metadata;
1167 metadata.device_scale_factor = device_scale_factor_;
1168 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1169 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1170 metadata.root_layer_size = active_tree_->ScrollableSize();
1171 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1172 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501173 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271174 if (top_controls_manager_) {
1175 metadata.location_bar_offset =
1176 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1177 metadata.location_bar_content_translation =
1178 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011179 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271180 }
[email protected]bf189f62012-12-18 03:42:111181
[email protected]c1bb5af2013-03-13 19:06:271182 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111183 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271184
[email protected]ffb2720f2013-03-15 19:18:371185 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271186
1187 return metadata;
[email protected]bf189f62012-12-18 03:42:111188}
1189
[email protected]73673592013-04-03 22:14:321190bool LayerTreeHostImpl::AllowPartialSwap() const {
1191 // We don't track damage on the HUD layer (it interacts with damage tracking
1192 // visualizations), so disable partial swaps to make the HUD layer display
1193 // properly.
1194 return !debug_state_.ShowHudRects();
1195}
1196
[email protected]c32a1962013-07-30 19:41:171197bool LayerTreeHostImpl::ExternalStencilTestEnabled() const {
1198 return external_stencil_test_enabled_;
1199}
1200
[email protected]6f50b8fa2013-07-01 19:51:491201static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1202 layer->DidBeginTracing();
1203}
[email protected]37349bc2013-06-04 01:31:521204
[email protected]f0c2a242013-03-15 19:34:521205void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1206 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:271207 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
1208 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201209
[email protected]3519b872013-07-30 07:17:501210 if (frame->has_no_damage) {
1211 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071212 DCHECK(!output_surface_->capabilities()
1213 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201214 return;
[email protected]3519b872013-07-30 07:17:501215 }
[email protected]e0341352013-04-06 05:01:201216
[email protected]c1bb5af2013-03-13 19:06:271217 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141218
[email protected]9e3594522013-03-18 00:57:361219 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:141220
[email protected]372bad5f2013-03-21 16:38:431221 rendering_stats_instrumentation_->SetScreenFrameCount(
1222 fps_counter_->current_frame_number());
1223 rendering_stats_instrumentation_->SetDroppedFrameCount(
1224 fps_counter_->dropped_frame_count());
1225
[email protected]c1bb5af2013-03-13 19:06:271226 if (tile_manager_) {
1227 memory_history_->SaveEntry(
1228 tile_manager_->memory_stats_from_last_assign());
1229 }
[email protected]1191d9d2013-02-02 06:00:331230
[email protected]846f455b2013-03-18 19:07:411231 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021232 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271233 active_tree_->root_layer(),
1234 *frame->render_surface_layer_list,
1235 frame->occluding_screen_space_rects,
1236 frame->non_occluding_screen_space_rects,
1237 debug_state_);
1238 }
[email protected]94f206c12012-08-25 00:09:141239
[email protected]a848c102013-03-26 08:59:091240 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1241 const RenderingStats& stats =
1242 rendering_stats_instrumentation_->GetRenderingStats();
1243 paint_time_counter_->SavePaintTime(stats.total_paint_time);
1244 }
1245
[email protected]37349bc2013-06-04 01:31:521246 bool is_new_trace;
1247 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1248 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491249 if (pending_tree_) {
1250 LayerTreeHostCommon::CallFunctionForSubtree(
1251 pending_tree_->root_layer(),
1252 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1253 }
1254 LayerTreeHostCommon::CallFunctionForSubtree(
1255 active_tree_->root_layer(),
1256 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521257 }
1258
[email protected]9197dbcb2013-05-15 20:28:511259 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1260 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this,
1261 TracedValue::FromValue(AsValue().release()));
[email protected]131a0c22013-02-12 18:31:081262
[email protected]c1bb5af2013-03-13 19:06:271263 // Because the contents of the HUD depend on everything else in the frame, the
1264 // contents of its texture are updated as the last thing before the frame is
1265 // drawn.
1266 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541267 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141268
[email protected]b09c1942013-05-10 00:06:371269 if (output_surface_->ForcedDrawToSoftwareDevice()) {
1270 scoped_ptr<SoftwareRenderer> temp_software_renderer =
1271 SoftwareRenderer::Create(this, output_surface_.get(), NULL);
1272 temp_software_renderer->DrawFrame(&frame->render_passes);
1273 } else {
1274 renderer_->DrawFrame(&frame->render_passes);
1275 }
[email protected]c1bb5af2013-03-13 19:06:271276 // The render passes should be consumed by the renderer.
1277 DCHECK(frame->render_passes.empty());
1278 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141279
[email protected]c1bb5af2013-03-13 19:06:271280 // The next frame should start by assuming nothing has changed, and changes
1281 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541282 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271283 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1284 DidDrawDamagedArea();
1285 }
1286 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]94f206c12012-08-25 00:09:141287}
1288
[email protected]c1bb5af2013-03-13 19:06:271289void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1290 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1291 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521292
[email protected]c1bb5af2013-03-13 19:06:271293 // Once all layers have been drawn, pending texture uploads should no
1294 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151295 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141296}
1297
[email protected]c1bb5af2013-03-13 19:06:271298void LayerTreeHostImpl::FinishAllRendering() {
1299 if (renderer_)
1300 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141301}
1302
[email protected]c1bb5af2013-03-13 19:06:271303bool LayerTreeHostImpl::IsContextLost() {
1304 DCHECK(proxy_->IsImplThread());
1305 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141306}
1307
[email protected]c1bb5af2013-03-13 19:06:271308const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1309 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141310}
1311
[email protected]e0341352013-04-06 05:01:201312bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1313 if (frame.has_no_damage)
1314 return false;
[email protected]36e5ff12013-06-11 12:19:291315 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161316 active_tree_->ClearLatencyInfo();
1317 return true;
[email protected]94f206c12012-08-25 00:09:141318}
1319
[email protected]c8cbae72013-05-23 10:45:031320void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451321 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031322 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451323}
1324
[email protected]94c40e6292013-05-24 22:01:501325float LayerTreeHostImpl::DeviceScaleFactor() const {
1326 return device_scale_factor_;
1327}
1328
[email protected]ffb2720f2013-03-15 19:18:371329gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
[email protected]4f0f43a2013-07-31 17:44:441330 // The container layer bounds should be used if non-overlay scrollbars may
1331 // exist since it adjusts for them.
1332 LayerImpl* container_layer = active_tree_->RootContainerLayer();
1333 if (!Settings().solid_color_scrollbars && container_layer) {
1334 DCHECK(!top_controls_manager_);
1335 DCHECK_EQ(0, overdraw_bottom_height_);
1336 return container_layer->bounds();
1337 }
1338
[email protected]ffb2720f2013-03-15 19:18:371339 gfx::SizeF dip_size =
[email protected]f224cc92013-06-06 23:23:321340 gfx::ScaleSize(device_viewport_size(), 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371341
[email protected]ed511b8d2013-03-25 03:29:291342 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371343 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401344 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291345 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371346}
1347
[email protected]c1bb5af2013-03-13 19:06:271348const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1349 return settings();
[email protected]493067512012-09-19 23:34:101350}
1351
[email protected]c1bb5af2013-03-13 19:06:271352void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011353 // TODO(jamesr): The renderer_ check is needed to make some of the
1354 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1355 // important) in production. We should adjust the test to not need this.
1356 if (renderer_)
1357 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141358}
1359
[email protected]c1bb5af2013-03-13 19:06:271360void LayerTreeHostImpl::Readback(void* pixels,
1361 gfx::Rect rect_in_device_viewport) {
1362 DCHECK(renderer_);
1363 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141364}
1365
[email protected]59adb112013-04-09 04:48:441366bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341367 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011368}
1369
[email protected]c1bb5af2013-03-13 19:06:271370LayerImpl* LayerTreeHostImpl::RootLayer() const {
1371 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081372}
1373
[email protected]c1bb5af2013-03-13 19:06:271374LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1375 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081376}
1377
[email protected]c1bb5af2013-03-13 19:06:271378LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1379 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081380}
1381
[email protected]94f206c12012-08-25 00:09:141382// Content layers can be either directly scrollable or contained in an outer
1383// scrolling layer which applies the scroll transform. Given a content layer,
1384// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271385static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1386 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141387 return 0;
[email protected]c1bb5af2013-03-13 19:06:271388
1389 if (layer_impl->scrollable())
1390 return layer_impl;
1391
1392 if (layer_impl->DrawsContent() &&
1393 layer_impl->parent() &&
1394 layer_impl->parent()->scrollable())
1395 return layer_impl->parent();
1396
1397 return 0;
[email protected]94f206c12012-08-25 00:09:141398}
1399
[email protected]c1bb5af2013-03-13 19:06:271400void LayerTreeHostImpl::CreatePendingTree() {
1401 CHECK(!pending_tree_);
1402 if (recycle_tree_)
1403 recycle_tree_.swap(pending_tree_);
1404 else
1405 pending_tree_ = LayerTreeImpl::create(this);
1406 client_->OnCanDrawStateChanged(CanDraw());
1407 client_->OnHasPendingTreeStateChanged(pending_tree_);
1408 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1409 TRACE_EVENT_ASYNC_STEP0("cc",
1410 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271411}
1412
[email protected]8bb6cfa2013-07-23 00:11:191413void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]c1bb5af2013-03-13 19:06:271414 DCHECK(!client_->IsInsideDraw()) <<
[email protected]8bb6cfa2013-07-23 00:11:191415 "Updating visible tiles within a draw may trigger "
[email protected]c1bb5af2013-03-13 19:06:271416 "spurious redraws.";
[email protected]8bb6cfa2013-07-23 00:11:191417 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1418 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191419
[email protected]8bb6cfa2013-07-23 00:11:191420 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481421}
1422
[email protected]94bf75c2013-06-12 13:20:041423void LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1424 DCHECK(pending_tree_);
[email protected]fbe89f72013-05-21 07:24:241425 CHECK(settings_.impl_side_painting);
[email protected]2ae038b2013-01-28 12:52:091426
[email protected]94bf75c2013-06-12 13:20:041427 if (!pending_tree_)
1428 return;
1429
1430 // The tile manager is usually responsible for notifying activation.
1431 // If there is no tile manager, then we need to manually activate.
1432 if (!tile_manager_ || tile_manager_->AreTilesRequiredForActivationReady()) {
1433 ActivatePendingTree();
1434 return;
1435 }
[email protected]615c78a2013-01-24 23:44:161436
[email protected]b67a44e2013-07-12 19:15:101437 // Manage tiles in case state affecting tile priority has changed.
1438 ManageTiles();
1439
[email protected]f6742f52013-05-08 23:52:221440 TRACE_EVENT_ASYNC_STEP1(
[email protected]fcb846d2013-05-22 01:42:361441 "cc",
1442 "PendingTree", pending_tree_.get(), "activate",
1443 "state", TracedValue::FromValue(ActivationStateAsValue().release()));
[email protected]2e7ca422012-12-20 02:57:271444}
1445
[email protected]c1bb5af2013-03-13 19:06:271446void LayerTreeHostImpl::ActivatePendingTree() {
1447 CHECK(pending_tree_);
1448 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351449
[email protected]1960a712013-04-30 17:06:471450 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271451 active_tree_->PushPersistedState(pending_tree_.get());
1452 if (pending_tree_->needs_full_tree_sync()) {
1453 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331454 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271455 active_tree_->DetachLayerTree(),
1456 active_tree_.get()));
1457 }
[email protected]b5651c22013-03-14 15:06:331458 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271459 active_tree_->root_layer());
1460 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511461
[email protected]c9280762013-08-01 06:28:571462 // Process any requests in the UI resource queue. The request queue is given
1463 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1464 // the swap.
1465 pending_tree_->ProcessUIResourceRequestQueue();
1466
[email protected]c1bb5af2013-03-13 19:06:271467 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511468
[email protected]c1bb5af2013-03-13 19:06:271469 // Now that we've synced everything from the pending tree to the active
1470 // tree, rename the pending tree the recycle tree so we can reuse it on the
1471 // next sync.
1472 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511473
[email protected]1960a712013-04-30 17:06:471474 active_tree_->SetRootLayerScrollOffsetDelegate(
1475 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271476 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221477
[email protected]c1bb5af2013-03-13 19:06:271478 // Reduce wasted memory now that unlinked resources are guaranteed not
1479 // to be used.
1480 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411481
[email protected]c1bb5af2013-03-13 19:06:271482 client_->OnCanDrawStateChanged(CanDraw());
1483 client_->OnHasPendingTreeStateChanged(pending_tree_);
1484 client_->SetNeedsRedrawOnImplThread();
1485 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241486
[email protected]a848c102013-03-26 08:59:091487 if (debug_state_.continuous_painting) {
1488 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431489 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091490 paint_time_counter_->SavePaintTime(
[email protected]41abb6d92013-04-26 16:38:501491 stats.total_paint_time + stats.total_record_time +
[email protected]a848c102013-03-26 08:59:091492 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271493 }
[email protected]2a61ad52013-05-13 14:01:291494
1495 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261496 if (!tree_activation_callback_.is_null())
1497 tree_activation_callback_.Run();
[email protected]2e7ca422012-12-20 02:57:271498}
1499
[email protected]c1bb5af2013-03-13 19:06:271500void LayerTreeHostImpl::SetVisible(bool visible) {
1501 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141502
[email protected]c1bb5af2013-03-13 19:06:271503 if (visible_ == visible)
1504 return;
1505 visible_ = visible;
1506 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121507 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141508
[email protected]c1bb5af2013-03-13 19:06:271509 if (!renderer_)
1510 return;
[email protected]94f206c12012-08-25 00:09:141511
[email protected]c1bb5af2013-03-13 19:06:271512 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141513}
1514
[email protected]206a3922013-05-17 06:34:121515ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071516 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1517 if (debug_state_.rasterize_only_visible_content) {
1518 actual.priority_cutoff_when_not_visible =
1519 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1520 actual.priority_cutoff_when_visible =
1521 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1522 }
[email protected]206a3922013-05-17 06:34:121523
[email protected]3f2ff112013-08-03 02:41:071524 if (zero_budget_) {
1525 actual.bytes_limit_when_visible = 0;
1526 actual.bytes_limit_when_not_visible = 0;
1527 }
1528
[email protected]206a3922013-05-17 06:34:121529 return actual;
1530}
1531
[email protected]3f2ff112013-08-03 02:41:071532size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1533 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1534}
1535
[email protected]50644642013-06-20 13:58:551536void LayerTreeHostImpl::ReleaseTreeResources() {
1537 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551538 SendReleaseResourcesRecursive(active_tree_->root_layer());
1539 if (pending_tree_ && pending_tree_->root_layer())
1540 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1541 if (recycle_tree_ && recycle_tree_->root_layer())
1542 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571543
1544 // Remove all existing maps from UIResourceId to ResourceId.
1545 ui_resource_map_.clear();
[email protected]50644642013-06-20 13:58:551546}
1547
1548void LayerTreeHostImpl::CreateAndSetRenderer(
1549 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201550 ResourceProvider* resource_provider,
1551 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551552 DCHECK(!renderer_);
1553 if (output_surface->capabilities().delegated_rendering) {
1554 renderer_ =
1555 DelegatingRenderer::Create(this, output_surface, resource_provider);
[email protected]50af3c22013-07-13 03:50:201556 } else if (output_surface->context3d() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551557 renderer_ = GLRenderer::Create(this,
1558 output_surface,
1559 resource_provider,
1560 settings_.highp_threshold_min,
1561 settings_.force_direct_layer_drawing);
1562 } else if (output_surface->software_device()) {
1563 renderer_ =
1564 SoftwareRenderer::Create(this, output_surface, resource_provider);
1565 }
1566
[email protected]84975832013-06-29 00:24:111567 if (renderer_) {
[email protected]50644642013-06-20 13:58:551568 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111569 SetFullRootLayerDamage();
1570 }
[email protected]50644642013-06-20 13:58:551571}
1572
[email protected]50af3c22013-07-13 03:50:201573void LayerTreeHostImpl::CreateAndSetTileManager(
1574 ResourceProvider* resource_provider,
1575 bool using_map_image) {
1576 DCHECK(settings_.impl_side_painting);
1577 DCHECK(resource_provider);
1578 tile_manager_ = TileManager::Create(this,
1579 resource_provider,
1580 settings_.num_raster_threads,
1581 rendering_stats_instrumentation_,
1582 using_map_image);
1583 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191584 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201585}
1586
[email protected]50644642013-06-20 13:58:551587void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071588 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551589}
1590
[email protected]c1bb5af2013-03-13 19:06:271591bool LayerTreeHostImpl::InitializeRenderer(
1592 scoped_ptr<OutputSurface> output_surface) {
1593 // Since we will create a new resource provider, we cannot continue to use
1594 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1595 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551596 ReleaseTreeResources();
[email protected]7ba3ca72013-04-11 06:37:251597 if (resource_provider_)
1598 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401599
[email protected]c1bb5af2013-03-13 19:06:271600 // Note: order is important here.
1601 renderer_.reset();
1602 tile_manager_.reset();
1603 resource_provider_.reset();
1604 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141605
[email protected]c1bb5af2013-03-13 19:06:271606 if (!output_surface->BindToClient(this))
1607 return false;
[email protected]be3181652012-09-25 13:02:131608
[email protected]50644642013-06-20 13:58:551609 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1610 output_surface.get(), settings_.highp_threshold_min);
1611 if (!resource_provider)
1612 return false;
[email protected]2b154b22013-06-07 09:03:271613
[email protected]50644642013-06-20 13:58:551614 if (output_surface->capabilities().deferred_gl_initialization)
1615 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131616
[email protected]50af3c22013-07-13 03:50:201617 bool skip_gl_renderer = false;
1618 CreateAndSetRenderer(
1619 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131620
[email protected]50644642013-06-20 13:58:551621 if (!renderer_)
1622 return false;
[email protected]6123930c2013-05-23 06:48:371623
[email protected]50644642013-06-20 13:58:551624 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201625 CreateAndSetTileManager(resource_provider.get(),
1626 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241627 }
1628
[email protected]049fc7a2013-06-18 12:32:351629 // Setup BeginFrameEmulation if it's not supported natively
1630 if (!settings_.begin_frame_scheduling_enabled) {
1631 const base::TimeDelta display_refresh_interval =
1632 base::TimeDelta::FromMicroseconds(
1633 base::Time::kMicrosecondsPerSecond /
1634 settings_.refresh_rate);
1635
1636 output_surface->InitializeBeginFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151637 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351638 settings_.throttle_frame_production,
1639 display_refresh_interval);
1640 }
1641
1642 int max_frames_pending =
1643 output_surface->capabilities().max_frames_pending;
1644 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351645 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351646 output_surface->SetMaxFramesPending(max_frames_pending);
1647
[email protected]50644642013-06-20 13:58:551648 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271649 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141650
[email protected]c1bb5af2013-03-13 19:06:271651 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211652
[email protected]c1bb5af2013-03-13 19:06:271653 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1654 // to be initialized to get max texture size.
1655 active_tree_->set_needs_update_draw_properties();
1656 if (pending_tree_)
1657 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161658
[email protected]c1bb5af2013-03-13 19:06:271659 return true;
[email protected]94f206c12012-08-25 00:09:141660}
1661
[email protected]2b154b22013-06-07 09:03:271662bool LayerTreeHostImpl::DeferredInitialize(
1663 scoped_refptr<ContextProvider> offscreen_context_provider) {
1664 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551665 DCHECK(settings_.impl_side_painting);
1666 DCHECK(settings_.solid_color_scrollbars);
[email protected]2b154b22013-06-07 09:03:271667 DCHECK(output_surface_->context3d());
1668
[email protected]50644642013-06-20 13:58:551669 ReleaseTreeResources();
1670 renderer_.reset();
[email protected]50af3c22013-07-13 03:50:201671 resource_provider_->InitializeGL();
1672 bool skip_gl_renderer = false;
1673 CreateAndSetRenderer(
1674 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
[email protected]2b154b22013-06-07 09:03:271675
[email protected]50644642013-06-20 13:58:551676 bool success = !!renderer_.get();
[email protected]2b154b22013-06-07 09:03:271677 client_->DidTryInitializeRendererOnImplThread(success,
1678 offscreen_context_provider);
[email protected]50644642013-06-20 13:58:551679 if (success) {
1680 EnforceZeroBudget(false);
1681 client_->SetNeedsCommitOnImplThread();
1682 }
[email protected]2b154b22013-06-07 09:03:271683 return success;
1684}
1685
[email protected]50af3c22013-07-13 03:50:201686void LayerTreeHostImpl::ReleaseGL() {
1687 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1688 DCHECK(settings_.impl_side_painting);
1689 DCHECK(settings_.solid_color_scrollbars);
1690 DCHECK(output_surface_->context3d());
1691
1692 ReleaseTreeResources();
1693 renderer_.reset();
1694 tile_manager_.reset();
1695 resource_provider_->InitializeSoftware();
1696
1697 bool skip_gl_renderer = true;
1698 CreateAndSetRenderer(
1699 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1700 DCHECK(renderer_);
1701
1702 EnforceZeroBudget(true);
1703 CreateAndSetTileManager(resource_provider_.get(),
1704 GetRendererCapabilities().using_map_image);
1705 DCHECK(tile_manager_);
1706
1707 bool success = true;
1708 client_->DidTryInitializeRendererOnImplThread(
1709 success, scoped_refptr<ContextProvider>());
1710 client_->SetNeedsCommitOnImplThread();
1711}
1712
[email protected]18ce59702013-04-09 04:58:401713void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1714 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271715 return;
[email protected]94f206c12012-08-25 00:09:141716
[email protected]c1bb5af2013-03-13 19:06:271717 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1718 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271719
[email protected]c1bb5af2013-03-13 19:06:271720 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141721
[email protected]c1bb5af2013-03-13 19:06:271722 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141723
[email protected]c1bb5af2013-03-13 19:06:271724 if (renderer_)
1725 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211726
[email protected]c1bb5af2013-03-13 19:06:271727 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441728 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141729}
1730
[email protected]d9083762013-03-24 01:36:401731void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1732 if (overdraw_bottom_height == overdraw_bottom_height_)
1733 return;
1734 overdraw_bottom_height_ = overdraw_bottom_height;
1735
1736 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441737 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401738}
1739
[email protected]c1bb5af2013-03-13 19:06:271740void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1741 if (device_scale_factor == device_scale_factor_)
1742 return;
1743 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271744
[email protected]94c40e6292013-05-24 22:01:501745 if (renderer_)
1746 renderer_->ViewportChanged();
1747
[email protected]c1bb5af2013-03-13 19:06:271748 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441749 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141750}
1751
[email protected]f224cc92013-06-06 23:23:321752gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1753 if (external_viewport_.IsEmpty())
1754 return gfx::Rect(device_viewport_size_);
1755
1756 return external_viewport_;
1757}
1758
1759const gfx::Transform& LayerTreeHostImpl::DeviceTransform() const {
1760 return external_transform_;
1761}
1762
[email protected]c1bb5af2013-03-13 19:06:271763void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1764 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141765}
1766
[email protected]59adb112013-04-09 04:48:441767void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271768 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441769 active_tree_->set_needs_update_draw_properties();
1770 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141771}
1772
[email protected]c1bb5af2013-03-13 19:06:271773bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041774 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341775 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141776}
1777
[email protected]200a9c062013-05-20 04:34:371778void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1779 DCHECK(input_handler_client_ == NULL);
1780 input_handler_client_ = client;
1781}
1782
[email protected]5ff3c9782013-04-29 17:35:121783InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1784 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291785 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141786
[email protected]c1bb5af2013-03-13 19:06:271787 if (top_controls_manager_)
1788 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381789
[email protected]c1bb5af2013-03-13 19:06:271790 DCHECK(!CurrentlyScrollingLayer());
1791 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141792
[email protected]c1bb5af2013-03-13 19:06:271793 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141794 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141795
[email protected]c1bb5af2013-03-13 19:06:271796 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1797 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141798
[email protected]c1bb5af2013-03-13 19:06:271799 // First find out which layer was hit from the saved list of visible layers
1800 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391801 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271802 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521803
[email protected]c1bb5af2013-03-13 19:06:271804 // Walk up the hierarchy and look for a scrollable layer.
1805 LayerImpl* potentially_scrolling_layer_impl = 0;
1806 for (; layer_impl; layer_impl = layer_impl->parent()) {
1807 // The content layer can also block attempts to scroll outside the main
1808 // thread.
1809 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1810 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431811 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271812 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1813 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141814 }
1815
[email protected]c1bb5af2013-03-13 19:06:271816 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1817 if (!scroll_layer_impl)
1818 continue;
[email protected]94f206c12012-08-25 00:09:141819
[email protected]c1bb5af2013-03-13 19:06:271820 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141821
[email protected]c1bb5af2013-03-13 19:06:271822 // If any layer wants to divert the scroll event to the main thread, abort.
1823 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431824 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271825 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1826 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141827 }
1828
[email protected]c1bb5af2013-03-13 19:06:271829 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1830 potentially_scrolling_layer_impl = scroll_layer_impl;
1831 }
1832
1833 // When hiding top controls is enabled and the controls are hidden or
1834 // overlaying the content, force scrolls to be enabled on the root layer to
1835 // allow bringing the top controls back into view.
1836 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1837 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521838 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271839 potentially_scrolling_layer_impl = RootScrollLayer();
1840 }
1841
1842 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201843 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271844 potentially_scrolling_layer_impl);
1845 should_bubble_scrolls_ = (type != NonBubblingGesture);
1846 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431847 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271848 client_->RenewTreePriority();
1849 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
1850 return ScrollStarted;
1851 }
1852 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141853}
1854
[email protected]c1bb5af2013-03-13 19:06:271855gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1856 LayerImpl* layer_impl,
1857 float scale_from_viewport_to_screen_space,
1858 gfx::PointF viewport_point,
1859 gfx::Vector2dF viewport_delta) {
1860 // Layers with non-invertible screen space transforms should not have passed
1861 // the scroll hit test in the first place.
1862 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1863 gfx::Transform inverse_screen_space_transform(
1864 gfx::Transform::kSkipInitialization);
1865 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1866 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351867 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1868 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271869 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141870
[email protected]c1bb5af2013-03-13 19:06:271871 gfx::PointF screen_space_point =
1872 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141873
[email protected]c1bb5af2013-03-13 19:06:271874 gfx::Vector2dF screen_space_delta = viewport_delta;
1875 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1876
1877 // First project the scroll start and end points to local layer space to find
1878 // the scroll delta in layer coordinates.
1879 bool start_clipped, end_clipped;
1880 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1881 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211882 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271883 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211884 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271885 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211886 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271887 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211888 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271889
1890 // In general scroll point coordinates should not get clipped.
1891 DCHECK(!start_clipped);
1892 DCHECK(!end_clipped);
1893 if (start_clipped || end_clipped)
1894 return gfx::Vector2dF();
1895
1896 // local_start_point and local_end_point are in content space but we want to
1897 // move them to layer space for scrolling.
1898 float width_scale = 1.f / layer_impl->contents_scale_x();
1899 float height_scale = 1.f / layer_impl->contents_scale_y();
1900 local_start_point.Scale(width_scale, height_scale);
1901 local_end_point.Scale(width_scale, height_scale);
1902
1903 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:471904 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:271905 layer_impl->ScrollBy(local_end_point - local_start_point);
1906
1907 // Get the end point in the layer's content space so we can apply its
1908 // ScreenSpaceTransform.
1909 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:471910 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:271911 previous_delta;
1912 gfx::PointF actual_local_content_end_point =
1913 gfx::ScalePoint(actual_local_end_point,
1914 1.f / width_scale,
1915 1.f / height_scale);
1916
1917 // Calculate the applied scroll delta in viewport space coordinates.
1918 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211919 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271920 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211921 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271922 DCHECK(!end_clipped);
1923 if (end_clipped)
1924 return gfx::Vector2dF();
1925 gfx::PointF actual_viewport_end_point =
1926 gfx::ScalePoint(actual_screen_space_end_point,
1927 1.f / scale_from_viewport_to_screen_space);
1928 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141929}
1930
[email protected]c1bb5af2013-03-13 19:06:271931static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1932 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:471933 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271934 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:471935 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:271936}
1937
1938bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1939 gfx::Vector2dF scroll_delta) {
1940 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1941 if (!CurrentlyScrollingLayer())
1942 return false;
1943
1944 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:531945 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:291946 bool did_scroll_x = false;
1947 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:421948 bool consume_by_top_controls = top_controls_manager_ &&
1949 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061950
[email protected]c1bb5af2013-03-13 19:06:271951 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1952 layer_impl;
1953 layer_impl = layer_impl->parent()) {
1954 if (!layer_impl->scrollable())
1955 continue;
1956
[email protected]a2b5ded2013-05-20 21:32:531957 if (layer_impl == RootScrollLayer()) {
1958 // Only allow bubble scrolling when the scroll is in the direction to make
1959 // the top controls visible.
1960 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1961 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1962 UpdateMaxScrollOffset();
1963 }
1964 // Track root layer deltas for reporting overscroll.
1965 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:421966 }
1967
[email protected]c1bb5af2013-03-13 19:06:271968 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271969 // Gesture events need to be transformed from viewport coordinates to local
1970 // layer coordinates so that the scrolling contents exactly follow the
1971 // user's finger. In contrast, wheel events represent a fixed amount of
1972 // scrolling so we can just apply them directly.
1973 if (!wheel_scrolling_) {
1974 float scale_from_viewport_to_screen_space = device_scale_factor_;
1975 applied_delta =
1976 ScrollLayerWithViewportSpaceDelta(layer_impl,
1977 scale_from_viewport_to_screen_space,
1978 viewport_point, pending_delta);
1979 } else {
1980 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141981 }
[email protected]94f206c12012-08-25 00:09:141982
[email protected]c1bb5af2013-03-13 19:06:271983 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:291984 float move_threshold = 0.1f;
1985 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
1986 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
1987 did_scroll_x |= did_move_layer_x;
1988 did_scroll_y |= did_move_layer_y;
1989 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]c1bb5af2013-03-13 19:06:271990 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1991 continue;
1992 else
1993 break;
[email protected]94f206c12012-08-25 00:09:141994 }
[email protected]a2b5ded2013-05-20 21:32:531995
1996 if (layer_impl == RootScrollLayer())
1997 unused_root_delta.Subtract(applied_delta);
1998
[email protected]c1bb5af2013-03-13 19:06:271999 did_lock_scrolling_layer_ = true;
2000 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202001 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272002 break;
[email protected]94f206c12012-08-25 00:09:142003 }
[email protected]94f206c12012-08-25 00:09:142004
[email protected]c1bb5af2013-03-13 19:06:272005 // If the applied delta is within 45 degrees of the input delta, bail out to
2006 // make it easier to scroll just one layer in one direction without
2007 // affecting any of its parents.
2008 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212009 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272010 applied_delta, pending_delta) < angle_threshold) {
2011 pending_delta = gfx::Vector2d();
2012 break;
[email protected]4a23c374c2012-12-08 08:38:552013 }
[email protected]c1bb5af2013-03-13 19:06:272014
2015 // Allow further movement only on an axis perpendicular to the direction in
2016 // which the layer moved.
2017 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212018 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272019
[email protected]a2b5ded2013-05-20 21:32:532020 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272021 break;
2022 }
2023
[email protected]2bd503f2013-07-23 05:35:292024 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272025 if (did_scroll) {
2026 client_->SetNeedsCommitOnImplThread();
2027 client_->SetNeedsRedrawOnImplThread();
2028 client_->RenewTreePriority();
2029 }
[email protected]a2b5ded2013-05-20 21:32:532030
[email protected]2bd503f2013-07-23 05:35:292031 // Scrolling along an axis resets accumulated root overscroll for that axis.
2032 if (did_scroll_x)
2033 accumulated_root_overscroll_.set_x(0);
2034 if (did_scroll_y)
2035 accumulated_root_overscroll_.set_y(0);
2036
[email protected]a2b5ded2013-05-20 21:32:532037 accumulated_root_overscroll_ += unused_root_delta;
2038 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2039 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112040 DidOverscrollParams params;
2041 params.accumulated_overscroll = accumulated_root_overscroll_;
2042 params.latest_overscroll_delta = unused_root_delta;
2043 params.current_fling_velocity = current_fling_velocity_;
2044 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532045 }
2046
[email protected]c1bb5af2013-03-13 19:06:272047 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552048}
2049
[email protected]be782f52013-03-23 21:36:142050// This implements scrolling by page as described here:
2051// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2052// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492053bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2054 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142055 DCHECK(wheel_scrolling_);
2056
2057 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2058 layer_impl;
2059 layer_impl = layer_impl->parent()) {
2060 if (!layer_impl->scrollable())
2061 continue;
2062
2063 if (!layer_impl->vertical_scrollbar_layer())
2064 continue;
2065
2066 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2067
2068 // These magical values match WebKit and are designed to scroll nearly the
2069 // entire visible content height but leave a bit of overlap.
2070 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492071 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142072 page = -page;
2073
2074 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2075
2076 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2077
2078 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142079 client_->SetNeedsCommitOnImplThread();
2080 client_->SetNeedsRedrawOnImplThread();
2081 client_->RenewTreePriority();
2082 return true;
2083 }
2084
2085 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2086 }
2087
2088 return false;
2089}
2090
[email protected]1960a712013-04-30 17:06:472091void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2092 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2093 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2094 active_tree_->SetRootLayerScrollOffsetDelegate(
2095 root_layer_scroll_offset_delegate_);
2096}
2097
2098void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2099 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2100 client_->SetNeedsCommitOnImplThread();
2101}
2102
[email protected]c1bb5af2013-03-13 19:06:272103void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2104 active_tree_->ClearCurrentlyScrollingLayer();
2105 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532106 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202107 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142108}
2109
[email protected]c1bb5af2013-03-13 19:06:272110void LayerTreeHostImpl::ScrollEnd() {
2111 if (top_controls_manager_)
2112 top_controls_manager_->ScrollEnd();
2113 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052114 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142115}
2116
[email protected]5ff3c9782013-04-29 17:35:122117InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462118 if (!active_tree_->CurrentlyScrollingLayer())
2119 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212120
[email protected]75147f52013-08-02 19:11:462121 if (settings_.ignore_root_layer_flings &&
2122 active_tree_->CurrentlyScrollingLayer() ==
2123 active_tree_->RootScrollLayer()) {
2124 ClearCurrentlyScrollingLayer();
2125 return ScrollIgnored;
2126 }
2127
2128 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212129}
2130
[email protected]6e921bd2013-04-29 21:10:202131void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2132 current_fling_velocity_ = velocity;
2133}
2134
[email protected]c1bb5af2013-03-13 19:06:272135void LayerTreeHostImpl::PinchGestureBegin() {
2136 pinch_gesture_active_ = true;
2137 previous_pinch_anchor_ = gfx::Point();
2138 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:142139}
2140
[email protected]c1bb5af2013-03-13 19:06:272141void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2142 gfx::Point anchor) {
2143 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282144
[email protected]c1bb5af2013-03-13 19:06:272145 if (!RootScrollLayer())
2146 return;
[email protected]d3afa112012-12-08 06:24:282147
[email protected]c1bb5af2013-03-13 19:06:272148 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2149 // position over the course of the magnify.
2150 float page_scale_delta = active_tree_->page_scale_delta();
2151 gfx::PointF previous_scale_anchor =
2152 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2153 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2154 page_scale_delta = active_tree_->page_scale_delta();
2155 gfx::PointF new_scale_anchor =
2156 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2157 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2158
2159 previous_pinch_anchor_ = anchor;
2160
2161 move.Scale(1 / active_tree_->page_scale_factor());
2162
2163 RootScrollLayer()->ScrollBy(move);
2164
[email protected]c1bb5af2013-03-13 19:06:272165 client_->SetNeedsCommitOnImplThread();
2166 client_->SetNeedsRedrawOnImplThread();
2167 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282168}
2169
[email protected]c1bb5af2013-03-13 19:06:272170void LayerTreeHostImpl::PinchGestureEnd() {
2171 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:272172 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142173}
2174
[email protected]c1bb5af2013-03-13 19:06:272175static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2176 LayerImpl* layer_impl) {
2177 if (!layer_impl)
2178 return;
[email protected]94f206c12012-08-25 00:09:142179
[email protected]c1bb5af2013-03-13 19:06:272180 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472181 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272182 if (!scroll_delta.IsZero()) {
2183 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392184 scroll.layer_id = layer_impl->id();
2185 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272186 scroll_info->scrolls.push_back(scroll);
2187 layer_impl->SetSentScrollDelta(scroll_delta);
2188 }
[email protected]94f206c12012-08-25 00:09:142189
[email protected]c1bb5af2013-03-13 19:06:272190 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2191 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142192}
2193
[email protected]c1bb5af2013-03-13 19:06:272194scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2195 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302196
[email protected]c1bb5af2013-03-13 19:06:272197 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392198 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2199 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302200
[email protected]c1bb5af2013-03-13 19:06:272201 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302202}
2203
[email protected]c1bb5af2013-03-13 19:06:272204void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]878705be2013-04-15 22:44:022205 SetViewportDamage(gfx::Rect(device_viewport_size_));
[email protected]829ad972013-01-28 23:36:102206}
2207
[email protected]c1bb5af2013-03-13 19:06:272208void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2209 if (!page_scale_animation_ || !RootScrollLayer())
2210 return;
2211
2212 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2213 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472214 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272215
2216 active_tree_->SetPageScaleDelta(
2217 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2218 active_tree_->page_scale_factor());
2219 gfx::Vector2dF next_scroll =
2220 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2221
2222 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2223 client_->SetNeedsRedrawOnImplThread();
2224
2225 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2226 page_scale_animation_.reset();
2227 client_->SetNeedsCommitOnImplThread();
2228 client_->RenewTreePriority();
2229 }
[email protected]829ad972013-01-28 23:36:102230}
2231
[email protected]ffb2720f2013-03-15 19:18:372232void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2233 if (!top_controls_manager_ || !RootScrollLayer())
2234 return;
2235 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2236 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172237 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2238 return;
[email protected]ffb2720f2013-03-15 19:18:372239 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2240 scroll, 1.f / active_tree_->total_page_scale_factor()));
2241}
2242
[email protected]c1bb5af2013-03-13 19:06:272243void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2244 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522245 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272246 animation_registrar_->active_animation_controllers().empty() ||
2247 !active_tree_->root_layer())
2248 return;
2249
2250 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2251
2252 last_animation_time_ = wall_clock_time;
2253 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2254
2255 AnimationRegistrar::AnimationControllerMap copy =
2256 animation_registrar_->active_animation_controllers();
2257 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2258 iter != copy.end();
2259 ++iter)
2260 (*iter).second->Animate(monotonic_seconds);
2261
2262 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082263}
2264
[email protected]3d9f7432013-04-06 00:35:182265void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522266 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272267 animation_registrar_->active_animation_controllers().empty() ||
2268 !active_tree_->root_layer())
2269 return;
2270
2271 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2272 scoped_ptr<AnimationEventsVector> events =
2273 make_scoped_ptr(new AnimationEventsVector);
2274 AnimationRegistrar::AnimationControllerMap copy =
2275 animation_registrar_->active_animation_controllers();
2276 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2277 iter != copy.end();
2278 ++iter)
[email protected]3d9f7432013-04-06 00:35:182279 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272280
2281 if (!events->empty()) {
2282 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2283 last_animation_time_);
2284 }
[email protected]131a0c22013-02-12 18:31:082285}
2286
[email protected]c1bb5af2013-03-13 19:06:272287base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2288 return base::TimeDelta::FromSeconds(1);
2289}
2290
[email protected]50644642013-06-20 13:58:552291void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352292 DCHECK(current);
[email protected]50644642013-06-20 13:58:552293 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352294 current->DidLoseOutputSurface();
2295 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552296 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352297 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552298 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352299 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552300 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352301}
2302
[email protected]c1bb5af2013-03-13 19:06:272303std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2304 std::string str;
2305 if (active_tree_->root_layer()) {
2306 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2307 base::JSONWriter::WriteWithOptions(
2308 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2309 }
2310 return str;
2311}
2312
[email protected]c1bb5af2013-03-13 19:06:272313int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362314 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272315}
2316
[email protected]c1bb5af2013-03-13 19:06:272317void LayerTreeHostImpl::SendManagedMemoryStats(
2318 size_t memory_visible_bytes,
2319 size_t memory_visible_and_nearby_bytes,
2320 size_t memory_use_bytes) {
2321 if (!renderer_)
2322 return;
2323
2324 // Round the numbers being sent up to the next 8MB, to throttle the rate
2325 // at which we spam the GPU process.
2326 static const size_t rounding_step = 8 * 1024 * 1024;
2327 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2328 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2329 rounding_step);
2330 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2331 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2332 last_sent_memory_visible_and_nearby_bytes_ ==
2333 memory_visible_and_nearby_bytes &&
2334 last_sent_memory_use_bytes_ == memory_use_bytes) {
2335 return;
2336 }
2337 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2338 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2339 last_sent_memory_use_bytes_ = memory_use_bytes;
2340
2341 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2342 last_sent_memory_visible_and_nearby_bytes_,
2343 last_sent_memory_use_bytes_);
2344}
2345
2346void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2347 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2348}
2349
2350void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2351 base::TimeTicks time) {
2352 if (!layer)
2353 return;
2354
2355 ScrollbarAnimationController* scrollbar_controller =
2356 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352357 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202358 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422359 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2360 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272361 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202362 }
[email protected]c1bb5af2013-03-13 19:06:272363
2364 for (size_t i = 0; i < layer->children().size(); ++i)
2365 AnimateScrollbarsRecursive(layer->children()[i], time);
2366}
2367
[email protected]21c9dee72013-06-15 01:20:052368void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202369 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052370 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202371}
2372
2373void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2374 base::TimeTicks time) {
2375 if (!layer)
2376 return;
2377
2378 ScrollbarAnimationController* scrollbar_controller =
2379 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352380 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2381 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202382 if (delay > base::TimeDelta())
2383 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352384 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202385 client_->SetNeedsRedrawOnImplThread();
2386 }
2387
2388 for (size_t i = 0; i < layer->children().size(); ++i)
2389 StartScrollbarAnimationRecursive(layer->children()[i], time);
2390}
2391
[email protected]c1bb5af2013-03-13 19:06:272392void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2393 if (!tile_manager_)
2394 return;
2395
2396 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2397 if (new_state.tree_priority == priority)
2398 return;
2399
2400 new_state.tree_priority = priority;
2401 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:262402 manage_tiles_needed_ = true;
[email protected]c1bb5af2013-03-13 19:06:272403}
2404
[email protected]8347d692013-05-17 23:22:382405void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552406 current_frame_timeticks_ = base::TimeTicks();
2407 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272408}
2409
[email protected]21c9dee72013-06-15 01:20:052410void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2411 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552412 if (ticks->is_null()) {
2413 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052414 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552415 *now = base::Time::Now();
2416 }
2417}
2418
2419base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2420 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2421 return current_frame_timeticks_;
2422}
2423
2424base::Time LayerTreeHostImpl::CurrentFrameTime() {
2425 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272426 return current_frame_time_;
2427}
2428
[email protected]21c9dee72013-06-15 01:20:052429base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
2430 return base::TimeTicks::Now();
2431}
2432
[email protected]c1bb5af2013-03-13 19:06:272433scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
2434 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222435 if (this->pending_tree_)
2436 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272437 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212438 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272439 if (tile_manager_)
2440 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2441 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222442 if (pending_tree_)
2443 state->Set("pending_tree", pending_tree_->AsValue().release());
2444 return state.PassAs<base::Value>();
2445}
2446
2447scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222448 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2449 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472450 if (tile_manager_)
2451 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272452 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082453}
2454
[email protected]6e7fdeb2013-07-09 14:28:382455void LayerTreeHostImpl::SetDebugState(
2456 const LayerTreeDebugState& new_debug_state) {
2457 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2458 return;
2459 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272460 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242461
[email protected]6e7fdeb2013-07-09 14:28:382462 debug_state_ = new_debug_state;
2463 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022464}
2465
[email protected]c9280762013-08-01 06:28:572466void LayerTreeHostImpl::CreateUIResource(
2467 UIResourceId uid,
2468 scoped_refptr<UIResourceBitmap> bitmap) {
2469 DCHECK_GT(uid, 0);
2470 DCHECK_EQ(bitmap->GetFormat(), UIResourceBitmap::RGBA8);
2471
2472 // Allow for multiple creation requests with the same UIResourceId. The
2473 // previous resource is simply deleted.
2474 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2475 if (id)
2476 DeleteUIResource(uid);
2477 id = resource_provider_->CreateResource(
2478 bitmap->GetSize(), GL_RGBA, ResourceProvider::TextureUsageAny);
2479
2480 ui_resource_map_[uid] = id;
2481 resource_provider_->SetPixels(id,
2482 reinterpret_cast<uint8_t*>(bitmap->GetPixels()),
2483 gfx::Rect(bitmap->GetSize()),
2484 gfx::Rect(bitmap->GetSize()),
2485 gfx::Vector2d(0, 0));
2486}
2487
2488void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2489 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2490 if (id) {
2491 resource_provider_->DeleteResource(id);
2492 ui_resource_map_.erase(uid);
2493 }
2494}
2495
2496ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2497 UIResourceId uid) const {
2498 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2499 if (iter != ui_resource_map_.end())
2500 return iter->second;
2501 return 0;
2502}
2503
[email protected]d3143c732012-10-05 19:17:592504} // namespace cc