blob: b263f2f4173bad34cd1030a755453ce70afa6597 [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]be4655a2013-03-18 08:36:3143#include "cc/scheduler/delay_based_time_source.h"
44#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0445#include "cc/trees/damage_tracker.h"
46#include "cc/trees/layer_tree_host.h"
47#include "cc/trees/layer_tree_host_common.h"
48#include "cc/trees/layer_tree_impl.h"
49#include "cc/trees/quad_culler.h"
50#include "cc/trees/single_thread_proxy.h"
51#include "cc/trees/tree_synchronizer.h"
[email protected]d455d552012-11-02 00:19:0652#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1353#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1454
[email protected]94f206c12012-08-25 00:09:1455namespace {
56
[email protected]c1bb5af2013-03-13 19:06:2757void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
58 if (visible) {
59 TRACE_EVENT_ASYNC_BEGIN1("webkit",
60 "LayerTreeHostImpl::SetVisible",
61 id,
62 "LayerTreeHostImpl",
63 id);
64 return;
65 }
[email protected]94f206c12012-08-25 00:09:1466
[email protected]c1bb5af2013-03-13 19:06:2767 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1468}
69
[email protected]c1bb5af2013-03-13 19:06:2770} // namespace
[email protected]94f206c12012-08-25 00:09:1471
[email protected]9c88e562012-09-14 22:21:3072namespace cc {
[email protected]94f206c12012-08-25 00:09:1473
[email protected]96baf3e2012-10-22 23:09:5574class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:2775 public:
76 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
77 LayerTreeHostImpl* layer_tree_host_impl,
78 scoped_refptr<DelayBasedTimeSource> time_source) {
79 return make_scoped_ptr(
80 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
81 time_source));
82 }
83 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:2684 time_source_->SetClient(NULL);
85 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:2786 }
87
[email protected]6d0e69d2013-03-20 14:53:2688 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:2789 // In single threaded mode we attempt to simulate changing the current
90 // thread by maintaining a fake thread id. When we switch from one
91 // thread to another, we construct DebugScopedSetXXXThread objects that
92 // update the thread id. This lets DCHECKS that ensure we're on the
93 // right thread to work correctly in single threaded mode. The problem
94 // here is that the timer tasks are run via the message loop, and when
95 // they run, we've had no chance to construct a DebugScopedSetXXXThread
96 // object. The result is that we report that we're running on the main
97 // thread. In multi-threaded mode, this timer is run on the compositor
98 // thread, so to keep this consistent in single-threaded mode, we'll
99 // construct a DebugScopedSetImplThread object. There is no need to do
100 // this in multi-threaded mode since the real thread id's will be
101 // correct. In fact, setting fake thread id's interferes with the real
102 // thread id's and causes breakage.
103 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
104 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
105 set_impl_thread.reset(
106 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14107 }
108
[email protected]94bf75c2013-06-12 13:20:04109 // TODO(enne): This should probably happen post-animate.
110 if (layer_tree_host_impl_->pending_tree()) {
111 layer_tree_host_impl_->ActivatePendingTreeIfNeeded();
112
113 if (layer_tree_host_impl_->pending_tree()) {
114 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
115 layer_tree_host_impl_->ManageTiles();
116 }
117 }
118
[email protected]fb7425a2013-04-22 16:28:55119 layer_tree_host_impl_->Animate(
120 layer_tree_host_impl_->CurrentFrameTimeTicks(),
121 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18122 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
123 bool start_ready_animations = true;
124 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38125 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27126 }
[email protected]373974232013-01-10 22:20:50127
[email protected]c1bb5af2013-03-13 19:06:27128 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26129 if (active != time_source_->Active())
130 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27131 }
[email protected]94f206c12012-08-25 00:09:14132
[email protected]c1bb5af2013-03-13 19:06:27133 private:
134 LayerTreeHostImplTimeSourceAdapter(
135 LayerTreeHostImpl* layer_tree_host_impl,
136 scoped_refptr<DelayBasedTimeSource> time_source)
137 : layer_tree_host_impl_(layer_tree_host_impl),
138 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26139 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27140 }
[email protected]94f206c12012-08-25 00:09:14141
[email protected]c1bb5af2013-03-13 19:06:27142 LayerTreeHostImpl* layer_tree_host_impl_;
143 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14144
[email protected]c1bb5af2013-03-13 19:06:27145 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14146};
147
[email protected]96baf3e2012-10-22 23:09:55148LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20149 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27150
151LayerTreeHostImpl::FrameData::~FrameData() {}
152
153scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
154 const LayerTreeSettings& settings,
155 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43156 Proxy* proxy,
157 RenderingStatsInstrumentation* rendering_stats_instrumentation) {
158 return make_scoped_ptr(
159 new LayerTreeHostImpl(settings,
160 client,
161 proxy,
162 rendering_stats_instrumentation));
[email protected]493067512012-09-19 23:34:10163}
164
[email protected]372bad5f2013-03-21 16:38:43165LayerTreeHostImpl::LayerTreeHostImpl(
166 const LayerTreeSettings& settings,
167 LayerTreeHostImplClient* client,
168 Proxy* proxy,
169 RenderingStatsInstrumentation* rendering_stats_instrumentation)
[email protected]c1bb5af2013-03-13 19:06:27170 : client_(client),
171 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37172 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27173 did_lock_scrolling_layer_(false),
174 should_bubble_scrolls_(false),
175 wheel_scrolling_(false),
[email protected]a23451e2013-06-07 20:58:26176 manage_tiles_needed_(false),
[email protected]1960a712013-04-30 17:06:47177 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27178 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27179 visible_(true),
180 managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34181 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]c1bb5af2013-03-13 19:06:27182 ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING,
183 0,
184 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING),
185 pinch_gesture_active_(false),
[email protected]9e3594522013-03-18 00:57:36186 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29187 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27188 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02189 debug_rect_history_(DebugRectHistory::Create()),
[email protected]d7626ffd2013-03-29 00:17:42190 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27191 last_sent_memory_visible_bytes_(0),
192 last_sent_memory_visible_and_nearby_bytes_(0),
193 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55194 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32195 device_scale_factor_(1.f),
196 overdraw_bottom_height_(0.f),
[email protected]c32a1962013-07-30 19:41:17197 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43198 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19199 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]8bb6cfa2013-07-23 00:11:19200 need_to_update_visible_tiles_before_draw_(false) {
[email protected]c1bb5af2013-03-13 19:06:27201 DCHECK(proxy_->IsImplThread());
202 DidVisibilityChange(this, visible_);
203
[email protected]8e0176d2013-03-21 03:14:52204 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27205
[email protected]8e0176d2013-03-21 03:14:52206 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27207 top_controls_manager_ =
208 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52209 settings.top_controls_height,
210 settings.top_controls_show_threshold,
211 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27212 }
213
[email protected]8e0176d2013-03-21 03:14:52214 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27215
216 // LTHI always has an active tree.
217 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51218 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
219 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10220}
221
[email protected]c1bb5af2013-03-13 19:06:27222LayerTreeHostImpl::~LayerTreeHostImpl() {
223 DCHECK(proxy_->IsImplThread());
224 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51225 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
226 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27227
[email protected]200a9c062013-05-20 04:34:37228 if (input_handler_client_) {
229 input_handler_client_->WillShutdown();
230 input_handler_client_ = NULL;
231 }
232
[email protected]75deb742013-06-24 20:19:18233 // The layer trees must be destroyed before the layer tree host. We've
234 // made a contract with our animation controllers that the registrar
235 // will outlive them, and we must make good.
236 recycle_tree_.reset();
237 pending_tree_.reset();
238 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14239}
240
[email protected]c1bb5af2013-03-13 19:06:27241void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29242
[email protected]c1bb5af2013-03-13 19:06:27243void LayerTreeHostImpl::CommitComplete() {
244 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08245
[email protected]c1bb5af2013-03-13 19:06:27246 // Impl-side painting needs an update immediately post-commit to have the
247 // opportunity to create tilings. Other paths can call UpdateDrawProperties
248 // more lazily when needed prior to drawing.
[email protected]8e0176d2013-03-21 03:14:52249 if (settings_.impl_side_painting) {
[email protected]c1bb5af2013-03-13 19:06:27250 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04251 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26252 // Start working on newly created tiles immediately if needed.
253 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27254 } else {
255 active_tree_->set_needs_update_draw_properties();
256 }
[email protected]3ba4cae2013-01-16 03:58:38257
[email protected]c1bb5af2013-03-13 19:06:27258 client_->SendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14259}
260
[email protected]6133cc232013-07-30 18:47:07261bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27262 // Note: If you are changing this function or any other function that might
263 // affect the result of CanDraw, make sure to call
264 // client_->OnCanDrawStateChanged in the proper places and update the
265 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14266
[email protected]6133cc232013-07-30 18:47:07267 if (!renderer_) {
268 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
269 TRACE_EVENT_SCOPE_THREAD);
270 return false;
271 }
272
273 // Must have an OutputSurface if |renderer_| is not NULL.
274 DCHECK(output_surface_);
275
276 // TODO(boliu): Make draws without root_layer work and move this below
277 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27278 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42279 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
280 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22281 return false;
[email protected]c1bb5af2013-03-13 19:06:27282 }
[email protected]6133cc232013-07-30 18:47:07283
284 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
285 return true;
286
[email protected]c1bb5af2013-03-13 19:06:27287 if (device_viewport_size_.IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42288 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
289 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27290 return false;
291 }
292 if (active_tree_->ViewportSizeInvalid()) {
293 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42294 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
295 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27296 return false;
297 }
[email protected]c1bb5af2013-03-13 19:06:27298 if (active_tree_->ContentsTexturesPurged()) {
299 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42300 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
301 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27302 return false;
303 }
304 return true;
[email protected]2f1acc262012-11-16 21:42:22305}
306
[email protected]c1bb5af2013-03-13 19:06:27307void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
308 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37309 if (input_handler_client_)
310 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27311 AnimatePageScale(monotonic_time);
312 AnimateLayers(monotonic_time, wall_clock_time);
313 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37314 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14315}
316
[email protected]c1bb5af2013-03-13 19:06:27317void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26318 if (!tile_manager_)
319 return;
320 if (!manage_tiles_needed_)
321 return;
322 manage_tiles_needed_ = false;
[email protected]c1bb5af2013-03-13 19:06:27323 tile_manager_->ManageTiles();
324
325 size_t memory_required_bytes;
326 size_t memory_nice_to_have_bytes;
327 size_t memory_used_bytes;
328 tile_manager_->GetMemoryStats(&memory_required_bytes,
329 &memory_nice_to_have_bytes,
330 &memory_used_bytes);
331 SendManagedMemoryStats(memory_required_bytes,
332 memory_nice_to_have_bytes,
333 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15334}
335
[email protected]c1bb5af2013-03-13 19:06:27336void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
337 bool anchor_point,
338 float page_scale,
339 base::TimeTicks start_time,
340 base::TimeDelta duration) {
341 if (!RootScrollLayer())
342 return;
343
344 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47345 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27346 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]6fc4ee002013-03-26 23:39:51347 gfx::SizeF viewport_size = VisibleViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27348
349 double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF();
[email protected]85d136f782013-04-26 22:04:40350
351 // Easing constants experimentally determined.
352 scoped_ptr<TimingFunction> timing_function =
353 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
354
[email protected]c1bb5af2013-03-13 19:06:27355 page_scale_animation_ =
356 PageScaleAnimation::Create(scroll_total,
357 active_tree_->total_page_scale_factor(),
358 viewport_size,
359 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40360 start_time_seconds,
361 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27362
363 if (anchor_point) {
364 gfx::Vector2dF anchor(target_offset);
365 page_scale_animation_->ZoomWithAnchor(anchor,
366 page_scale,
367 duration.InSecondsF());
368 } else {
369 gfx::Vector2dF scaled_target_offset = target_offset;
370 page_scale_animation_->ZoomTo(scaled_target_offset,
371 page_scale,
372 duration.InSecondsF());
373 }
374
375 client_->SetNeedsRedrawOnImplThread();
376 client_->SetNeedsCommitOnImplThread();
377 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15378}
379
[email protected]c1bb5af2013-03-13 19:06:27380void LayerTreeHostImpl::ScheduleAnimation() {
381 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15382}
383
[email protected]c1bb5af2013-03-13 19:06:27384bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
385 if (!EnsureRenderSurfaceLayerList())
386 return false;
[email protected]f57bbc02012-11-21 07:02:15387
[email protected]c1bb5af2013-03-13 19:06:27388 gfx::PointF device_viewport_point =
389 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15390
[email protected]c1bb5af2013-03-13 19:06:27391 // First find out which layer was hit from the saved list of visible layers
392 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:39393 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:27394 device_viewport_point,
395 active_tree_->RenderSurfaceLayerList());
[email protected]f57bbc02012-11-21 07:02:15396
[email protected]c1bb5af2013-03-13 19:06:27397 // Walk up the hierarchy and look for a layer with a touch event handler
398 // region that the given point hits.
399 for (; layer_impl; layer_impl = layer_impl->parent()) {
[email protected]6ba914122013-03-22 16:26:39400 if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point,
[email protected]c1bb5af2013-03-13 19:06:27401 layer_impl))
402 return true;
403 }
[email protected]f57bbc02012-11-21 07:02:15404
[email protected]c1bb5af2013-03-13 19:06:27405 return false;
406}
407
[email protected]b76329d2013-06-11 04:15:08408void LayerTreeHostImpl::SetLatencyInfoForInputEvent(
409 const ui::LatencyInfo& latency_info) {
410 active_tree()->SetLatencyInfo(latency_info);
411}
412
[email protected]c1bb5af2013-03-13 19:06:27413void LayerTreeHostImpl::TrackDamageForAllSurfaces(
414 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28415 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27416 // For now, we use damage tracking to compute a global scissor. To do this, we
417 // must compute all damage tracking before drawing anything, so that we know
418 // the root damage rect. The root damage rect is then used to scissor each
419 // surface.
420
421 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06422 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27423 --surface_index) {
424 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
425 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
426 DCHECK(render_surface);
427 render_surface->damage_tracker()->UpdateDamageTrackingState(
428 render_surface->layer_list(),
429 render_surface_layer->id(),
430 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
431 render_surface->content_rect(),
432 render_surface_layer->mask_layer(),
433 render_surface_layer->filters(),
434 render_surface_layer->filter().get());
435 }
436}
437
438void LayerTreeHostImpl::FrameData::AppendRenderPass(
439 scoped_ptr<RenderPass> render_pass) {
440 render_passes_by_id[render_pass->id] = render_pass.get();
441 render_passes.push_back(render_pass.Pass());
442}
443
[email protected]ffbb2212013-06-02 23:47:59444static DrawMode GetDrawMode(OutputSurface* output_surface) {
445 if (output_surface->ForcedDrawToSoftwareDevice()) {
446 return DRAW_MODE_RESOURCELESS_SOFTWARE;
447 } else if (output_surface->context3d()) {
448 return DRAW_MODE_HARDWARE;
449 } else {
450 DCHECK(output_surface->software_device());
451 return DRAW_MODE_SOFTWARE;
452 }
453}
454
[email protected]c1bb5af2013-03-13 19:06:27455static void AppendQuadsForLayer(RenderPass* target_render_pass,
456 LayerImpl* layer,
457 const OcclusionTrackerImpl& occlusion_tracker,
458 AppendQuadsData* append_quads_data) {
459 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49460 QuadCuller quad_culler(&target_render_pass->quad_list,
461 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27462 layer,
463 occlusion_tracker,
464 layer->ShowDebugBorders(),
465 for_surface);
466 layer->AppendQuads(&quad_culler, append_quads_data);
467}
468
469static void AppendQuadsForRenderSurfaceLayer(
470 RenderPass* target_render_pass,
471 LayerImpl* layer,
472 const RenderPass* contributing_render_pass,
473 const OcclusionTrackerImpl& occlusion_tracker,
474 AppendQuadsData* append_quads_data) {
475 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49476 QuadCuller quad_culler(&target_render_pass->quad_list,
477 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27478 layer,
479 occlusion_tracker,
480 layer->ShowDebugBorders(),
481 for_surface);
482
483 bool is_replica = false;
484 layer->render_surface()->AppendQuads(&quad_culler,
485 append_quads_data,
486 is_replica,
487 contributing_render_pass->id);
488
489 // Add replica after the surface so that it appears below the surface.
490 if (layer->has_replica()) {
491 is_replica = true;
492 layer->render_surface()->AppendQuads(&quad_culler,
493 append_quads_data,
494 is_replica,
495 contributing_render_pass->id);
496 }
497}
498
499static void AppendQuadsToFillScreen(
500 RenderPass* target_render_pass,
501 LayerImpl* root_layer,
502 SkColor screen_background_color,
503 const OcclusionTrackerImpl& occlusion_tracker) {
504 if (!root_layer || !SkColorGetA(screen_background_color))
505 return;
506
507 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
508 if (fill_region.IsEmpty())
509 return;
510
511 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49512 QuadCuller quad_culler(&target_render_pass->quad_list,
513 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27514 root_layer,
515 occlusion_tracker,
516 root_layer->ShowDebugBorders(),
517 for_surface);
518
519 // Manually create the quad state for the gutter quads, as the root layer
520 // doesn't have any bounds and so can't generate this itself.
521 // TODO(danakj): Make the gutter quads generated by the solid color layer
522 // (make it smarter about generating quads to fill unoccluded areas).
523
524 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
525 float opacity = 1.f;
526 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49527 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27528 shared_quad_state->SetAll(root_layer->draw_transform(),
529 root_target_rect.size(),
530 root_target_rect,
531 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01532 false,
[email protected]f57bbc02012-11-21 07:02:15533 opacity);
534
[email protected]c1bb5af2013-03-13 19:06:27535 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17536
[email protected]c1bb5af2013-03-13 19:06:27537 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
538 bool did_invert = root_layer->screen_space_transform().GetInverse(
539 &transform_to_layer_space);
540 DCHECK(did_invert);
541 for (Region::Iterator fill_rects(fill_region);
542 fill_rects.has_rect();
543 fill_rects.next()) {
544 // The root layer transform is composed of translations and scales only,
545 // no perspective, so mapping is sufficient (as opposed to projecting).
546 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21547 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]c1bb5af2013-03-13 19:06:27548 // Skip the quad culler and just append the quads directly to avoid
549 // occlusion checks.
550 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]10a30b12013-05-02 16:42:11551 quad->SetNew(shared_quad_state, layer_rect, screen_background_color, false);
[email protected]c7e95b42013-03-18 01:13:49552 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27553 }
[email protected]467b3612012-08-28 07:41:16554}
555
[email protected]c1bb5af2013-03-13 19:06:27556bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
557 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14558
[email protected]c1bb5af2013-03-13 19:06:27559 if (!CanDraw() || !active_tree_->root_layer())
560 return false;
[email protected]2d692992012-12-19 01:19:32561
[email protected]c1bb5af2013-03-13 19:06:27562 TrackDamageForAllSurfaces(active_tree_->root_layer(),
563 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14564
[email protected]e0341352013-04-06 05:01:20565 // If the root render surface has no visible damage, then don't generate a
566 // frame at all.
567 RenderSurfaceImpl* root_surface =
568 active_tree_->root_layer()->render_surface();
569 bool root_surface_has_no_visible_damage =
570 !root_surface->damage_tracker()->current_damage_rect().Intersects(
571 root_surface->content_rect());
572 bool root_surface_has_contributing_layers =
573 !root_surface->layer_list().empty();
574 if (root_surface_has_contributing_layers &&
575 root_surface_has_no_visible_damage) {
576 TRACE_EVENT0("cc",
577 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
578 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45579 // A copy request should cause damage, so we should not have any copy
580 // requests in this case.
581 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07582 DCHECK(!output_surface_->capabilities()
583 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20584 return true;
585 }
586
[email protected]c1bb5af2013-03-13 19:06:27587 TRACE_EVENT1("cc",
588 "LayerTreeHostImpl::CalculateRenderPasses",
589 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06590 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14591
[email protected]c1bb5af2013-03-13 19:06:27592 // Create the render passes in dependency order.
593 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06594 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27595 --surface_index) {
596 LayerImpl* render_surface_layer =
597 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45598 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
599
600 bool should_draw_into_render_pass =
601 render_surface_layer->parent() == NULL ||
602 render_surface->contributes_to_drawn_surface() ||
603 render_surface_layer->HasCopyRequest();
604 if (should_draw_into_render_pass)
605 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27606 }
[email protected]94f206c12012-08-25 00:09:14607
[email protected]c1bb5af2013-03-13 19:06:27608 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52609 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27610 base::debug::TraceLog::GetInstance() &&
611 base::debug::TraceLog::GetInstance()->IsEnabled();
612 OcclusionTrackerImpl occlusion_tracker(
613 active_tree_->root_layer()->render_surface()->content_rect(),
614 record_metrics_for_frame);
615 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52616 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14617
[email protected]846f455b2013-03-18 19:07:41618 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27619 occlusion_tracker.set_occluding_screen_space_rects_container(
620 &frame->occluding_screen_space_rects);
621 }
[email protected]846f455b2013-03-18 19:07:41622 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27623 occlusion_tracker.set_non_occluding_screen_space_rects_container(
624 &frame->non_occluding_screen_space_rects);
625 }
[email protected]94f206c12012-08-25 00:09:14626
[email protected]c1bb5af2013-03-13 19:06:27627 // Add quads to the Render passes in FrontToBack order to allow for testing
628 // occlusion and performing culling during the tree walk.
629 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28630 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27631 RenderSurfaceImpl,
632 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14633
[email protected]c1bb5af2013-03-13 19:06:27634 // Typically when we are missing a texture and use a checkerboard quad, we
635 // still draw the frame. However when the layer being checkerboarded is moving
636 // due to an impl-animation, we drop the frame to avoid flashing due to the
637 // texture suddenly appearing in the future.
638 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25639 // When we have a copy request for a layer, we need to draw no matter
640 // what, as the layer may disappear after this frame.
641 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14642
[email protected]372bad5f2013-03-21 16:38:43643 int layers_drawn = 0;
644
[email protected]ffbb2212013-06-02 23:47:59645 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
646
[email protected]c1bb5af2013-03-13 19:06:27647 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09648 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27649 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09650 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27651 it != end;
652 ++it) {
653 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09654 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27655 RenderPass* target_render_pass =
656 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14657
[email protected]0e5f7142013-05-24 06:45:36658 bool prevent_occlusion = it.target_render_surface_layer()->HasCopyRequest();
[email protected]2ea5e6c2013-04-26 21:52:23659 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14660
[email protected]30fe19ff2013-07-04 00:54:45661 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30662
[email protected]18a70192013-04-26 16:18:25663 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36664 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25665 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11666 it->TakeCopyRequestsAndTransformToTarget(
667 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25668 }
[email protected]30fe19ff2013-07-04 00:54:45669 } else if (it.represents_contributing_render_surface() &&
670 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27671 RenderPass::Id contributing_render_pass_id =
672 it->render_surface()->RenderPassId();
673 RenderPass* contributing_render_pass =
674 frame->render_passes_by_id[contributing_render_pass_id];
675 AppendQuadsForRenderSurfaceLayer(target_render_pass,
676 *it,
677 contributing_render_pass,
678 occlusion_tracker,
679 &append_quads_data);
[email protected]e1e768f2013-03-26 08:48:09680 } else if (it.represents_itself() &&
681 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27682 bool has_occlusion_from_outside_target_surface;
683 bool impl_draw_transform_is_unknown = false;
684 if (occlusion_tracker.Occluded(
685 it->render_target(),
686 it->visible_content_rect(),
687 it->draw_transform(),
688 impl_draw_transform_is_unknown,
689 it->is_clipped(),
690 it->clip_rect(),
691 &has_occlusion_from_outside_target_surface)) {
[email protected]e1e768f2013-03-26 08:48:09692 append_quads_data.had_occlusion_from_outside_target_surface |=
[email protected]c1bb5af2013-03-13 19:06:27693 has_occlusion_from_outside_target_surface;
[email protected]ffbb2212013-06-02 23:47:59694 } else if (it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27695 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59696
[email protected]c1bb5af2013-03-13 19:06:27697 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57698
[email protected]c1bb5af2013-03-13 19:06:27699 if (it->HasContributingDelegatedRenderPasses()) {
700 RenderPass::Id contributing_render_pass_id =
701 it->FirstContributingRenderPassId();
702 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
703 frame->render_passes_by_id.end()) {
704 RenderPass* render_pass =
705 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14706
[email protected]c1bb5af2013-03-13 19:06:27707 AppendQuadsData append_quads_data(render_pass->id);
708 AppendQuadsForLayer(render_pass,
709 *it,
710 occlusion_tracker,
711 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57712
[email protected]c1bb5af2013-03-13 19:06:27713 contributing_render_pass_id =
714 it->NextContributingRenderPassId(contributing_render_pass_id);
715 }
[email protected]94f206c12012-08-25 00:09:14716 }
717
[email protected]c1bb5af2013-03-13 19:06:27718 AppendQuadsForLayer(target_render_pass,
719 *it,
720 occlusion_tracker,
721 &append_quads_data);
722 }
[email protected]89228202012-08-29 03:20:30723
[email protected]372bad5f2013-03-21 16:38:43724 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14725 }
726
[email protected]e1e768f2013-03-26 08:48:09727 if (append_quads_data.had_occlusion_from_outside_target_surface)
[email protected]c1bb5af2013-03-13 19:06:27728 target_render_pass->has_occlusion_from_outside_target_surface = true;
729
[email protected]e1e768f2013-03-26 08:48:09730 if (append_quads_data.num_missing_tiles) {
[email protected]372bad5f2013-03-21 16:38:43731 rendering_stats_instrumentation_->AddMissingTiles(
[email protected]e1e768f2013-03-26 08:48:09732 append_quads_data.num_missing_tiles);
[email protected]c1bb5af2013-03-13 19:06:27733 bool layer_has_animating_transform =
734 it->screen_space_transform_is_animating() ||
735 it->draw_transform_is_animating();
736 if (layer_has_animating_transform)
737 draw_frame = false;
738 }
739
[email protected]e1e768f2013-03-26 08:48:09740 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27741 frame->contains_incomplete_tile = true;
742
743 occlusion_tracker.LeaveLayer(it);
744 }
745
[email protected]6133cc232013-07-30 18:47:07746 if (have_copy_request ||
747 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25748 draw_frame = true;
749
[email protected]372bad5f2013-03-21 16:38:43750 rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn);
751
[email protected]1d993172012-10-18 18:15:04752#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27753 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
754 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
755 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
756 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
757 != frame->render_passes_by_id.end());
758 }
[email protected]94f206c12012-08-25 00:09:14759#endif
[email protected]c1bb5af2013-03-13 19:06:27760 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14761
[email protected]c1bb5af2013-03-13 19:06:27762 if (!active_tree_->has_transparent_background()) {
763 frame->render_passes.back()->has_transparent_background = false;
764 AppendQuadsToFillScreen(frame->render_passes.back(),
765 active_tree_->root_layer(),
766 active_tree_->background_color(),
767 occlusion_tracker);
768 }
[email protected]94f206c12012-08-25 00:09:14769
[email protected]c1bb5af2013-03-13 19:06:27770 if (draw_frame)
771 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25772 else
773 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14774
[email protected]c1bb5af2013-03-13 19:06:27775 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]b09c1942013-05-10 00:06:37776 if (!output_surface_->ForcedDrawToSoftwareDevice())
777 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]d15d9622013-06-21 02:49:15778 RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()),
779 frame);
[email protected]94f206c12012-08-25 00:09:14780
[email protected]30fe19ff2013-07-04 00:54:45781 // Any copy requests left in the tree are not going to get serviced, and
782 // should be aborted.
783 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
784 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
785 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
786 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
787 }
788 for (size_t i = 0; i < requests_to_abort.size(); ++i)
789 requests_to_abort[i]->SendEmptyResult();
790
[email protected]e0341352013-04-06 05:01:20791 // If we're making a frame to draw, it better have at least one render pass.
792 DCHECK(!frame->render_passes.empty());
[email protected]c1bb5af2013-03-13 19:06:27793 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14794}
795
[email protected]200a9c062013-05-20 04:34:37796void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
797 if (input_handler_client_)
798 input_handler_client_->MainThreadHasStoppedFlinging();
799}
800
[email protected]3d9f7432013-04-06 00:35:18801void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
802 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15803 DCHECK(proxy_->IsImplThread());
804
[email protected]3d9f7432013-04-06 00:35:18805 bool enabled = should_background_tick &&
806 !animation_registrar_->active_animation_controllers().empty();
807
[email protected]c1bb5af2013-03-13 19:06:27808 // Lazily create the time_source adapter so that we can vary the interval for
809 // testing.
810 if (!time_source_client_adapter_) {
811 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
812 this,
[email protected]810d40b72013-06-20 18:26:15813 DelayBasedTimeSource::Create(
814 LowFrequencyAnimationInterval(),
815 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
816 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27817 }
[email protected]94f206c12012-08-25 00:09:14818
[email protected]c1bb5af2013-03-13 19:06:27819 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14820}
821
[email protected]b9d4a362013-04-23 05:36:27822void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02823 viewport_damage_rect_.Union(damage_rect);
824}
825
[email protected]c1bb5af2013-03-13 19:06:27826static inline RenderPass* FindRenderPassById(
827 RenderPass::Id render_pass_id,
828 const LayerTreeHostImpl::FrameData& frame) {
829 RenderPassIdHashMap::const_iterator it =
830 frame.render_passes_by_id.find(render_pass_id);
831 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14832}
833
[email protected]c1bb5af2013-03-13 19:06:27834static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
835 LayerTreeHostImpl::FrameData* frame) {
836 RenderPass* remove_render_pass =
837 FindRenderPassById(remove_render_pass_id, *frame);
838 // The pass was already removed by another quad - probably the original, and
839 // we are the replica.
840 if (!remove_render_pass)
841 return;
842 RenderPassList& render_passes = frame->render_passes;
843 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
844 render_passes.end(),
845 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14846
[email protected]c1bb5af2013-03-13 19:06:27847 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14848
[email protected]c1bb5af2013-03-13 19:06:27849 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
850 frame->render_passes.erase(to_remove);
851 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14852
[email protected]c1bb5af2013-03-13 19:06:27853 // Now follow up for all RenderPass quads and remove their RenderPasses
854 // recursively.
855 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29856 QuadList::ConstBackToFrontIterator quad_list_iterator =
857 quad_list.BackToFrontBegin();
858 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27859 ++quad_list_iterator) {
860 DrawQuad* current_quad = (*quad_list_iterator);
861 if (current_quad->material != DrawQuad::RENDER_PASS)
862 continue;
[email protected]94f206c12012-08-25 00:09:14863
[email protected]c1bb5af2013-03-13 19:06:27864 RenderPass::Id next_remove_render_pass_id =
865 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
866 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
867 }
[email protected]94f206c12012-08-25 00:09:14868}
869
[email protected]c1bb5af2013-03-13 19:06:27870bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::
871 ShouldRemoveRenderPass(const RenderPassDrawQuad& quad,
872 const FrameData& frame) const {
[email protected]fbe89f72013-05-21 07:24:24873 DCHECK(renderer_);
[email protected]c1bb5af2013-03-13 19:06:27874 bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty();
875 bool quad_has_cached_resource =
[email protected]bf691c22013-03-26 21:15:06876 renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]c1bb5af2013-03-13 19:06:27877 if (quad_has_damage) {
878 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage");
879 return false;
880 } else if (!quad_has_cached_resource) {
881 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture");
882 return false;
883 }
884 TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!");
885 return true;
[email protected]94f206c12012-08-25 00:09:14886}
887
[email protected]c1bb5af2013-03-13 19:06:27888bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
889 const RenderPassDrawQuad& quad, const FrameData& frame) const {
890 const RenderPass* render_pass =
891 FindRenderPassById(quad.render_pass_id, frame);
892 if (!render_pass)
893 return false;
[email protected]94f206c12012-08-25 00:09:14894
[email protected]c1bb5af2013-03-13 19:06:27895 // If any quad or RenderPass draws into this RenderPass, then keep it.
896 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29897 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
898 quad_list.BackToFrontBegin();
899 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27900 ++quad_list_iterator) {
901 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14902
[email protected]c1bb5af2013-03-13 19:06:27903 if (current_quad->material != DrawQuad::RENDER_PASS)
904 return false;
[email protected]94f206c12012-08-25 00:09:14905
[email protected]c1bb5af2013-03-13 19:06:27906 const RenderPass* contributing_pass = FindRenderPassById(
907 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
908 if (contributing_pass)
909 return false;
910 }
911 return true;
[email protected]94f206c12012-08-25 00:09:14912}
913
914// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27915template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
916 LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(
917 CullRenderPassesWithCachedTextures culler, FrameData* frame);
918template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
919 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
920 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14921
922// static
[email protected]c1bb5af2013-03-13 19:06:27923template <typename RenderPassCuller>
924void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
925 FrameData* frame) {
926 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
927 it != culler.RenderPassListEnd(frame->render_passes);
928 it = culler.RenderPassListNext(it)) {
929 const RenderPass* current_pass = frame->render_passes[it];
930 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29931 QuadList::ConstBackToFrontIterator quad_list_iterator =
932 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14933
[email protected]ed511b8d2013-03-25 03:29:29934 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27935 ++quad_list_iterator) {
936 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14937
[email protected]c1bb5af2013-03-13 19:06:27938 if (current_quad->material != DrawQuad::RENDER_PASS)
939 continue;
[email protected]94f206c12012-08-25 00:09:14940
[email protected]c1bb5af2013-03-13 19:06:27941 const RenderPassDrawQuad* render_pass_quad =
942 RenderPassDrawQuad::MaterialCast(current_quad);
943 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
944 continue;
[email protected]94f206c12012-08-25 00:09:14945
[email protected]c1bb5af2013-03-13 19:06:27946 // We are changing the vector in the middle of iteration. Because we
947 // delete render passes that draw into the current pass, we are
948 // guaranteed that any data from the iterator to the end will not
949 // change. So, capture the iterator position from the end of the
950 // list, and restore it after the change.
951 size_t position_from_end = frame->render_passes.size() - it;
952 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
953 it = frame->render_passes.size() - position_from_end;
954 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:14955 }
[email protected]c1bb5af2013-03-13 19:06:27956 }
[email protected]94f206c12012-08-25 00:09:14957}
958
[email protected]e0341352013-04-06 05:01:20959bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
960 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:47961 TRACE_EVENT1("cc",
962 "LayerTreeHostImpl::PrepareToDraw",
963 "SourceFrameNumber",
964 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:14965
[email protected]8bb6cfa2013-07-23 00:11:19966 if (need_to_update_visible_tiles_before_draw_) {
[email protected]39643fb2013-07-09 17:28:19967 DCHECK(tile_manager_);
[email protected]8bb6cfa2013-07-23 00:11:19968 if (tile_manager_->UpdateVisibleTiles())
969 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:19970 }
971
[email protected]7d19dc342013-05-02 22:02:04972 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:27973
[email protected]c1bb5af2013-03-13 19:06:27974 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
975 frame->render_passes.clear();
976 frame->render_passes_by_id.clear();
977 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:20978 frame->contains_incomplete_tile = false;
979 frame->has_no_damage = false;
980
981 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:02982 device_viewport_damage_rect.Union(viewport_damage_rect_);
983 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:20984
985 active_tree_->root_layer()->render_surface()->damage_tracker()->
986 AddDamageNextUpdate(device_viewport_damage_rect);
987 }
[email protected]94f206c12012-08-25 00:09:14988
[email protected]6133cc232013-07-30 18:47:07989 if (!CalculateRenderPasses(frame)) {
990 DCHECK(!output_surface_->capabilities()
991 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:27992 return false;
[email protected]6133cc232013-07-30 18:47:07993 }
[email protected]94f206c12012-08-25 00:09:14994
[email protected]c1bb5af2013-03-13 19:06:27995 // If we return true, then we expect DrawLayers() to be called before this
996 // function is called again.
997 return true;
[email protected]94f206c12012-08-25 00:09:14998}
999
[email protected]0309ba5e02013-06-26 04:11:081000void LayerTreeHostImpl::EvictTexturesForTesting() {
1001 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1002}
1003
[email protected]c1bb5af2013-03-13 19:06:271004void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1005 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121006
[email protected]c1bb5af2013-03-13 19:06:271007 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]46b8acc2013-03-19 22:38:351008 visible_ ? policy.bytes_limit_when_visible
1009 : policy.bytes_limit_when_not_visible,
1010 ManagedMemoryPolicy::PriorityCutoffToValue(
1011 visible_ ? policy.priority_cutoff_when_visible
1012 : policy.priority_cutoff_when_not_visible));
[email protected]c1bb5af2013-03-13 19:06:271013 if (evicted_resources) {
1014 active_tree_->SetContentsTexturesPurged();
1015 if (pending_tree_)
1016 pending_tree_->SetContentsTexturesPurged();
1017 client_->SetNeedsCommitOnImplThread();
1018 client_->OnCanDrawStateChanged(CanDraw());
1019 client_->RenewTreePriority();
1020 }
1021 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431022
[email protected]8be1a9bf2013-05-01 03:45:191023 UpdateTileManagerMemoryPolicy(policy);
1024}
1025
1026void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1027 const ManagedMemoryPolicy& policy) {
1028 if (!tile_manager_)
1029 return;
1030
1031 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
1032 new_state.memory_limit_in_bytes = visible_ ?
1033 policy.bytes_limit_when_visible :
1034 policy.bytes_limit_when_not_visible;
1035 // TODO(reveman): We should avoid keeping around unused resources if
1036 // possible. crbug.com/224475
1037 new_state.unused_memory_limit_in_bytes = static_cast<size_t>(
1038 (static_cast<int64>(new_state.memory_limit_in_bytes) *
1039 settings_.max_unused_resource_memory_percentage) / 100);
1040 new_state.memory_limit_policy =
1041 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1042 visible_ ?
1043 policy.priority_cutoff_when_visible :
1044 policy.priority_cutoff_when_not_visible);
1045 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:261046 manage_tiles_needed_ = true;
[email protected]94f206c12012-08-25 00:09:141047}
1048
[email protected]c1bb5af2013-03-13 19:06:271049bool LayerTreeHostImpl::HasImplThread() const {
1050 return proxy_->HasImplThread();
[email protected]61de5812012-11-08 07:03:441051}
1052
[email protected]86126792013-03-16 20:07:541053void LayerTreeHostImpl::DidInitializeVisibleTile() {
1054 // TODO(reveman): Determine tiles that changed and only damage
1055 // what's necessary.
1056 SetFullRootLayerDamage();
[email protected]c1bb5af2013-03-13 19:06:271057 if (client_)
[email protected]86126792013-03-16 20:07:541058 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471059}
1060
[email protected]94bf75c2013-06-12 13:20:041061void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]39643fb2013-07-09 17:28:191062 if (pending_tree_) {
[email protected]8bb6cfa2013-07-23 00:11:191063 need_to_update_visible_tiles_before_draw_ = true;
[email protected]94bf75c2013-06-12 13:20:041064 ActivatePendingTree();
[email protected]39643fb2013-07-09 17:28:191065 }
[email protected]94bf75c2013-06-12 13:20:041066}
1067
[email protected]c1bb5af2013-03-13 19:06:271068bool LayerTreeHostImpl::ShouldClearRootRenderPass() const {
[email protected]8e0176d2013-03-21 03:14:521069 return settings_.should_clear_root_render_pass;
[email protected]f35e2322012-12-15 21:45:521070}
1071
[email protected]fd32d122013-06-29 13:11:041072void LayerTreeHostImpl::SetMemoryPolicy(
1073 const ManagedMemoryPolicy& policy,
1074 bool discard_backbuffer_when_not_visible) {
1075 if (policy.bytes_limit_when_visible) {
1076 // Just ignore the memory manager when it says to set the limit to zero
1077 // bytes. This will happen when the memory manager thinks that the renderer
1078 // is not visible (which the renderer knows better).
1079 SetManagedMemoryPolicy(policy);
1080 }
1081 renderer_->SetDiscardBackBufferWhenNotVisible(
1082 discard_backbuffer_when_not_visible);
1083}
1084
[email protected]5f07afc2013-07-19 05:17:181085void LayerTreeHostImpl::SetTreeActivationCallback(
1086 const base::Closure& callback) {
1087 DCHECK(proxy_->IsImplThread());
1088 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261089 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181090}
1091
[email protected]c1bb5af2013-03-13 19:06:271092void LayerTreeHostImpl::SetManagedMemoryPolicy(
1093 const ManagedMemoryPolicy& policy) {
1094 if (managed_memory_policy_ == policy)
1095 return;
[email protected]61de5812012-11-08 07:03:441096
[email protected]50644642013-06-20 13:58:551097 if (zero_budget_)
1098 DCHECK_EQ(0u, policy.bytes_limit_when_visible);
1099
[email protected]d7626ffd2013-03-29 00:17:421100 // If there is already enough memory to draw everything imaginable and the
1101 // new memory limit does not change this, then do not re-commit. Don't bother
1102 // skipping commits if this is not visible (commits don't happen when not
1103 // visible, there will almost always be a commit when this becomes visible).
1104 bool needs_commit = true;
1105 if (visible() &&
1106 policy.bytes_limit_when_visible >=
1107 max_memory_needed_bytes_ &&
1108 managed_memory_policy_.bytes_limit_when_visible >=
1109 max_memory_needed_bytes_ &&
1110 policy.priority_cutoff_when_visible ==
1111 managed_memory_policy_.priority_cutoff_when_visible) {
1112 needs_commit = false;
1113 }
1114
[email protected]c1bb5af2013-03-13 19:06:271115 managed_memory_policy_ = policy;
1116 if (!proxy_->HasImplThread()) {
[email protected]d7626ffd2013-03-29 00:17:421117 // In single-thread mode, this can be called on the main thread by
1118 // GLRenderer::OnMemoryAllocationChanged.
[email protected]c1bb5af2013-03-13 19:06:271119 DebugScopedSetImplThread impl_thread(proxy_);
[email protected]206a3922013-05-17 06:34:121120 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271121 } else {
1122 DCHECK(proxy_->IsImplThread());
[email protected]206a3922013-05-17 06:34:121123 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]c1bb5af2013-03-13 19:06:271124 }
[email protected]d7626ffd2013-03-29 00:17:421125
1126 if (needs_commit)
1127 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141128}
1129
[email protected]f224cc92013-06-06 23:23:321130void LayerTreeHostImpl::SetExternalDrawConstraints(
1131 const gfx::Transform& transform,
1132 gfx::Rect viewport) {
1133 external_transform_ = transform;
1134 external_viewport_ = viewport;
1135}
1136
[email protected]c32a1962013-07-30 19:41:171137void LayerTreeHostImpl::SetExternalStencilTest(bool enabled) {
1138 external_stencil_test_enabled_ = enabled;
1139}
1140
[email protected]1cd9f5552013-04-26 04:22:031141void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1142 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1143}
1144
[email protected]df3c24c92013-06-19 03:54:351145void LayerTreeHostImpl::BeginFrame(const BeginFrameArgs& args) {
1146 client_->BeginFrameOnImplThread(args);
[email protected]7ed47512013-03-26 22:28:451147}
1148
[email protected]36e5ff12013-06-11 12:19:291149void LayerTreeHostImpl::OnSwapBuffersComplete(
1150 const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271151 // TODO(piman): We may need to do some validation on this ack before
1152 // processing it.
[email protected]36e5ff12013-06-11 12:19:291153 if (ack && renderer_)
1154 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]bb1e2822013-04-17 22:06:011155
[email protected]36e5ff12013-06-11 12:19:291156 client_->OnSwapBuffersCompleteOnImplThread();
[email protected]a46f32932012-12-07 21:43:161157}
1158
[email protected]c1bb5af2013-03-13 19:06:271159void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1160 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291161}
1162
[email protected]c1bb5af2013-03-13 19:06:271163CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1164 CompositorFrameMetadata metadata;
1165 metadata.device_scale_factor = device_scale_factor_;
1166 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1167 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1168 metadata.root_layer_size = active_tree_->ScrollableSize();
1169 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1170 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501171 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271172 if (top_controls_manager_) {
1173 metadata.location_bar_offset =
1174 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1175 metadata.location_bar_content_translation =
1176 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011177 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271178 }
[email protected]bf189f62012-12-18 03:42:111179
[email protected]c1bb5af2013-03-13 19:06:271180 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111181 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271182
[email protected]ffb2720f2013-03-15 19:18:371183 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271184
1185 return metadata;
[email protected]bf189f62012-12-18 03:42:111186}
1187
[email protected]73673592013-04-03 22:14:321188bool LayerTreeHostImpl::AllowPartialSwap() const {
1189 // We don't track damage on the HUD layer (it interacts with damage tracking
1190 // visualizations), so disable partial swaps to make the HUD layer display
1191 // properly.
1192 return !debug_state_.ShowHudRects();
1193}
1194
[email protected]c32a1962013-07-30 19:41:171195bool LayerTreeHostImpl::ExternalStencilTestEnabled() const {
1196 return external_stencil_test_enabled_;
1197}
1198
[email protected]6f50b8fa2013-07-01 19:51:491199static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1200 layer->DidBeginTracing();
1201}
[email protected]37349bc2013-06-04 01:31:521202
[email protected]f0c2a242013-03-15 19:34:521203void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1204 base::TimeTicks frame_begin_time) {
[email protected]c1bb5af2013-03-13 19:06:271205 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
1206 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201207
[email protected]3519b872013-07-30 07:17:501208 if (frame->has_no_damage) {
1209 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071210 DCHECK(!output_surface_->capabilities()
1211 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201212 return;
[email protected]3519b872013-07-30 07:17:501213 }
[email protected]e0341352013-04-06 05:01:201214
[email protected]c1bb5af2013-03-13 19:06:271215 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141216
[email protected]9e3594522013-03-18 00:57:361217 fps_counter_->SaveTimeStamp(frame_begin_time);
[email protected]94f206c12012-08-25 00:09:141218
[email protected]372bad5f2013-03-21 16:38:431219 rendering_stats_instrumentation_->SetScreenFrameCount(
1220 fps_counter_->current_frame_number());
1221 rendering_stats_instrumentation_->SetDroppedFrameCount(
1222 fps_counter_->dropped_frame_count());
1223
[email protected]c1bb5af2013-03-13 19:06:271224 if (tile_manager_) {
1225 memory_history_->SaveEntry(
1226 tile_manager_->memory_stats_from_last_assign());
1227 }
[email protected]1191d9d2013-02-02 06:00:331228
[email protected]846f455b2013-03-18 19:07:411229 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021230 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271231 active_tree_->root_layer(),
1232 *frame->render_surface_layer_list,
1233 frame->occluding_screen_space_rects,
1234 frame->non_occluding_screen_space_rects,
1235 debug_state_);
1236 }
[email protected]94f206c12012-08-25 00:09:141237
[email protected]a848c102013-03-26 08:59:091238 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1239 const RenderingStats& stats =
1240 rendering_stats_instrumentation_->GetRenderingStats();
1241 paint_time_counter_->SavePaintTime(stats.total_paint_time);
1242 }
1243
[email protected]37349bc2013-06-04 01:31:521244 bool is_new_trace;
1245 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1246 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491247 if (pending_tree_) {
1248 LayerTreeHostCommon::CallFunctionForSubtree(
1249 pending_tree_->root_layer(),
1250 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1251 }
1252 LayerTreeHostCommon::CallFunctionForSubtree(
1253 active_tree_->root_layer(),
1254 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521255 }
1256
[email protected]9197dbcb2013-05-15 20:28:511257 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1258 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this,
1259 TracedValue::FromValue(AsValue().release()));
[email protected]131a0c22013-02-12 18:31:081260
[email protected]c1bb5af2013-03-13 19:06:271261 // Because the contents of the HUD depend on everything else in the frame, the
1262 // contents of its texture are updated as the last thing before the frame is
1263 // drawn.
1264 if (active_tree_->hud_layer())
[email protected]264dc0332013-03-17 21:00:541265 active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get());
[email protected]94f206c12012-08-25 00:09:141266
[email protected]b09c1942013-05-10 00:06:371267 if (output_surface_->ForcedDrawToSoftwareDevice()) {
1268 scoped_ptr<SoftwareRenderer> temp_software_renderer =
1269 SoftwareRenderer::Create(this, output_surface_.get(), NULL);
1270 temp_software_renderer->DrawFrame(&frame->render_passes);
1271 } else {
1272 renderer_->DrawFrame(&frame->render_passes);
1273 }
[email protected]c1bb5af2013-03-13 19:06:271274 // The render passes should be consumed by the renderer.
1275 DCHECK(frame->render_passes.empty());
1276 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141277
[email protected]c1bb5af2013-03-13 19:06:271278 // The next frame should start by assuming nothing has changed, and changes
1279 // are noted as they occur.
[email protected]264dc0332013-03-17 21:00:541280 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271281 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1282 DidDrawDamagedArea();
1283 }
1284 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]94f206c12012-08-25 00:09:141285}
1286
[email protected]c1bb5af2013-03-13 19:06:271287void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1288 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1289 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521290
[email protected]c1bb5af2013-03-13 19:06:271291 // Once all layers have been drawn, pending texture uploads should no
1292 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151293 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141294}
1295
[email protected]c1bb5af2013-03-13 19:06:271296void LayerTreeHostImpl::FinishAllRendering() {
1297 if (renderer_)
1298 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141299}
1300
[email protected]c1bb5af2013-03-13 19:06:271301bool LayerTreeHostImpl::IsContextLost() {
1302 DCHECK(proxy_->IsImplThread());
1303 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141304}
1305
[email protected]c1bb5af2013-03-13 19:06:271306const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1307 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141308}
1309
[email protected]e0341352013-04-06 05:01:201310bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1311 if (frame.has_no_damage)
1312 return false;
[email protected]36e5ff12013-06-11 12:19:291313 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161314 active_tree_->ClearLatencyInfo();
1315 return true;
[email protected]94f206c12012-08-25 00:09:141316}
1317
[email protected]c8cbae72013-05-23 10:45:031318void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451319 if (output_surface_)
[email protected]c8cbae72013-05-23 10:45:031320 output_surface_->SetNeedsBeginFrame(enable);
[email protected]7ed47512013-03-26 22:28:451321}
1322
[email protected]94c40e6292013-05-24 22:01:501323float LayerTreeHostImpl::DeviceScaleFactor() const {
1324 return device_scale_factor_;
1325}
1326
[email protected]ffb2720f2013-03-15 19:18:371327gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const {
1328 gfx::SizeF dip_size =
[email protected]f224cc92013-06-06 23:23:321329 gfx::ScaleSize(device_viewport_size(), 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371330
[email protected]ed511b8d2013-03-25 03:29:291331 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371332 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401333 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291334 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371335}
1336
[email protected]c1bb5af2013-03-13 19:06:271337const LayerTreeSettings& LayerTreeHostImpl::Settings() const {
1338 return settings();
[email protected]493067512012-09-19 23:34:101339}
1340
[email protected]c1bb5af2013-03-13 19:06:271341void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011342 // TODO(jamesr): The renderer_ check is needed to make some of the
1343 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1344 // important) in production. We should adjust the test to not need this.
1345 if (renderer_)
1346 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141347}
1348
[email protected]c1bb5af2013-03-13 19:06:271349void LayerTreeHostImpl::Readback(void* pixels,
1350 gfx::Rect rect_in_device_viewport) {
1351 DCHECK(renderer_);
1352 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141353}
1354
[email protected]59adb112013-04-09 04:48:441355bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341356 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011357}
1358
[email protected]c1bb5af2013-03-13 19:06:271359LayerImpl* LayerTreeHostImpl::RootLayer() const {
1360 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081361}
1362
[email protected]c1bb5af2013-03-13 19:06:271363LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1364 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081365}
1366
[email protected]c1bb5af2013-03-13 19:06:271367LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1368 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081369}
1370
[email protected]94f206c12012-08-25 00:09:141371// Content layers can be either directly scrollable or contained in an outer
1372// scrolling layer which applies the scroll transform. Given a content layer,
1373// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271374static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1375 if (!layer_impl)
[email protected]94f206c12012-08-25 00:09:141376 return 0;
[email protected]c1bb5af2013-03-13 19:06:271377
1378 if (layer_impl->scrollable())
1379 return layer_impl;
1380
1381 if (layer_impl->DrawsContent() &&
1382 layer_impl->parent() &&
1383 layer_impl->parent()->scrollable())
1384 return layer_impl->parent();
1385
1386 return 0;
[email protected]94f206c12012-08-25 00:09:141387}
1388
[email protected]c1bb5af2013-03-13 19:06:271389void LayerTreeHostImpl::CreatePendingTree() {
1390 CHECK(!pending_tree_);
1391 if (recycle_tree_)
1392 recycle_tree_.swap(pending_tree_);
1393 else
1394 pending_tree_ = LayerTreeImpl::create(this);
1395 client_->OnCanDrawStateChanged(CanDraw());
1396 client_->OnHasPendingTreeStateChanged(pending_tree_);
1397 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get());
1398 TRACE_EVENT_ASYNC_STEP0("cc",
1399 "PendingTree", pending_tree_.get(), "waiting");
[email protected]2e7ca422012-12-20 02:57:271400}
1401
[email protected]8bb6cfa2013-07-23 00:11:191402void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]c1bb5af2013-03-13 19:06:271403 DCHECK(!client_->IsInsideDraw()) <<
[email protected]8bb6cfa2013-07-23 00:11:191404 "Updating visible tiles within a draw may trigger "
[email protected]c1bb5af2013-03-13 19:06:271405 "spurious redraws.";
[email protected]8bb6cfa2013-07-23 00:11:191406 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1407 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191408
[email protected]8bb6cfa2013-07-23 00:11:191409 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481410}
1411
[email protected]94bf75c2013-06-12 13:20:041412void LayerTreeHostImpl::ActivatePendingTreeIfNeeded() {
1413 DCHECK(pending_tree_);
[email protected]fbe89f72013-05-21 07:24:241414 CHECK(settings_.impl_side_painting);
[email protected]2ae038b2013-01-28 12:52:091415
[email protected]94bf75c2013-06-12 13:20:041416 if (!pending_tree_)
1417 return;
1418
1419 // The tile manager is usually responsible for notifying activation.
1420 // If there is no tile manager, then we need to manually activate.
1421 if (!tile_manager_ || tile_manager_->AreTilesRequiredForActivationReady()) {
1422 ActivatePendingTree();
1423 return;
1424 }
[email protected]615c78a2013-01-24 23:44:161425
[email protected]b67a44e2013-07-12 19:15:101426 // Manage tiles in case state affecting tile priority has changed.
1427 ManageTiles();
1428
[email protected]f6742f52013-05-08 23:52:221429 TRACE_EVENT_ASYNC_STEP1(
[email protected]fcb846d2013-05-22 01:42:361430 "cc",
1431 "PendingTree", pending_tree_.get(), "activate",
1432 "state", TracedValue::FromValue(ActivationStateAsValue().release()));
[email protected]2e7ca422012-12-20 02:57:271433}
1434
[email protected]c1bb5af2013-03-13 19:06:271435void LayerTreeHostImpl::ActivatePendingTree() {
1436 CHECK(pending_tree_);
1437 TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351438
[email protected]1960a712013-04-30 17:06:471439 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271440 active_tree_->PushPersistedState(pending_tree_.get());
1441 if (pending_tree_->needs_full_tree_sync()) {
1442 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331443 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271444 active_tree_->DetachLayerTree(),
1445 active_tree_.get()));
1446 }
[email protected]b5651c22013-03-14 15:06:331447 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271448 active_tree_->root_layer());
1449 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511450
[email protected]c1bb5af2013-03-13 19:06:271451 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511452
[email protected]c1bb5af2013-03-13 19:06:271453 // Now that we've synced everything from the pending tree to the active
1454 // tree, rename the pending tree the recycle tree so we can reuse it on the
1455 // next sync.
1456 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511457
[email protected]1960a712013-04-30 17:06:471458 active_tree_->SetRootLayerScrollOffsetDelegate(
1459 root_layer_scroll_offset_delegate_);
[email protected]c1bb5af2013-03-13 19:06:271460 active_tree_->DidBecomeActive();
[email protected]37386f052013-01-13 00:42:221461
[email protected]c1bb5af2013-03-13 19:06:271462 // Reduce wasted memory now that unlinked resources are guaranteed not
1463 // to be used.
1464 client_->ReduceWastedContentsTextureMemoryOnImplThread();
[email protected]a0b84172013-02-04 08:13:411465
[email protected]c1bb5af2013-03-13 19:06:271466 client_->OnCanDrawStateChanged(CanDraw());
1467 client_->OnHasPendingTreeStateChanged(pending_tree_);
1468 client_->SetNeedsRedrawOnImplThread();
1469 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241470
[email protected]a848c102013-03-26 08:59:091471 if (debug_state_.continuous_painting) {
1472 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431473 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a848c102013-03-26 08:59:091474 paint_time_counter_->SavePaintTime(
[email protected]41abb6d92013-04-26 16:38:501475 stats.total_paint_time + stats.total_record_time +
[email protected]a848c102013-03-26 08:59:091476 stats.total_rasterize_time_for_now_bins_on_pending_tree);
[email protected]c1bb5af2013-03-13 19:06:271477 }
[email protected]2a61ad52013-05-13 14:01:291478
1479 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261480 if (!tree_activation_callback_.is_null())
1481 tree_activation_callback_.Run();
[email protected]2e7ca422012-12-20 02:57:271482}
1483
[email protected]c1bb5af2013-03-13 19:06:271484void LayerTreeHostImpl::SetVisible(bool visible) {
1485 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141486
[email protected]c1bb5af2013-03-13 19:06:271487 if (visible_ == visible)
1488 return;
1489 visible_ = visible;
1490 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121491 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141492
[email protected]c1bb5af2013-03-13 19:06:271493 if (!renderer_)
1494 return;
[email protected]94f206c12012-08-25 00:09:141495
[email protected]c1bb5af2013-03-13 19:06:271496 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141497}
1498
[email protected]206a3922013-05-17 06:34:121499ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
1500 if (!debug_state_.rasterize_only_visible_content)
1501 return managed_memory_policy_;
1502
1503 ManagedMemoryPolicy actual = managed_memory_policy_;
1504 actual.priority_cutoff_when_not_visible =
1505 ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
1506 actual.priority_cutoff_when_visible =
1507 ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
1508 return actual;
1509}
1510
[email protected]50644642013-06-20 13:58:551511void LayerTreeHostImpl::ReleaseTreeResources() {
1512 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551513 SendReleaseResourcesRecursive(active_tree_->root_layer());
1514 if (pending_tree_ && pending_tree_->root_layer())
1515 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1516 if (recycle_tree_ && recycle_tree_->root_layer())
1517 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
1518}
1519
1520void LayerTreeHostImpl::CreateAndSetRenderer(
1521 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201522 ResourceProvider* resource_provider,
1523 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551524 DCHECK(!renderer_);
1525 if (output_surface->capabilities().delegated_rendering) {
1526 renderer_ =
1527 DelegatingRenderer::Create(this, output_surface, resource_provider);
[email protected]50af3c22013-07-13 03:50:201528 } else if (output_surface->context3d() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551529 renderer_ = GLRenderer::Create(this,
1530 output_surface,
1531 resource_provider,
1532 settings_.highp_threshold_min,
1533 settings_.force_direct_layer_drawing);
1534 } else if (output_surface->software_device()) {
1535 renderer_ =
1536 SoftwareRenderer::Create(this, output_surface, resource_provider);
1537 }
1538
[email protected]84975832013-06-29 00:24:111539 if (renderer_) {
[email protected]50644642013-06-20 13:58:551540 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111541 SetFullRootLayerDamage();
1542 }
[email protected]50644642013-06-20 13:58:551543}
1544
[email protected]50af3c22013-07-13 03:50:201545void LayerTreeHostImpl::CreateAndSetTileManager(
1546 ResourceProvider* resource_provider,
1547 bool using_map_image) {
1548 DCHECK(settings_.impl_side_painting);
1549 DCHECK(resource_provider);
1550 tile_manager_ = TileManager::Create(this,
1551 resource_provider,
1552 settings_.num_raster_threads,
1553 rendering_stats_instrumentation_,
1554 using_map_image);
1555 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191556 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201557}
1558
[email protected]50644642013-06-20 13:58:551559void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
1560 if (zero_budget_ == zero_budget)
1561 return;
1562
1563 zero_budget_ = zero_budget;
1564
1565 ManagedMemoryPolicy new_policy = managed_memory_policy_;
1566 if (zero_budget_) {
1567 new_policy.bytes_limit_when_visible = 0;
1568 } else {
1569 new_policy.bytes_limit_when_visible =
1570 PrioritizedResourceManager::DefaultMemoryAllocationLimit();
1571 }
1572 SetManagedMemoryPolicy(new_policy);
1573}
1574
[email protected]c1bb5af2013-03-13 19:06:271575bool LayerTreeHostImpl::InitializeRenderer(
1576 scoped_ptr<OutputSurface> output_surface) {
1577 // Since we will create a new resource provider, we cannot continue to use
1578 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1579 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551580 ReleaseTreeResources();
[email protected]7ba3ca72013-04-11 06:37:251581 if (resource_provider_)
1582 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401583
[email protected]c1bb5af2013-03-13 19:06:271584 // Note: order is important here.
1585 renderer_.reset();
1586 tile_manager_.reset();
1587 resource_provider_.reset();
1588 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141589
[email protected]c1bb5af2013-03-13 19:06:271590 if (!output_surface->BindToClient(this))
1591 return false;
[email protected]be3181652012-09-25 13:02:131592
[email protected]50644642013-06-20 13:58:551593 scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1594 output_surface.get(), settings_.highp_threshold_min);
1595 if (!resource_provider)
1596 return false;
[email protected]2b154b22013-06-07 09:03:271597
[email protected]50644642013-06-20 13:58:551598 if (output_surface->capabilities().deferred_gl_initialization)
1599 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131600
[email protected]50af3c22013-07-13 03:50:201601 bool skip_gl_renderer = false;
1602 CreateAndSetRenderer(
1603 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131604
[email protected]50644642013-06-20 13:58:551605 if (!renderer_)
1606 return false;
[email protected]6123930c2013-05-23 06:48:371607
[email protected]50644642013-06-20 13:58:551608 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201609 CreateAndSetTileManager(resource_provider.get(),
1610 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241611 }
1612
[email protected]049fc7a2013-06-18 12:32:351613 // Setup BeginFrameEmulation if it's not supported natively
1614 if (!settings_.begin_frame_scheduling_enabled) {
1615 const base::TimeDelta display_refresh_interval =
1616 base::TimeDelta::FromMicroseconds(
1617 base::Time::kMicrosecondsPerSecond /
1618 settings_.refresh_rate);
1619
1620 output_surface->InitializeBeginFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151621 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351622 settings_.throttle_frame_production,
1623 display_refresh_interval);
1624 }
1625
1626 int max_frames_pending =
1627 output_surface->capabilities().max_frames_pending;
1628 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351629 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351630 output_surface->SetMaxFramesPending(max_frames_pending);
1631
[email protected]50644642013-06-20 13:58:551632 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271633 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141634
[email protected]c1bb5af2013-03-13 19:06:271635 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211636
[email protected]c1bb5af2013-03-13 19:06:271637 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs
1638 // to be initialized to get max texture size.
1639 active_tree_->set_needs_update_draw_properties();
1640 if (pending_tree_)
1641 pending_tree_->set_needs_update_draw_properties();
[email protected]615c78a2013-01-24 23:44:161642
[email protected]c1bb5af2013-03-13 19:06:271643 return true;
[email protected]94f206c12012-08-25 00:09:141644}
1645
[email protected]2b154b22013-06-07 09:03:271646bool LayerTreeHostImpl::DeferredInitialize(
1647 scoped_refptr<ContextProvider> offscreen_context_provider) {
1648 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551649 DCHECK(settings_.impl_side_painting);
1650 DCHECK(settings_.solid_color_scrollbars);
[email protected]2b154b22013-06-07 09:03:271651 DCHECK(output_surface_->context3d());
1652
[email protected]50644642013-06-20 13:58:551653 ReleaseTreeResources();
1654 renderer_.reset();
[email protected]50af3c22013-07-13 03:50:201655 resource_provider_->InitializeGL();
1656 bool skip_gl_renderer = false;
1657 CreateAndSetRenderer(
1658 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
[email protected]2b154b22013-06-07 09:03:271659
[email protected]50644642013-06-20 13:58:551660 bool success = !!renderer_.get();
[email protected]2b154b22013-06-07 09:03:271661 client_->DidTryInitializeRendererOnImplThread(success,
1662 offscreen_context_provider);
[email protected]50644642013-06-20 13:58:551663 if (success) {
1664 EnforceZeroBudget(false);
1665 client_->SetNeedsCommitOnImplThread();
1666 }
[email protected]2b154b22013-06-07 09:03:271667 return success;
1668}
1669
[email protected]50af3c22013-07-13 03:50:201670void LayerTreeHostImpl::ReleaseGL() {
1671 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1672 DCHECK(settings_.impl_side_painting);
1673 DCHECK(settings_.solid_color_scrollbars);
1674 DCHECK(output_surface_->context3d());
1675
1676 ReleaseTreeResources();
1677 renderer_.reset();
1678 tile_manager_.reset();
1679 resource_provider_->InitializeSoftware();
1680
1681 bool skip_gl_renderer = true;
1682 CreateAndSetRenderer(
1683 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1684 DCHECK(renderer_);
1685
1686 EnforceZeroBudget(true);
1687 CreateAndSetTileManager(resource_provider_.get(),
1688 GetRendererCapabilities().using_map_image);
1689 DCHECK(tile_manager_);
1690
1691 bool success = true;
1692 client_->DidTryInitializeRendererOnImplThread(
1693 success, scoped_refptr<ContextProvider>());
1694 client_->SetNeedsCommitOnImplThread();
1695}
1696
[email protected]18ce59702013-04-09 04:58:401697void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1698 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271699 return;
[email protected]94f206c12012-08-25 00:09:141700
[email protected]c1bb5af2013-03-13 19:06:271701 if (pending_tree_ && device_viewport_size_ != device_viewport_size)
1702 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271703
[email protected]c1bb5af2013-03-13 19:06:271704 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141705
[email protected]c1bb5af2013-03-13 19:06:271706 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141707
[email protected]c1bb5af2013-03-13 19:06:271708 if (renderer_)
1709 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211710
[email protected]c1bb5af2013-03-13 19:06:271711 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441712 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141713}
1714
[email protected]d9083762013-03-24 01:36:401715void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1716 if (overdraw_bottom_height == overdraw_bottom_height_)
1717 return;
1718 overdraw_bottom_height_ = overdraw_bottom_height;
1719
1720 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441721 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401722}
1723
[email protected]c1bb5af2013-03-13 19:06:271724void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1725 if (device_scale_factor == device_scale_factor_)
1726 return;
1727 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271728
[email protected]94c40e6292013-05-24 22:01:501729 if (renderer_)
1730 renderer_->ViewportChanged();
1731
[email protected]c1bb5af2013-03-13 19:06:271732 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441733 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141734}
1735
[email protected]f224cc92013-06-06 23:23:321736gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1737 if (external_viewport_.IsEmpty())
1738 return gfx::Rect(device_viewport_size_);
1739
1740 return external_viewport_;
1741}
1742
1743const gfx::Transform& LayerTreeHostImpl::DeviceTransform() const {
1744 return external_transform_;
1745}
1746
[email protected]c1bb5af2013-03-13 19:06:271747void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1748 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141749}
1750
[email protected]59adb112013-04-09 04:48:441751void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271752 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441753 active_tree_->set_needs_update_draw_properties();
1754 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141755}
1756
[email protected]c1bb5af2013-03-13 19:06:271757bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041758 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341759 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141760}
1761
[email protected]200a9c062013-05-20 04:34:371762void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1763 DCHECK(input_handler_client_ == NULL);
1764 input_handler_client_ = client;
1765}
1766
[email protected]5ff3c9782013-04-29 17:35:121767InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1768 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
[email protected]ed511b8d2013-03-25 03:29:291769 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
[email protected]94f206c12012-08-25 00:09:141770
[email protected]c1bb5af2013-03-13 19:06:271771 if (top_controls_manager_)
1772 top_controls_manager_->ScrollBegin();
[email protected]3ba4cae2013-01-16 03:58:381773
[email protected]c1bb5af2013-03-13 19:06:271774 DCHECK(!CurrentlyScrollingLayer());
1775 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:141776
[email protected]c1bb5af2013-03-13 19:06:271777 if (!EnsureRenderSurfaceLayerList())
[email protected]94f206c12012-08-25 00:09:141778 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141779
[email protected]c1bb5af2013-03-13 19:06:271780 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1781 device_scale_factor_);
[email protected]94f206c12012-08-25 00:09:141782
[email protected]c1bb5af2013-03-13 19:06:271783 // First find out which layer was hit from the saved list of visible layers
1784 // in the most recent frame.
[email protected]6ba914122013-03-22 16:26:391785 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
[email protected]c1bb5af2013-03-13 19:06:271786 device_viewport_point, active_tree_->RenderSurfaceLayerList());
[email protected]31bfe272012-10-19 18:49:521787
[email protected]c1bb5af2013-03-13 19:06:271788 // Walk up the hierarchy and look for a scrollable layer.
1789 LayerImpl* potentially_scrolling_layer_impl = 0;
1790 for (; layer_impl; layer_impl = layer_impl->parent()) {
1791 // The content layer can also block attempts to scroll outside the main
1792 // thread.
1793 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1794 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431795 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271796 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1797 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141798 }
1799
[email protected]c1bb5af2013-03-13 19:06:271800 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1801 if (!scroll_layer_impl)
1802 continue;
[email protected]94f206c12012-08-25 00:09:141803
[email protected]c1bb5af2013-03-13 19:06:271804 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]94f206c12012-08-25 00:09:141805
[email protected]c1bb5af2013-03-13 19:06:271806 // If any layer wants to divert the scroll event to the main thread, abort.
1807 if (status == ScrollOnMainThread) {
[email protected]372bad5f2013-03-21 16:38:431808 rendering_stats_instrumentation_->IncrementMainThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271809 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1810 return ScrollOnMainThread;
[email protected]94f206c12012-08-25 00:09:141811 }
1812
[email protected]c1bb5af2013-03-13 19:06:271813 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1814 potentially_scrolling_layer_impl = scroll_layer_impl;
1815 }
1816
1817 // When hiding top controls is enabled and the controls are hidden or
1818 // overlaying the content, force scrolls to be enabled on the root layer to
1819 // allow bringing the top controls back into view.
1820 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1821 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521822 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271823 potentially_scrolling_layer_impl = RootScrollLayer();
1824 }
1825
1826 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201827 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271828 potentially_scrolling_layer_impl);
1829 should_bubble_scrolls_ = (type != NonBubblingGesture);
1830 wheel_scrolling_ = (type == Wheel);
[email protected]372bad5f2013-03-21 16:38:431831 rendering_stats_instrumentation_->IncrementImplThreadScrolls();
[email protected]c1bb5af2013-03-13 19:06:271832 client_->RenewTreePriority();
1833 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
1834 return ScrollStarted;
1835 }
1836 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:141837}
1838
[email protected]c1bb5af2013-03-13 19:06:271839gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
1840 LayerImpl* layer_impl,
1841 float scale_from_viewport_to_screen_space,
1842 gfx::PointF viewport_point,
1843 gfx::Vector2dF viewport_delta) {
1844 // Layers with non-invertible screen space transforms should not have passed
1845 // the scroll hit test in the first place.
1846 DCHECK(layer_impl->screen_space_transform().IsInvertible());
1847 gfx::Transform inverse_screen_space_transform(
1848 gfx::Transform::kSkipInitialization);
1849 bool did_invert = layer_impl->screen_space_transform().GetInverse(
1850 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:351851 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
1852 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:271853 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:141854
[email protected]c1bb5af2013-03-13 19:06:271855 gfx::PointF screen_space_point =
1856 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:141857
[email protected]c1bb5af2013-03-13 19:06:271858 gfx::Vector2dF screen_space_delta = viewport_delta;
1859 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
1860
1861 // First project the scroll start and end points to local layer space to find
1862 // the scroll delta in layer coordinates.
1863 bool start_clipped, end_clipped;
1864 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
1865 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:211866 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271867 screen_space_point,
[email protected]fa816c62013-03-18 04:24:211868 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:271869 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:211870 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:271871 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:211872 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271873
1874 // In general scroll point coordinates should not get clipped.
1875 DCHECK(!start_clipped);
1876 DCHECK(!end_clipped);
1877 if (start_clipped || end_clipped)
1878 return gfx::Vector2dF();
1879
1880 // local_start_point and local_end_point are in content space but we want to
1881 // move them to layer space for scrolling.
1882 float width_scale = 1.f / layer_impl->contents_scale_x();
1883 float height_scale = 1.f / layer_impl->contents_scale_y();
1884 local_start_point.Scale(width_scale, height_scale);
1885 local_end_point.Scale(width_scale, height_scale);
1886
1887 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:471888 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:271889 layer_impl->ScrollBy(local_end_point - local_start_point);
1890
1891 // Get the end point in the layer's content space so we can apply its
1892 // ScreenSpaceTransform.
1893 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:471894 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:271895 previous_delta;
1896 gfx::PointF actual_local_content_end_point =
1897 gfx::ScalePoint(actual_local_end_point,
1898 1.f / width_scale,
1899 1.f / height_scale);
1900
1901 // Calculate the applied scroll delta in viewport space coordinates.
1902 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:211903 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:271904 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:211905 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:271906 DCHECK(!end_clipped);
1907 if (end_clipped)
1908 return gfx::Vector2dF();
1909 gfx::PointF actual_viewport_end_point =
1910 gfx::ScalePoint(actual_screen_space_end_point,
1911 1.f / scale_from_viewport_to_screen_space);
1912 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:141913}
1914
[email protected]c1bb5af2013-03-13 19:06:271915static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
1916 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:471917 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:271918 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:471919 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:271920}
1921
1922bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
1923 gfx::Vector2dF scroll_delta) {
1924 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
1925 if (!CurrentlyScrollingLayer())
1926 return false;
1927
1928 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:531929 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:291930 bool did_scroll_x = false;
1931 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:421932 bool consume_by_top_controls = top_controls_manager_ &&
1933 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:061934
[email protected]c1bb5af2013-03-13 19:06:271935 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
1936 layer_impl;
1937 layer_impl = layer_impl->parent()) {
1938 if (!layer_impl->scrollable())
1939 continue;
1940
[email protected]a2b5ded2013-05-20 21:32:531941 if (layer_impl == RootScrollLayer()) {
1942 // Only allow bubble scrolling when the scroll is in the direction to make
1943 // the top controls visible.
1944 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
1945 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
1946 UpdateMaxScrollOffset();
1947 }
1948 // Track root layer deltas for reporting overscroll.
1949 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:421950 }
1951
[email protected]c1bb5af2013-03-13 19:06:271952 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:271953 // Gesture events need to be transformed from viewport coordinates to local
1954 // layer coordinates so that the scrolling contents exactly follow the
1955 // user's finger. In contrast, wheel events represent a fixed amount of
1956 // scrolling so we can just apply them directly.
1957 if (!wheel_scrolling_) {
1958 float scale_from_viewport_to_screen_space = device_scale_factor_;
1959 applied_delta =
1960 ScrollLayerWithViewportSpaceDelta(layer_impl,
1961 scale_from_viewport_to_screen_space,
1962 viewport_point, pending_delta);
1963 } else {
1964 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:141965 }
[email protected]94f206c12012-08-25 00:09:141966
[email protected]c1bb5af2013-03-13 19:06:271967 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:291968 float move_threshold = 0.1f;
1969 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
1970 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
1971 did_scroll_x |= did_move_layer_x;
1972 did_scroll_y |= did_move_layer_y;
1973 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]c1bb5af2013-03-13 19:06:271974 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
1975 continue;
1976 else
1977 break;
[email protected]94f206c12012-08-25 00:09:141978 }
[email protected]a2b5ded2013-05-20 21:32:531979
1980 if (layer_impl == RootScrollLayer())
1981 unused_root_delta.Subtract(applied_delta);
1982
[email protected]c1bb5af2013-03-13 19:06:271983 did_lock_scrolling_layer_ = true;
1984 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:201985 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:271986 break;
[email protected]94f206c12012-08-25 00:09:141987 }
[email protected]94f206c12012-08-25 00:09:141988
[email protected]c1bb5af2013-03-13 19:06:271989 // If the applied delta is within 45 degrees of the input delta, bail out to
1990 // make it easier to scroll just one layer in one direction without
1991 // affecting any of its parents.
1992 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:211993 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:271994 applied_delta, pending_delta) < angle_threshold) {
1995 pending_delta = gfx::Vector2d();
1996 break;
[email protected]4a23c374c2012-12-08 08:38:551997 }
[email protected]c1bb5af2013-03-13 19:06:271998
1999 // Allow further movement only on an axis perpendicular to the direction in
2000 // which the layer moved.
2001 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212002 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272003
[email protected]a2b5ded2013-05-20 21:32:532004 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272005 break;
2006 }
2007
[email protected]2bd503f2013-07-23 05:35:292008 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272009 if (did_scroll) {
2010 client_->SetNeedsCommitOnImplThread();
2011 client_->SetNeedsRedrawOnImplThread();
2012 client_->RenewTreePriority();
2013 }
[email protected]a2b5ded2013-05-20 21:32:532014
[email protected]2bd503f2013-07-23 05:35:292015 // Scrolling along an axis resets accumulated root overscroll for that axis.
2016 if (did_scroll_x)
2017 accumulated_root_overscroll_.set_x(0);
2018 if (did_scroll_y)
2019 accumulated_root_overscroll_.set_y(0);
2020
[email protected]a2b5ded2013-05-20 21:32:532021 accumulated_root_overscroll_ += unused_root_delta;
2022 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2023 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112024 DidOverscrollParams params;
2025 params.accumulated_overscroll = accumulated_root_overscroll_;
2026 params.latest_overscroll_delta = unused_root_delta;
2027 params.current_fling_velocity = current_fling_velocity_;
2028 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532029 }
2030
[email protected]c1bb5af2013-03-13 19:06:272031 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552032}
2033
[email protected]be782f52013-03-23 21:36:142034// This implements scrolling by page as described here:
2035// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2036// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492037bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2038 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142039 DCHECK(wheel_scrolling_);
2040
2041 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2042 layer_impl;
2043 layer_impl = layer_impl->parent()) {
2044 if (!layer_impl->scrollable())
2045 continue;
2046
2047 if (!layer_impl->vertical_scrollbar_layer())
2048 continue;
2049
2050 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2051
2052 // These magical values match WebKit and are designed to scroll nearly the
2053 // entire visible content height but leave a bit of overlap.
2054 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492055 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142056 page = -page;
2057
2058 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2059
2060 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2061
2062 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142063 client_->SetNeedsCommitOnImplThread();
2064 client_->SetNeedsRedrawOnImplThread();
2065 client_->RenewTreePriority();
2066 return true;
2067 }
2068
2069 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2070 }
2071
2072 return false;
2073}
2074
[email protected]1960a712013-04-30 17:06:472075void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2076 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2077 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2078 active_tree_->SetRootLayerScrollOffsetDelegate(
2079 root_layer_scroll_offset_delegate_);
2080}
2081
2082void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2083 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2084 client_->SetNeedsCommitOnImplThread();
2085}
2086
[email protected]c1bb5af2013-03-13 19:06:272087void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2088 active_tree_->ClearCurrentlyScrollingLayer();
2089 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532090 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202091 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142092}
2093
[email protected]c1bb5af2013-03-13 19:06:272094void LayerTreeHostImpl::ScrollEnd() {
2095 if (top_controls_manager_)
2096 top_controls_manager_->ScrollEnd();
2097 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052098 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142099}
2100
[email protected]5ff3c9782013-04-29 17:35:122101InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]7c45d8152013-04-23 18:27:212102 if (active_tree_->CurrentlyScrollingLayer())
2103 return ScrollStarted;
2104
2105 return ScrollIgnored;
2106}
2107
[email protected]6e921bd2013-04-29 21:10:202108void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2109 current_fling_velocity_ = velocity;
2110}
2111
[email protected]c1bb5af2013-03-13 19:06:272112void LayerTreeHostImpl::PinchGestureBegin() {
2113 pinch_gesture_active_ = true;
2114 previous_pinch_anchor_ = gfx::Point();
2115 client_->RenewTreePriority();
[email protected]94f206c12012-08-25 00:09:142116}
2117
[email protected]c1bb5af2013-03-13 19:06:272118void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2119 gfx::Point anchor) {
2120 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282121
[email protected]c1bb5af2013-03-13 19:06:272122 if (!RootScrollLayer())
2123 return;
[email protected]d3afa112012-12-08 06:24:282124
[email protected]c1bb5af2013-03-13 19:06:272125 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2126 // position over the course of the magnify.
2127 float page_scale_delta = active_tree_->page_scale_delta();
2128 gfx::PointF previous_scale_anchor =
2129 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2130 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2131 page_scale_delta = active_tree_->page_scale_delta();
2132 gfx::PointF new_scale_anchor =
2133 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2134 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2135
2136 previous_pinch_anchor_ = anchor;
2137
2138 move.Scale(1 / active_tree_->page_scale_factor());
2139
2140 RootScrollLayer()->ScrollBy(move);
2141
[email protected]c1bb5af2013-03-13 19:06:272142 client_->SetNeedsCommitOnImplThread();
2143 client_->SetNeedsRedrawOnImplThread();
2144 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282145}
2146
[email protected]c1bb5af2013-03-13 19:06:272147void LayerTreeHostImpl::PinchGestureEnd() {
2148 pinch_gesture_active_ = false;
[email protected]c1bb5af2013-03-13 19:06:272149 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142150}
2151
[email protected]c1bb5af2013-03-13 19:06:272152static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2153 LayerImpl* layer_impl) {
2154 if (!layer_impl)
2155 return;
[email protected]94f206c12012-08-25 00:09:142156
[email protected]c1bb5af2013-03-13 19:06:272157 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472158 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272159 if (!scroll_delta.IsZero()) {
2160 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392161 scroll.layer_id = layer_impl->id();
2162 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272163 scroll_info->scrolls.push_back(scroll);
2164 layer_impl->SetSentScrollDelta(scroll_delta);
2165 }
[email protected]94f206c12012-08-25 00:09:142166
[email protected]c1bb5af2013-03-13 19:06:272167 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2168 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142169}
2170
[email protected]c1bb5af2013-03-13 19:06:272171scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2172 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302173
[email protected]c1bb5af2013-03-13 19:06:272174 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392175 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2176 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302177
[email protected]c1bb5af2013-03-13 19:06:272178 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302179}
2180
[email protected]c1bb5af2013-03-13 19:06:272181void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]878705be2013-04-15 22:44:022182 SetViewportDamage(gfx::Rect(device_viewport_size_));
[email protected]829ad972013-01-28 23:36:102183}
2184
[email protected]c1bb5af2013-03-13 19:06:272185void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2186 if (!page_scale_animation_ || !RootScrollLayer())
2187 return;
2188
2189 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2190 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472191 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272192
2193 active_tree_->SetPageScaleDelta(
2194 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2195 active_tree_->page_scale_factor());
2196 gfx::Vector2dF next_scroll =
2197 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2198
2199 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2200 client_->SetNeedsRedrawOnImplThread();
2201
2202 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2203 page_scale_animation_.reset();
2204 client_->SetNeedsCommitOnImplThread();
2205 client_->RenewTreePriority();
2206 }
[email protected]829ad972013-01-28 23:36:102207}
2208
[email protected]ffb2720f2013-03-15 19:18:372209void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2210 if (!top_controls_manager_ || !RootScrollLayer())
2211 return;
2212 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2213 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172214 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2215 return;
[email protected]ffb2720f2013-03-15 19:18:372216 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2217 scroll, 1.f / active_tree_->total_page_scale_factor()));
2218}
2219
[email protected]c1bb5af2013-03-13 19:06:272220void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2221 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522222 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272223 animation_registrar_->active_animation_controllers().empty() ||
2224 !active_tree_->root_layer())
2225 return;
2226
2227 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2228
2229 last_animation_time_ = wall_clock_time;
2230 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2231
2232 AnimationRegistrar::AnimationControllerMap copy =
2233 animation_registrar_->active_animation_controllers();
2234 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2235 iter != copy.end();
2236 ++iter)
2237 (*iter).second->Animate(monotonic_seconds);
2238
2239 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082240}
2241
[email protected]3d9f7432013-04-06 00:35:182242void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522243 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272244 animation_registrar_->active_animation_controllers().empty() ||
2245 !active_tree_->root_layer())
2246 return;
2247
2248 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2249 scoped_ptr<AnimationEventsVector> events =
2250 make_scoped_ptr(new AnimationEventsVector);
2251 AnimationRegistrar::AnimationControllerMap copy =
2252 animation_registrar_->active_animation_controllers();
2253 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2254 iter != copy.end();
2255 ++iter)
[email protected]3d9f7432013-04-06 00:35:182256 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272257
2258 if (!events->empty()) {
2259 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2260 last_animation_time_);
2261 }
[email protected]131a0c22013-02-12 18:31:082262}
2263
[email protected]c1bb5af2013-03-13 19:06:272264base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2265 return base::TimeDelta::FromSeconds(1);
2266}
2267
[email protected]50644642013-06-20 13:58:552268void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352269 DCHECK(current);
[email protected]50644642013-06-20 13:58:552270 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352271 current->DidLoseOutputSurface();
2272 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552273 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352274 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552275 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352276 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552277 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352278}
2279
[email protected]c1bb5af2013-03-13 19:06:272280std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2281 std::string str;
2282 if (active_tree_->root_layer()) {
2283 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2284 base::JSONWriter::WriteWithOptions(
2285 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2286 }
2287 return str;
2288}
2289
[email protected]c1bb5af2013-03-13 19:06:272290int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362291 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272292}
2293
[email protected]c1bb5af2013-03-13 19:06:272294void LayerTreeHostImpl::SendManagedMemoryStats(
2295 size_t memory_visible_bytes,
2296 size_t memory_visible_and_nearby_bytes,
2297 size_t memory_use_bytes) {
2298 if (!renderer_)
2299 return;
2300
2301 // Round the numbers being sent up to the next 8MB, to throttle the rate
2302 // at which we spam the GPU process.
2303 static const size_t rounding_step = 8 * 1024 * 1024;
2304 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2305 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2306 rounding_step);
2307 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2308 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2309 last_sent_memory_visible_and_nearby_bytes_ ==
2310 memory_visible_and_nearby_bytes &&
2311 last_sent_memory_use_bytes_ == memory_use_bytes) {
2312 return;
2313 }
2314 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2315 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2316 last_sent_memory_use_bytes_ = memory_use_bytes;
2317
2318 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2319 last_sent_memory_visible_and_nearby_bytes_,
2320 last_sent_memory_use_bytes_);
2321}
2322
2323void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2324 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2325}
2326
2327void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2328 base::TimeTicks time) {
2329 if (!layer)
2330 return;
2331
2332 ScrollbarAnimationController* scrollbar_controller =
2333 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352334 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202335 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422336 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2337 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272338 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202339 }
[email protected]c1bb5af2013-03-13 19:06:272340
2341 for (size_t i = 0; i < layer->children().size(); ++i)
2342 AnimateScrollbarsRecursive(layer->children()[i], time);
2343}
2344
[email protected]21c9dee72013-06-15 01:20:052345void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202346 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052347 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202348}
2349
2350void LayerTreeHostImpl::StartScrollbarAnimationRecursive(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->IsAnimating()) {
2358 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202359 if (delay > base::TimeDelta())
2360 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352361 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202362 client_->SetNeedsRedrawOnImplThread();
2363 }
2364
2365 for (size_t i = 0; i < layer->children().size(); ++i)
2366 StartScrollbarAnimationRecursive(layer->children()[i], time);
2367}
2368
[email protected]c1bb5af2013-03-13 19:06:272369void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2370 if (!tile_manager_)
2371 return;
2372
2373 GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState());
2374 if (new_state.tree_priority == priority)
2375 return;
2376
2377 new_state.tree_priority = priority;
2378 tile_manager_->SetGlobalState(new_state);
[email protected]a23451e2013-06-07 20:58:262379 manage_tiles_needed_ = true;
[email protected]c1bb5af2013-03-13 19:06:272380}
2381
[email protected]8347d692013-05-17 23:22:382382void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552383 current_frame_timeticks_ = base::TimeTicks();
2384 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272385}
2386
[email protected]21c9dee72013-06-15 01:20:052387void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2388 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552389 if (ticks->is_null()) {
2390 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052391 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552392 *now = base::Time::Now();
2393 }
2394}
2395
2396base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2397 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2398 return current_frame_timeticks_;
2399}
2400
2401base::Time LayerTreeHostImpl::CurrentFrameTime() {
2402 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272403 return current_frame_time_;
2404}
2405
[email protected]21c9dee72013-06-15 01:20:052406base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
2407 return base::TimeTicks::Now();
2408}
2409
[email protected]c1bb5af2013-03-13 19:06:272410scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const {
2411 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222412 if (this->pending_tree_)
2413 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272414 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212415 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272416 if (tile_manager_)
2417 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2418 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222419 if (pending_tree_)
2420 state->Set("pending_tree", pending_tree_->AsValue().release());
2421 return state.PassAs<base::Value>();
2422}
2423
2424scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222425 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2426 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472427 if (tile_manager_)
2428 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272429 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082430}
2431
[email protected]6e7fdeb2013-07-09 14:28:382432void LayerTreeHostImpl::SetDebugState(
2433 const LayerTreeDebugState& new_debug_state) {
2434 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2435 return;
2436 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272437 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242438
[email protected]6e7fdeb2013-07-09 14:28:382439 debug_state_ = new_debug_state;
2440 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022441}
2442
[email protected]d3143c732012-10-05 19:17:592443} // namespace cc