blob: d79b0855044a71f1bb1ff867bd0dd447e4d699d0 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
[email protected]b6eb8e332013-09-10 00:51:018#include <limits>
[email protected]ac7c7f52012-11-08 06:26:509
[email protected]ad5d1422012-10-19 13:40:2910#include "base/basictypes.h"
[email protected]34806722013-08-09 23:51:2111#include "base/containers/hash_tables.h"
[email protected]4a23c374c2012-12-08 08:38:5512#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1413#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3414#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4815#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0916#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4017#include "cc/animation/timing_function.h"
[email protected]6be422b2013-12-08 06:47:3118#include "cc/base/latency_info_swap_promise_monitor.h"
[email protected]681ccff2013-03-18 06:13:5219#include "cc/base/math_util.h"
20#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3321#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2722#include "cc/debug/debug_rect_history.h"
[email protected]dbe759a2013-12-02 19:23:0223#include "cc/debug/devtools_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2724#include "cc/debug/frame_rate_counter.h"
25#include "cc/debug/overdraw_metrics.h"
26#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4327#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2228#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2129#include "cc/input/page_scale_animation.h"
30#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/append_quads_data.h"
32#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2833#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5234#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1735#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2836#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0237#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3038#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4539#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3040#include "cc/output/delegating_renderer.h"
41#include "cc/output/gl_renderer.h"
42#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5643#include "cc/quads/render_pass_draw_quad.h"
44#include "cc/quads/shared_quad_state.h"
45#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1246#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4047#include "cc/resources/memory_history.h"
48#include "cc/resources/picture_layer_tiling.h"
49#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1150#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5751#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3152#include "cc/scheduler/delay_based_time_source.h"
[email protected]556fd292013-03-18 08:03:0453#include "cc/trees/damage_tracker.h"
54#include "cc/trees/layer_tree_host.h"
55#include "cc/trees/layer_tree_host_common.h"
56#include "cc/trees/layer_tree_impl.h"
57#include "cc/trees/quad_culler.h"
58#include "cc/trees/single_thread_proxy.h"
59#include "cc/trees/tree_synchronizer.h"
[email protected]8d7aa012013-11-23 21:19:1260#include "gpu/GLES2/gl2extchromium.h"
[email protected]de2cf8c2013-10-25 19:46:4661#include "ui/gfx/frame_time.h"
[email protected]d455d552012-11-02 00:19:0662#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1363#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1464
[email protected]94f206c12012-08-25 00:09:1465namespace {
66
[email protected]c1bb5af2013-03-13 19:06:2767void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
68 if (visible) {
69 TRACE_EVENT_ASYNC_BEGIN1("webkit",
70 "LayerTreeHostImpl::SetVisible",
71 id,
72 "LayerTreeHostImpl",
73 id);
74 return;
75 }
[email protected]94f206c12012-08-25 00:09:1476
[email protected]c1bb5af2013-03-13 19:06:2777 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1478}
79
[email protected]b6eb8e332013-09-10 00:51:0180size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
[email protected]b4664142013-11-08 00:50:3181 // Software compositing should not use this value in production. Just use a
82 // default value when testing uploads with the software compositor.
83 if (!context_provider)
[email protected]b6eb8e332013-09-10 00:51:0184 return std::numeric_limits<size_t>::max();
[email protected]b4664142013-11-08 00:50:3185
86 // We want to make sure the default transfer buffer size is equal to the
87 // amount of data that can be uploaded by the compositor to avoid stalling
88 // the pipeline.
89 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
90 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
91 // Assuming a two frame deep pipeline between CPU and GPU and we are
92 // drawing 60 frames per second which would require us to draw one
93 // frame in 16 milliseconds.
94 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
95 return std::min(
96 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
97 kMaxTransferBufferUsageBytes);
98}
99
100size_t GetMaxRasterTasksUsageBytes(cc::ContextProvider* context_provider) {
101 // Transfer-buffer/raster-tasks limits are different but related. We make
102 // equal here, as this is ideal when using transfer buffers. When not using
103 // transfer buffers we should still limit raster to something similar, to
104 // preserve caching behavior (and limit memory waste when priorities change).
105 return GetMaxTransferBufferUsageBytes(context_provider);
[email protected]b6eb8e332013-09-10 00:51:01106}
107
[email protected]00d92d32014-01-23 09:46:50108unsigned GetMapImageTextureTarget(cc::ContextProvider* context_provider) {
[email protected]8d7aa012013-11-23 21:19:12109 if (!context_provider)
110 return GL_TEXTURE_2D;
111
112 // TODO(reveman): Determine if GL_TEXTURE_EXTERNAL_OES works well on
113 // Android before we enable this. crbug.com/322780
114#if !defined(OS_ANDROID)
[email protected]900db4412014-01-12 02:19:01115 if (context_provider->ContextCapabilities().gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12116 return GL_TEXTURE_EXTERNAL_OES;
[email protected]900db4412014-01-12 02:19:01117 if (context_provider->ContextCapabilities().gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12118 return GL_TEXTURE_RECTANGLE_ARB;
119#endif
120
121 return GL_TEXTURE_2D;
122}
123
[email protected]c1bb5af2013-03-13 19:06:27124} // namespace
[email protected]94f206c12012-08-25 00:09:14125
[email protected]9c88e562012-09-14 22:21:30126namespace cc {
[email protected]94f206c12012-08-25 00:09:14127
[email protected]96baf3e2012-10-22 23:09:55128class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27129 public:
130 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
131 LayerTreeHostImpl* layer_tree_host_impl,
132 scoped_refptr<DelayBasedTimeSource> time_source) {
133 return make_scoped_ptr(
134 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
135 time_source));
136 }
137 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26138 time_source_->SetClient(NULL);
139 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27140 }
141
[email protected]6d0e69d2013-03-20 14:53:26142 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27143 // In single threaded mode we attempt to simulate changing the current
144 // thread by maintaining a fake thread id. When we switch from one
145 // thread to another, we construct DebugScopedSetXXXThread objects that
146 // update the thread id. This lets DCHECKS that ensure we're on the
147 // right thread to work correctly in single threaded mode. The problem
148 // here is that the timer tasks are run via the message loop, and when
149 // they run, we've had no chance to construct a DebugScopedSetXXXThread
150 // object. The result is that we report that we're running on the main
151 // thread. In multi-threaded mode, this timer is run on the compositor
152 // thread, so to keep this consistent in single-threaded mode, we'll
153 // construct a DebugScopedSetImplThread object. There is no need to do
154 // this in multi-threaded mode since the real thread id's will be
155 // correct. In fact, setting fake thread id's interferes with the real
156 // thread id's and causes breakage.
157 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
158 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
159 set_impl_thread.reset(
160 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14161 }
162
[email protected]94bf75c2013-06-12 13:20:04163 // TODO(enne): This should probably happen post-animate.
164 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38165 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
166 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04167 }
168
[email protected]fb7425a2013-04-22 16:28:55169 layer_tree_host_impl_->Animate(
170 layer_tree_host_impl_->CurrentFrameTimeTicks(),
171 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18172 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
173 bool start_ready_animations = true;
174 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38175 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27176 }
[email protected]373974232013-01-10 22:20:50177
[email protected]c1bb5af2013-03-13 19:06:27178 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26179 if (active != time_source_->Active())
180 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27181 }
[email protected]94f206c12012-08-25 00:09:14182
[email protected]d9fce6722013-08-30 01:10:01183 bool Active() const { return time_source_->Active(); }
184
[email protected]c1bb5af2013-03-13 19:06:27185 private:
186 LayerTreeHostImplTimeSourceAdapter(
187 LayerTreeHostImpl* layer_tree_host_impl,
188 scoped_refptr<DelayBasedTimeSource> time_source)
189 : layer_tree_host_impl_(layer_tree_host_impl),
190 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26191 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27192 }
[email protected]94f206c12012-08-25 00:09:14193
[email protected]c1bb5af2013-03-13 19:06:27194 LayerTreeHostImpl* layer_tree_host_impl_;
195 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14196
[email protected]c1bb5af2013-03-13 19:06:27197 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14198};
199
[email protected]96baf3e2012-10-22 23:09:55200LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20201 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27202
203LayerTreeHostImpl::FrameData::~FrameData() {}
204
205scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
206 const LayerTreeSettings& settings,
207 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43208 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57209 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02210 SharedBitmapManager* manager,
211 int id) {
[email protected]a7f35682013-10-22 23:05:57212 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02213 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10214}
215
[email protected]372bad5f2013-03-21 16:38:43216LayerTreeHostImpl::LayerTreeHostImpl(
217 const LayerTreeSettings& settings,
218 LayerTreeHostImplClient* client,
219 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57220 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02221 SharedBitmapManager* manager,
222 int id)
[email protected]c1bb5af2013-03-13 19:06:27223 : client_(client),
224 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37225 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27226 did_lock_scrolling_layer_(false),
227 should_bubble_scrolls_(false),
228 wheel_scrolling_(false),
[email protected]f620b0e72013-10-01 21:38:24229 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08230 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47231 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27232 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27233 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07234 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34235 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29236 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07237 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27238 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13239 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36240 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29241 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27242 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02243 debug_rect_history_(DebugRectHistory::Create()),
[email protected]ea468c6c2013-09-10 08:25:11244 texture_mailbox_deleter_(new TextureMailboxDeleter),
[email protected]d7626ffd2013-03-29 00:17:42245 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27246 last_sent_memory_visible_bytes_(0),
247 last_sent_memory_visible_and_nearby_bytes_(0),
248 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55249 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32250 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12251 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32252 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28253 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17254 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43255 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19256 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33257 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57258 need_to_update_visible_tiles_before_draw_(false),
[email protected]ce2e8112013-11-28 07:44:36259#ifndef NDEBUG
260 did_lose_called_(false),
261#endif
[email protected]dbe759a2013-12-02 19:23:02262 shared_bitmap_manager_(manager),
263 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27264 DCHECK(proxy_->IsImplThread());
265 DidVisibilityChange(this, visible_);
266
[email protected]8e0176d2013-03-21 03:14:52267 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27268
[email protected]8e0176d2013-03-21 03:14:52269 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27270 top_controls_manager_ =
271 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52272 settings.top_controls_height,
273 settings.top_controls_show_threshold,
274 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27275 }
276
[email protected]8e0176d2013-03-21 03:14:52277 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27278
279 // LTHI always has an active tree.
280 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51281 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
282 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10283}
284
[email protected]c1bb5af2013-03-13 19:06:27285LayerTreeHostImpl::~LayerTreeHostImpl() {
286 DCHECK(proxy_->IsImplThread());
287 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51288 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
289 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27290
[email protected]200a9c062013-05-20 04:34:37291 if (input_handler_client_) {
292 input_handler_client_->WillShutdown();
293 input_handler_client_ = NULL;
294 }
295
[email protected]75deb742013-06-24 20:19:18296 // The layer trees must be destroyed before the layer tree host. We've
297 // made a contract with our animation controllers that the registrar
298 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40299 if (recycle_tree_)
300 recycle_tree_->Shutdown();
301 if (pending_tree_)
302 pending_tree_->Shutdown();
303 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18304 recycle_tree_.reset();
305 pending_tree_.reset();
306 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14307}
308
[email protected]097a0cbf2013-12-12 17:04:58309void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
310 // If the begin frame data was handled, then scroll and scale set was applied
311 // by the main thread, so the active tree needs to be updated as if these sent
312 // values were applied and committed.
313 if (did_handle) {
314 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
315 active_tree_->ResetContentsTexturesPurged();
316 }
317}
318
[email protected]c1bb5af2013-03-13 19:06:27319void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29320
[email protected]c1bb5af2013-03-13 19:06:27321void LayerTreeHostImpl::CommitComplete() {
322 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08323
[email protected]8e0176d2013-03-21 03:14:52324 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25325 // Impl-side painting needs an update immediately post-commit to have the
326 // opportunity to create tilings. Other paths can call UpdateDrawProperties
327 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50328 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27329 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04330 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26331 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08332 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38333 NotifyReadyToActivate();
334 else
335 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27336 } else {
337 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01338 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
339 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27340 }
[email protected]3ba4cae2013-01-16 03:58:38341
[email protected]c1bb5af2013-03-13 19:06:27342 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33343
344 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14345}
346
[email protected]6133cc232013-07-30 18:47:07347bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27348 // Note: If you are changing this function or any other function that might
349 // affect the result of CanDraw, make sure to call
350 // client_->OnCanDrawStateChanged in the proper places and update the
351 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14352
[email protected]6133cc232013-07-30 18:47:07353 if (!renderer_) {
354 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
355 TRACE_EVENT_SCOPE_THREAD);
356 return false;
357 }
358
359 // Must have an OutputSurface if |renderer_| is not NULL.
360 DCHECK(output_surface_);
361
362 // TODO(boliu): Make draws without root_layer work and move this below
363 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27364 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42365 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
366 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22367 return false;
[email protected]c1bb5af2013-03-13 19:06:27368 }
[email protected]6133cc232013-07-30 18:47:07369
370 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
371 return true;
372
[email protected]54af03522013-09-05 00:43:28373 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42374 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
375 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27376 return false;
377 }
378 if (active_tree_->ViewportSizeInvalid()) {
379 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42380 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
381 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27382 return false;
383 }
[email protected]c1bb5af2013-03-13 19:06:27384 if (active_tree_->ContentsTexturesPurged()) {
385 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42386 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
387 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27388 return false;
389 }
[email protected]127bdc1a2013-09-11 01:44:48390 if (EvictedUIResourcesExist()) {
391 TRACE_EVENT_INSTANT0(
392 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
393 TRACE_EVENT_SCOPE_THREAD);
394 return false;
395 }
[email protected]c1bb5af2013-03-13 19:06:27396 return true;
[email protected]2f1acc262012-11-16 21:42:22397}
398
[email protected]c1bb5af2013-03-13 19:06:27399void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
400 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37401 if (input_handler_client_)
402 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27403 AnimatePageScale(monotonic_time);
404 AnimateLayers(monotonic_time, wall_clock_time);
405 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37406 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14407}
408
[email protected]c1bb5af2013-03-13 19:06:27409void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26410 if (!tile_manager_)
411 return;
[email protected]c48536a52013-09-14 00:02:08412 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26413 return;
[email protected]54af03522013-09-05 00:43:28414 if (!device_viewport_valid_for_tile_management_)
415 return;
416
[email protected]c48536a52013-09-14 00:02:08417 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19418 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27419
420 size_t memory_required_bytes;
421 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03422 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27423 size_t memory_used_bytes;
424 tile_manager_->GetMemoryStats(&memory_required_bytes,
425 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03426 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27427 &memory_used_bytes);
428 SendManagedMemoryStats(memory_required_bytes,
429 memory_nice_to_have_bytes,
430 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57431
432 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15433}
434
[email protected]243e4f12014-02-05 09:18:42435void LayerTreeHostImpl::StartPageScaleAnimation(
436 const gfx::Vector2d& target_offset,
437 bool anchor_point,
438 float page_scale,
439 base::TimeDelta duration) {
[email protected]adeda572014-01-31 00:49:47440 if (!InnerViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:27441 return;
442
[email protected]adeda572014-01-31 00:49:47443 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27444 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28445 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27446
[email protected]85d136f782013-04-26 22:04:40447 // Easing constants experimentally determined.
448 scoped_ptr<TimingFunction> timing_function =
449 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
450
[email protected]c1bb5af2013-03-13 19:06:27451 page_scale_animation_ =
452 PageScaleAnimation::Create(scroll_total,
453 active_tree_->total_page_scale_factor(),
454 viewport_size,
455 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40456 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27457
458 if (anchor_point) {
459 gfx::Vector2dF anchor(target_offset);
460 page_scale_animation_->ZoomWithAnchor(anchor,
461 page_scale,
462 duration.InSecondsF());
463 } else {
464 gfx::Vector2dF scaled_target_offset = target_offset;
465 page_scale_animation_->ZoomTo(scaled_target_offset,
466 page_scale,
467 duration.InSecondsF());
468 }
469
[email protected]4e4136d2013-11-29 02:22:44470 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27471 client_->SetNeedsCommitOnImplThread();
472 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15473}
474
[email protected]c1bb5af2013-03-13 19:06:27475void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44476 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15477}
478
[email protected]c1bb5af2013-03-13 19:06:27479bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07480 if (!settings_.touch_hit_testing)
481 return true;
[email protected]c1bb5af2013-03-13 19:06:27482 if (!EnsureRenderSurfaceLayerList())
483 return false;
[email protected]f57bbc02012-11-21 07:02:15484
[email protected]c1bb5af2013-03-13 19:06:27485 gfx::PointF device_viewport_point =
486 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15487
[email protected]c974d5d2013-10-24 01:02:48488 LayerImpl* layer_impl =
489 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
490 device_viewport_point,
491 active_tree_->RenderSurfaceLayerList());
492 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27493}
494
[email protected]6be422b2013-12-08 06:47:31495scoped_ptr<SwapPromiseMonitor>
496LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
497 ui::LatencyInfo* latency) {
498 return scoped_ptr<SwapPromiseMonitor>(
499 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08500}
501
[email protected]c1bb5af2013-03-13 19:06:27502void LayerTreeHostImpl::TrackDamageForAllSurfaces(
503 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28504 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27505 // For now, we use damage tracking to compute a global scissor. To do this, we
506 // must compute all damage tracking before drawing anything, so that we know
507 // the root damage rect. The root damage rect is then used to scissor each
508 // surface.
509
510 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06511 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27512 --surface_index) {
513 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
514 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
515 DCHECK(render_surface);
516 render_surface->damage_tracker()->UpdateDamageTrackingState(
517 render_surface->layer_list(),
518 render_surface_layer->id(),
519 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
520 render_surface->content_rect(),
521 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48522 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27523 }
524}
525
[email protected]34806722013-08-09 23:51:21526scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
527 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
528 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
529 value->SetBoolean("has_no_damage", has_no_damage);
530
531 // Quad data can be quite large, so only dump render passes if we select
532 // cc.debug.quads.
533 bool quads_enabled;
534 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
535 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
536 if (quads_enabled) {
537 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
538 for (size_t i = 0; i < render_passes.size(); ++i)
539 render_pass_list->Append(render_passes[i]->AsValue().release());
540 value->Set("render_passes", render_pass_list.release());
541 }
542 return value.PassAs<base::Value>();
543}
544
[email protected]c1bb5af2013-03-13 19:06:27545void LayerTreeHostImpl::FrameData::AppendRenderPass(
546 scoped_ptr<RenderPass> render_pass) {
547 render_passes_by_id[render_pass->id] = render_pass.get();
548 render_passes.push_back(render_pass.Pass());
549}
550
[email protected]ffbb2212013-06-02 23:47:59551static DrawMode GetDrawMode(OutputSurface* output_surface) {
552 if (output_surface->ForcedDrawToSoftwareDevice()) {
553 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09554 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59555 return DRAW_MODE_HARDWARE;
556 } else {
[email protected]a7f35682013-10-22 23:05:57557 DCHECK_EQ(!output_surface->software_device(),
558 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59559 return DRAW_MODE_SOFTWARE;
560 }
561}
562
[email protected]c1bb5af2013-03-13 19:06:27563static void AppendQuadsForLayer(RenderPass* target_render_pass,
564 LayerImpl* layer,
565 const OcclusionTrackerImpl& occlusion_tracker,
566 AppendQuadsData* append_quads_data) {
567 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49568 QuadCuller quad_culler(&target_render_pass->quad_list,
569 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27570 layer,
571 occlusion_tracker,
572 layer->ShowDebugBorders(),
573 for_surface);
574 layer->AppendQuads(&quad_culler, append_quads_data);
575}
576
577static void AppendQuadsForRenderSurfaceLayer(
578 RenderPass* target_render_pass,
579 LayerImpl* layer,
580 const RenderPass* contributing_render_pass,
581 const OcclusionTrackerImpl& occlusion_tracker,
582 AppendQuadsData* append_quads_data) {
583 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49584 QuadCuller quad_culler(&target_render_pass->quad_list,
585 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27586 layer,
587 occlusion_tracker,
588 layer->ShowDebugBorders(),
589 for_surface);
590
591 bool is_replica = false;
592 layer->render_surface()->AppendQuads(&quad_culler,
593 append_quads_data,
594 is_replica,
595 contributing_render_pass->id);
596
597 // Add replica after the surface so that it appears below the surface.
598 if (layer->has_replica()) {
599 is_replica = true;
600 layer->render_surface()->AppendQuads(&quad_culler,
601 append_quads_data,
602 is_replica,
603 contributing_render_pass->id);
604 }
605}
606
607static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08608 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20609 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06610 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27611 RenderPass* target_render_pass,
612 LayerImpl* root_layer,
613 SkColor screen_background_color,
614 const OcclusionTrackerImpl& occlusion_tracker) {
615 if (!root_layer || !SkColorGetA(screen_background_color))
616 return;
617
618 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
619 if (fill_region.IsEmpty())
620 return;
621
[email protected]3744e27b2013-11-06 21:44:08622 // Divide the fill region into the part to be filled with the overhang
623 // resource and the part to be filled with the background color.
624 Region screen_background_color_region = fill_region;
625 Region overhang_region;
626 if (overhang_resource_id) {
627 overhang_region = fill_region;
628 overhang_region.Subtract(root_scroll_layer_rect);
629 screen_background_color_region.Intersect(root_scroll_layer_rect);
630 }
631
[email protected]c1bb5af2013-03-13 19:06:27632 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49633 QuadCuller quad_culler(&target_render_pass->quad_list,
634 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27635 root_layer,
636 occlusion_tracker,
637 root_layer->ShowDebugBorders(),
638 for_surface);
639
640 // Manually create the quad state for the gutter quads, as the root layer
641 // doesn't have any bounds and so can't generate this itself.
642 // TODO(danakj): Make the gutter quads generated by the solid color layer
643 // (make it smarter about generating quads to fill unoccluded areas).
644
645 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
646 float opacity = 1.f;
647 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49648 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27649 shared_quad_state->SetAll(root_layer->draw_transform(),
650 root_target_rect.size(),
651 root_target_rect,
652 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01653 false,
[email protected]7bbeaf4e2013-11-26 10:27:22654 opacity,
655 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15656
[email protected]c1bb5af2013-03-13 19:06:27657 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17658
[email protected]c1bb5af2013-03-13 19:06:27659 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
660 bool did_invert = root_layer->screen_space_transform().GetInverse(
661 &transform_to_layer_space);
662 DCHECK(did_invert);
[email protected]3744e27b2013-11-06 21:44:08663 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27664 fill_rects.has_rect();
665 fill_rects.next()) {
666 // The root layer transform is composed of translations and scales only,
667 // no perspective, so mapping is sufficient (as opposed to projecting).
[email protected]8a822692014-02-12 17:30:55668 gfx::Rect layer_rect = MathUtil::MapEnclosingClippedRect(
669 transform_to_layer_space, fill_rects.rect());
[email protected]3744e27b2013-11-06 21:44:08670 // Skip the quad culler and just append the quads directly to avoid
671 // occlusion checks.
672 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
673 quad->SetNew(
674 shared_quad_state, layer_rect, screen_background_color, false);
675 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
676 }
677 for (Region::Iterator fill_rects(overhang_region);
678 fill_rects.has_rect();
679 fill_rects.next()) {
680 DCHECK(overhang_resource_id);
[email protected]8a822692014-02-12 17:30:55681 gfx::Rect layer_rect = MathUtil::MapEnclosingClippedRect(
682 transform_to_layer_space, fill_rects.rect());
[email protected]3744e27b2013-11-06 21:44:08683 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
684 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
685 tex_quad->SetNew(
686 shared_quad_state,
687 layer_rect,
688 layer_rect,
689 overhang_resource_id,
690 false,
691 gfx::PointF(layer_rect.x() / overhang_resource_scaled_size.width(),
692 layer_rect.y() / overhang_resource_scaled_size.height()),
693 gfx::PointF(layer_rect.right() /
694 overhang_resource_scaled_size.width(),
695 layer_rect.bottom() /
696 overhang_resource_scaled_size.height()),
697 screen_background_color,
698 vertex_opacity,
699 false);
700 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27701 }
[email protected]467b3612012-08-28 07:41:16702}
703
[email protected]7ddfe7e82014-01-30 07:22:11704DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::CalculateRenderPasses(
705 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27706 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11707 DCHECK(CanDraw());
708 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32709
[email protected]c1bb5af2013-03-13 19:06:27710 TrackDamageForAllSurfaces(active_tree_->root_layer(),
711 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14712
[email protected]e0341352013-04-06 05:01:20713 // If the root render surface has no visible damage, then don't generate a
714 // frame at all.
715 RenderSurfaceImpl* root_surface =
716 active_tree_->root_layer()->render_surface();
717 bool root_surface_has_no_visible_damage =
718 !root_surface->damage_tracker()->current_damage_rect().Intersects(
719 root_surface->content_rect());
720 bool root_surface_has_contributing_layers =
721 !root_surface->layer_list().empty();
722 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23723 root_surface_has_no_visible_damage &&
724 active_tree_->LayersWithCopyOutputRequest().empty()) {
[email protected]e0341352013-04-06 05:01:20725 TRACE_EVENT0("cc",
726 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
727 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07728 DCHECK(!output_surface_->capabilities()
729 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11730 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20731 }
732
[email protected]c1bb5af2013-03-13 19:06:27733 TRACE_EVENT1("cc",
734 "LayerTreeHostImpl::CalculateRenderPasses",
735 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06736 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14737
[email protected]c1bb5af2013-03-13 19:06:27738 // Create the render passes in dependency order.
739 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06740 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27741 --surface_index) {
742 LayerImpl* render_surface_layer =
743 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45744 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
745
746 bool should_draw_into_render_pass =
747 render_surface_layer->parent() == NULL ||
748 render_surface->contributes_to_drawn_surface() ||
749 render_surface_layer->HasCopyRequest();
750 if (should_draw_into_render_pass)
751 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27752 }
[email protected]94f206c12012-08-25 00:09:14753
[email protected]c1bb5af2013-03-13 19:06:27754 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52755 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27756 base::debug::TraceLog::GetInstance() &&
757 base::debug::TraceLog::GetInstance()->IsEnabled();
758 OcclusionTrackerImpl occlusion_tracker(
759 active_tree_->root_layer()->render_surface()->content_rect(),
760 record_metrics_for_frame);
761 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52762 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14763
[email protected]846f455b2013-03-18 19:07:41764 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27765 occlusion_tracker.set_occluding_screen_space_rects_container(
766 &frame->occluding_screen_space_rects);
767 }
[email protected]846f455b2013-03-18 19:07:41768 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27769 occlusion_tracker.set_non_occluding_screen_space_rects_container(
770 &frame->non_occluding_screen_space_rects);
771 }
[email protected]94f206c12012-08-25 00:09:14772
[email protected]b573e4e2014-02-14 02:54:00773 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27774 // occlusion and performing culling during the tree walk.
[email protected]b573e4e2014-02-14 02:54:00775 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14776
[email protected]c1bb5af2013-03-13 19:06:27777 // Typically when we are missing a texture and use a checkerboard quad, we
778 // still draw the frame. However when the layer being checkerboarded is moving
779 // due to an impl-animation, we drop the frame to avoid flashing due to the
780 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11781 DrawSwapReadbackResult::DrawResult draw_result =
782 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25783 // When we have a copy request for a layer, we need to draw no matter
784 // what, as the layer may disappear after this frame.
785 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14786
[email protected]372bad5f2013-03-21 16:38:43787 int layers_drawn = 0;
788
[email protected]ffbb2212013-06-02 23:47:59789 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
790
[email protected]c1bb5af2013-03-13 19:06:27791 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09792 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27793 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09794 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27795 it != end;
796 ++it) {
797 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09798 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27799 RenderPass* target_render_pass =
800 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14801
[email protected]e47b0a0a2013-11-18 23:26:22802 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14803
[email protected]30fe19ff2013-07-04 00:54:45804 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30805
[email protected]18a70192013-04-26 16:18:25806 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36807 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25808 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11809 it->TakeCopyRequestsAndTransformToTarget(
810 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25811 }
[email protected]30fe19ff2013-07-04 00:54:45812 } else if (it.represents_contributing_render_surface() &&
813 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27814 RenderPass::Id contributing_render_pass_id =
815 it->render_surface()->RenderPassId();
816 RenderPass* contributing_render_pass =
817 frame->render_passes_by_id[contributing_render_pass_id];
818 AppendQuadsForRenderSurfaceLayer(target_render_pass,
819 *it,
820 contributing_render_pass,
821 occlusion_tracker,
822 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45823 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09824 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27825 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15826 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27827 it->render_target(),
828 it->visible_content_rect(),
829 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45830 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15831 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27832 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59833
[email protected]c1bb5af2013-03-13 19:06:27834 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57835
[email protected]c1bb5af2013-03-13 19:06:27836 if (it->HasContributingDelegatedRenderPasses()) {
837 RenderPass::Id contributing_render_pass_id =
838 it->FirstContributingRenderPassId();
839 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
840 frame->render_passes_by_id.end()) {
841 RenderPass* render_pass =
842 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14843
[email protected]c1bb5af2013-03-13 19:06:27844 AppendQuadsData append_quads_data(render_pass->id);
845 AppendQuadsForLayer(render_pass,
846 *it,
847 occlusion_tracker,
848 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57849
[email protected]c1bb5af2013-03-13 19:06:27850 contributing_render_pass_id =
851 it->NextContributingRenderPassId(contributing_render_pass_id);
852 }
[email protected]94f206c12012-08-25 00:09:14853 }
854
[email protected]c1bb5af2013-03-13 19:06:27855 AppendQuadsForLayer(target_render_pass,
856 *it,
857 occlusion_tracker,
858 &append_quads_data);
859 }
[email protected]89228202012-08-29 03:20:30860
[email protected]372bad5f2013-03-21 16:38:43861 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14862 }
863
[email protected]e1e768f2013-03-26 08:48:09864 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27865 bool layer_has_animating_transform =
866 it->screen_space_transform_is_animating() ||
867 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11868 if (layer_has_animating_transform) {
869 draw_result =
870 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
871 }
[email protected]c1bb5af2013-03-13 19:06:27872 }
873
[email protected]3d609bb2014-02-01 01:10:23874 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27875 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23876 if (active_tree()->RequiresHighResToDraw())
877 draw_result =
878 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
879 }
[email protected]c1bb5af2013-03-13 19:06:27880
881 occlusion_tracker.LeaveLayer(it);
882 }
883
[email protected]6133cc232013-07-30 18:47:07884 if (have_copy_request ||
885 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11886 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25887
[email protected]1d993172012-10-18 18:15:04888#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27889 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
890 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
891 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
892 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
893 != frame->render_passes_by_id.end());
894 }
[email protected]94f206c12012-08-25 00:09:14895#endif
[email protected]c1bb5af2013-03-13 19:06:27896 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14897
[email protected]c1bb5af2013-03-13 19:06:27898 if (!active_tree_->has_transparent_background()) {
899 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08900 AppendQuadsToFillScreen(
901 ResourceIdForUIResource(overhang_ui_resource_id_),
902 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
903 active_tree_->RootScrollLayerDeviceViewportBounds(),
904 frame->render_passes.back(),
905 active_tree_->root_layer(),
906 active_tree_->background_color(),
907 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27908 }
[email protected]94f206c12012-08-25 00:09:14909
[email protected]7ddfe7e82014-01-30 07:22:11910 if (draw_result == DrawSwapReadbackResult::DRAW_SUCCESS)
[email protected]c1bb5af2013-03-13 19:06:27911 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25912 else
913 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14914
[email protected]c1bb5af2013-03-13 19:06:27915 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48916 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14917
[email protected]30fe19ff2013-07-04 00:54:45918 // Any copy requests left in the tree are not going to get serviced, and
919 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11920 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
921 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
922 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
923 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45924 }
[email protected]19334ac2013-12-12 23:59:11925 for (size_t i = 0; i < requests_to_abort.size(); ++i)
926 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45927
[email protected]e0341352013-04-06 05:01:20928 // If we're making a frame to draw, it better have at least one render pass.
929 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48930
931 // Should only have one render pass in resourceless software mode.
932 if (output_surface_->ForcedDrawToSoftwareDevice())
933 DCHECK_EQ(1u, frame->render_passes.size());
934
[email protected]7ddfe7e82014-01-30 07:22:11935 return draw_result;
[email protected]94f206c12012-08-25 00:09:14936}
937
[email protected]200a9c062013-05-20 04:34:37938void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
939 if (input_handler_client_)
940 input_handler_client_->MainThreadHasStoppedFlinging();
941}
942
[email protected]3d9f7432013-04-06 00:35:18943void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
944 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15945 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01946 if (should_background_tick)
947 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15948
[email protected]3d9f7432013-04-06 00:35:18949 bool enabled = should_background_tick &&
950 !animation_registrar_->active_animation_controllers().empty();
951
[email protected]c1bb5af2013-03-13 19:06:27952 // Lazily create the time_source adapter so that we can vary the interval for
953 // testing.
954 if (!time_source_client_adapter_) {
955 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
956 this,
[email protected]810d40b72013-06-20 18:26:15957 DelayBasedTimeSource::Create(
958 LowFrequencyAnimationInterval(),
959 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
960 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27961 }
[email protected]94f206c12012-08-25 00:09:14962
[email protected]c1bb5af2013-03-13 19:06:27963 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14964}
965
[email protected]b8384e22013-12-03 02:20:48966void LayerTreeHostImpl::DidAnimateScrollOffset() {
967 client_->SetNeedsCommitOnImplThread();
968 client_->RenewTreePriority();
969}
970
[email protected]0023fc72014-01-10 20:05:06971void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02972 viewport_damage_rect_.Union(damage_rect);
973}
974
[email protected]c1bb5af2013-03-13 19:06:27975static inline RenderPass* FindRenderPassById(
976 RenderPass::Id render_pass_id,
977 const LayerTreeHostImpl::FrameData& frame) {
978 RenderPassIdHashMap::const_iterator it =
979 frame.render_passes_by_id.find(render_pass_id);
980 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14981}
982
[email protected]c1bb5af2013-03-13 19:06:27983static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
984 LayerTreeHostImpl::FrameData* frame) {
985 RenderPass* remove_render_pass =
986 FindRenderPassById(remove_render_pass_id, *frame);
987 // The pass was already removed by another quad - probably the original, and
988 // we are the replica.
989 if (!remove_render_pass)
990 return;
991 RenderPassList& render_passes = frame->render_passes;
992 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
993 render_passes.end(),
994 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14995
[email protected]c1bb5af2013-03-13 19:06:27996 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14997
[email protected]c1bb5af2013-03-13 19:06:27998 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
999 frame->render_passes.erase(to_remove);
1000 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:141001
[email protected]c1bb5af2013-03-13 19:06:271002 // Now follow up for all RenderPass quads and remove their RenderPasses
1003 // recursively.
1004 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291005 QuadList::ConstBackToFrontIterator quad_list_iterator =
1006 quad_list.BackToFrontBegin();
1007 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271008 ++quad_list_iterator) {
1009 DrawQuad* current_quad = (*quad_list_iterator);
1010 if (current_quad->material != DrawQuad::RENDER_PASS)
1011 continue;
[email protected]94f206c12012-08-25 00:09:141012
[email protected]c1bb5af2013-03-13 19:06:271013 RenderPass::Id next_remove_render_pass_id =
1014 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1015 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1016 }
[email protected]94f206c12012-08-25 00:09:141017}
1018
[email protected]c1bb5af2013-03-13 19:06:271019bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1020 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1021 const RenderPass* render_pass =
1022 FindRenderPassById(quad.render_pass_id, frame);
1023 if (!render_pass)
1024 return false;
[email protected]94f206c12012-08-25 00:09:141025
[email protected]c1bb5af2013-03-13 19:06:271026 // If any quad or RenderPass draws into this RenderPass, then keep it.
1027 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291028 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1029 quad_list.BackToFrontBegin();
1030 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271031 ++quad_list_iterator) {
1032 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141033
[email protected]c1bb5af2013-03-13 19:06:271034 if (current_quad->material != DrawQuad::RENDER_PASS)
1035 return false;
[email protected]94f206c12012-08-25 00:09:141036
[email protected]c1bb5af2013-03-13 19:06:271037 const RenderPass* contributing_pass = FindRenderPassById(
1038 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1039 if (contributing_pass)
1040 return false;
1041 }
1042 return true;
[email protected]94f206c12012-08-25 00:09:141043}
1044
1045// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271046template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271047 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1048 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141049
1050// static
[email protected]c1bb5af2013-03-13 19:06:271051template <typename RenderPassCuller>
1052void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1053 FrameData* frame) {
1054 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1055 it != culler.RenderPassListEnd(frame->render_passes);
1056 it = culler.RenderPassListNext(it)) {
1057 const RenderPass* current_pass = frame->render_passes[it];
1058 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291059 QuadList::ConstBackToFrontIterator quad_list_iterator =
1060 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141061
[email protected]ed511b8d2013-03-25 03:29:291062 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271063 ++quad_list_iterator) {
1064 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141065
[email protected]c1bb5af2013-03-13 19:06:271066 if (current_quad->material != DrawQuad::RENDER_PASS)
1067 continue;
[email protected]94f206c12012-08-25 00:09:141068
[email protected]c1bb5af2013-03-13 19:06:271069 const RenderPassDrawQuad* render_pass_quad =
1070 RenderPassDrawQuad::MaterialCast(current_quad);
1071 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1072 continue;
[email protected]94f206c12012-08-25 00:09:141073
[email protected]c1bb5af2013-03-13 19:06:271074 // We are changing the vector in the middle of iteration. Because we
1075 // delete render passes that draw into the current pass, we are
1076 // guaranteed that any data from the iterator to the end will not
1077 // change. So, capture the iterator position from the end of the
1078 // list, and restore it after the change.
1079 size_t position_from_end = frame->render_passes.size() - it;
1080 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1081 it = frame->render_passes.size() - position_from_end;
1082 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141083 }
[email protected]c1bb5af2013-03-13 19:06:271084 }
[email protected]94f206c12012-08-25 00:09:141085}
1086
[email protected]7ddfe7e82014-01-30 07:22:111087DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::PrepareToDraw(
1088 FrameData* frame,
1089 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471090 TRACE_EVENT1("cc",
1091 "LayerTreeHostImpl::PrepareToDraw",
1092 "SourceFrameNumber",
1093 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141094
[email protected]425e5332013-09-21 04:10:031095 if (need_to_update_visible_tiles_before_draw_ &&
1096 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1097 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191098 }
[email protected]425e5332013-09-21 04:10:031099 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191100
[email protected]7d19dc342013-05-02 22:02:041101 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271102
[email protected]c1bb5af2013-03-13 19:06:271103 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1104 frame->render_passes.clear();
1105 frame->render_passes_by_id.clear();
1106 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201107 frame->contains_incomplete_tile = false;
1108 frame->has_no_damage = false;
1109
[email protected]0023fc72014-01-10 20:05:061110 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201111 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021112 device_viewport_damage_rect.Union(viewport_damage_rect_);
1113 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201114
1115 active_tree_->root_layer()->render_surface()->damage_tracker()->
1116 AddDamageNextUpdate(device_viewport_damage_rect);
1117 }
[email protected]94f206c12012-08-25 00:09:141118
[email protected]7ddfe7e82014-01-30 07:22:111119 DrawSwapReadbackResult::DrawResult draw_result = CalculateRenderPasses(frame);
1120 if (draw_result != DrawSwapReadbackResult::DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071121 DCHECK(!output_surface_->capabilities()
1122 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111123 return draw_result;
[email protected]6133cc232013-07-30 18:47:071124 }
[email protected]94f206c12012-08-25 00:09:141125
[email protected]7ddfe7e82014-01-30 07:22:111126 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1127 // this function is called again.
1128 return draw_result;
[email protected]94f206c12012-08-25 00:09:141129}
1130
[email protected]0309ba5e02013-06-26 04:11:081131void LayerTreeHostImpl::EvictTexturesForTesting() {
1132 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1133}
1134
[email protected]4f48f6e2013-08-27 06:33:381135void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1136 NOTREACHED();
1137}
1138
[email protected]425e5332013-09-21 04:10:031139void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1140 DidInitializeVisibleTile();
1141}
1142
[email protected]c1bb5af2013-03-13 19:06:271143void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1144 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121145
[email protected]c1bb5af2013-03-13 19:06:271146 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351147 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351148 ManagedMemoryPolicy::PriorityCutoffToValue(
1149 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351150 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271151 if (evicted_resources) {
1152 active_tree_->SetContentsTexturesPurged();
1153 if (pending_tree_)
1154 pending_tree_->SetContentsTexturesPurged();
1155 client_->SetNeedsCommitOnImplThread();
1156 client_->OnCanDrawStateChanged(CanDraw());
1157 client_->RenewTreePriority();
1158 }
1159 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431160
[email protected]8be1a9bf2013-05-01 03:45:191161 UpdateTileManagerMemoryPolicy(policy);
1162}
1163
1164void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1165 const ManagedMemoryPolicy& policy) {
1166 if (!tile_manager_)
1167 return;
1168
[email protected]8be1a9bf2013-05-01 03:45:191169 // TODO(reveman): We should avoid keeping around unused resources if
1170 // possible. crbug.com/224475
[email protected]25489c02014-02-13 23:18:411171 global_tile_state_.hard_memory_limit_in_bytes = 0;
1172 global_tile_state_.soft_memory_limit_in_bytes = 0;
1173 if (visible_ && policy.bytes_limit_when_visible > 0) {
1174 global_tile_state_.hard_memory_limit_in_bytes =
1175 policy.bytes_limit_when_visible;
1176 global_tile_state_.soft_memory_limit_in_bytes =
1177 global_tile_state_.hard_memory_limit_in_bytes *
1178 settings_.max_memory_for_prepaint_percentage / 100;
1179 }
1180 // Unused limit is calculated from soft-limit, as hard-limit may
1181 // be very high and shouldn't typically be exceeded.
[email protected]1bcced22013-09-24 13:51:191182 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
[email protected]25489c02014-02-13 23:18:411183 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1184 settings_.max_unused_resource_memory_percentage) /
1185 100);
[email protected]1bcced22013-09-24 13:51:191186 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191187 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1188 visible_ ?
1189 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351190 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191191 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081192
[email protected]c48536a52013-09-14 00:02:081193 DidModifyTilePriorities();
1194}
1195
1196void LayerTreeHostImpl::DidModifyTilePriorities() {
1197 DCHECK(settings_.impl_side_painting);
1198 // Mark priorities as dirty and schedule a ManageTiles().
1199 tile_priorities_dirty_ = true;
1200 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141201}
1202
[email protected]86126792013-03-16 20:07:541203void LayerTreeHostImpl::DidInitializeVisibleTile() {
1204 // TODO(reveman): Determine tiles that changed and only damage
1205 // what's necessary.
1206 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031207 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541208 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471209}
1210
[email protected]94bf75c2013-06-12 13:20:041211void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381212 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041213}
1214
[email protected]3f2ff112013-08-03 02:41:071215void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1216 SetManagedMemoryPolicy(policy, zero_budget_);
1217}
1218
[email protected]5f07afc2013-07-19 05:17:181219void LayerTreeHostImpl::SetTreeActivationCallback(
1220 const base::Closure& callback) {
1221 DCHECK(proxy_->IsImplThread());
1222 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261223 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181224}
1225
[email protected]c1bb5af2013-03-13 19:06:271226void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071227 const ManagedMemoryPolicy& policy, bool zero_budget) {
1228 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271229 return;
[email protected]61de5812012-11-08 07:03:441230
[email protected]3f2ff112013-08-03 02:41:071231 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1232
1233 cached_managed_memory_policy_ = policy;
1234 zero_budget_ = zero_budget;
1235 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1236
1237 if (old_policy == actual_policy)
1238 return;
1239
1240 if (!proxy_->HasImplThread()) {
1241 // In single-thread mode, this can be called on the main thread by
1242 // GLRenderer::OnMemoryAllocationChanged.
1243 DebugScopedSetImplThread impl_thread(proxy_);
1244 EnforceManagedMemoryPolicy(actual_policy);
1245 } else {
1246 DCHECK(proxy_->IsImplThread());
1247 EnforceManagedMemoryPolicy(actual_policy);
1248 }
[email protected]50644642013-06-20 13:58:551249
[email protected]d7626ffd2013-03-29 00:17:421250 // If there is already enough memory to draw everything imaginable and the
1251 // new memory limit does not change this, then do not re-commit. Don't bother
1252 // skipping commits if this is not visible (commits don't happen when not
1253 // visible, there will almost always be a commit when this becomes visible).
1254 bool needs_commit = true;
1255 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071256 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1257 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1258 actual_policy.priority_cutoff_when_visible ==
1259 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421260 needs_commit = false;
1261 }
1262
[email protected]d7626ffd2013-03-29 00:17:421263 if (needs_commit)
1264 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141265}
1266
[email protected]f224cc92013-06-06 23:23:321267void LayerTreeHostImpl::SetExternalDrawConstraints(
1268 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061269 const gfx::Rect& viewport,
1270 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281271 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321272 external_transform_ = transform;
1273 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281274 external_clip_ = clip;
1275 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321276}
1277
[email protected]0023fc72014-01-10 20:05:061278void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311279 if (damage_rect.IsEmpty())
1280 return;
1281 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031282 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1283}
1284
[email protected]daea3d42013-10-23 17:04:501285void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1286 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451287}
1288
[email protected]4d7e46a2013-11-08 05:33:401289void LayerTreeHostImpl::DidSwapBuffers() {
1290 client_->DidSwapBuffersOnImplThread();
1291}
1292
[email protected]a7335e0b2013-09-18 09:34:511293void LayerTreeHostImpl::OnSwapBuffersComplete() {
1294 client_->OnSwapBuffersCompleteOnImplThread();
1295}
1296
1297void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271298 // TODO(piman): We may need to do some validation on this ack before
1299 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511300 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291301 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161302}
1303
[email protected]c1bb5af2013-03-13 19:06:271304void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1305 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291306}
1307
[email protected]c1bb5af2013-03-13 19:06:271308CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1309 CompositorFrameMetadata metadata;
1310 metadata.device_scale_factor = device_scale_factor_;
1311 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1312 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1313 metadata.root_layer_size = active_tree_->ScrollableSize();
1314 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1315 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1316 if (top_controls_manager_) {
1317 metadata.location_bar_offset =
1318 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1319 metadata.location_bar_content_translation =
1320 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011321 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271322 }
[email protected]bf189f62012-12-18 03:42:111323
[email protected]adeda572014-01-31 00:49:471324 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111325 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271326
[email protected]adeda572014-01-31 00:49:471327 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271328
1329 return metadata;
[email protected]bf189f62012-12-18 03:42:111330}
1331
[email protected]6f50b8fa2013-07-01 19:51:491332static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1333 layer->DidBeginTracing();
1334}
[email protected]37349bc2013-06-04 01:31:521335
[email protected]f0c2a242013-03-15 19:34:521336void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1337 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351338 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271339 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201340
[email protected]3519b872013-07-30 07:17:501341 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191342 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071343 DCHECK(!output_surface_->capabilities()
1344 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201345 return;
[email protected]3519b872013-07-30 07:17:501346 }
[email protected]e0341352013-04-06 05:01:201347
[email protected]c1bb5af2013-03-13 19:06:271348 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141349
[email protected]213fcfc2013-08-24 14:59:271350 fps_counter_->SaveTimeStamp(frame_begin_time,
1351 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351352 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091353 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351354 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431355
[email protected]c1bb5af2013-03-13 19:06:271356 if (tile_manager_) {
1357 memory_history_->SaveEntry(
1358 tile_manager_->memory_stats_from_last_assign());
1359 }
[email protected]1191d9d2013-02-02 06:00:331360
[email protected]846f455b2013-03-18 19:07:411361 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021362 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271363 active_tree_->root_layer(),
1364 *frame->render_surface_layer_list,
1365 frame->occluding_screen_space_rects,
1366 frame->non_occluding_screen_space_rects,
1367 debug_state_);
1368 }
[email protected]94f206c12012-08-25 00:09:141369
[email protected]a848c102013-03-26 08:59:091370 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1371 const RenderingStats& stats =
1372 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181373 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091374 }
1375
[email protected]37349bc2013-06-04 01:31:521376 bool is_new_trace;
1377 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1378 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491379 if (pending_tree_) {
1380 LayerTreeHostCommon::CallFunctionForSubtree(
1381 pending_tree_->root_layer(),
1382 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1383 }
1384 LayerTreeHostCommon::CallFunctionForSubtree(
1385 active_tree_->root_layer(),
1386 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521387 }
1388
[email protected]9197dbcb2013-05-15 20:28:511389 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211390 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1391 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1392 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081393
[email protected]c1bb5af2013-03-13 19:06:271394 // Because the contents of the HUD depend on everything else in the frame, the
1395 // contents of its texture are updated as the last thing before the frame is
1396 // drawn.
[email protected]671f6672013-10-01 07:31:071397 if (active_tree_->hud_layer()) {
1398 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531399 active_tree_->hud_layer()->UpdateHudTexture(
1400 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071401 }
[email protected]94f206c12012-08-25 00:09:141402
[email protected]b09c1942013-05-10 00:06:371403 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321404 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261405 bool disable_picture_quad_image_filtering =
1406 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321407
[email protected]b09c1942013-05-10 00:06:371408 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321409 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301410 temp_software_renderer->DrawFrame(&frame->render_passes,
1411 NULL,
1412 device_scale_factor_,
1413 DeviceViewport(),
1414 DeviceClip(),
1415 allow_partial_swap,
1416 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371417 } else {
[email protected]573e8e32013-09-09 17:24:321418 // We don't track damage on the HUD layer (it interacts with damage tracking
1419 // visualizations), so disable partial swaps to make the HUD layer display
1420 // properly.
1421 bool allow_partial_swap = !debug_state_.ShowHudRects();
1422
[email protected]b5174d712013-08-28 08:10:431423 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321424 offscreen_context_provider_.get(),
1425 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301426 DeviceViewport(),
1427 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261428 allow_partial_swap,
1429 false);
[email protected]b09c1942013-05-10 00:06:371430 }
[email protected]c1bb5af2013-03-13 19:06:271431 // The render passes should be consumed by the renderer.
1432 DCHECK(frame->render_passes.empty());
1433 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141434
[email protected]c1bb5af2013-03-13 19:06:271435 // The next frame should start by assuming nothing has changed, and changes
1436 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321437 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1438 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541439 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271440 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1441 DidDrawDamagedArea();
1442 }
1443 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351444
[email protected]d73094482014-02-13 21:28:281445 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331446 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1447 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181448 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141449}
1450
[email protected]c1bb5af2013-03-13 19:06:271451void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1452 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1453 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521454
[email protected]c1bb5af2013-03-13 19:06:271455 // Once all layers have been drawn, pending texture uploads should no
1456 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151457 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141458}
1459
[email protected]c1bb5af2013-03-13 19:06:271460void LayerTreeHostImpl::FinishAllRendering() {
1461 if (renderer_)
1462 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141463}
1464
[email protected]c1bb5af2013-03-13 19:06:271465bool LayerTreeHostImpl::IsContextLost() {
1466 DCHECK(proxy_->IsImplThread());
1467 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141468}
1469
[email protected]7a8bcd262014-01-15 12:54:581470const RendererCapabilitiesImpl&
1471LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271472 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141473}
1474
[email protected]e0341352013-04-06 05:01:201475bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231476 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191477 if (frame.has_no_damage) {
1478 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201479 return false;
[email protected]b69c1db2013-11-27 00:05:191480 }
[email protected]4a28a612013-11-27 02:06:331481 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551482 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331483 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161484 return true;
[email protected]94f206c12012-08-25 00:09:141485}
1486
[email protected]daea3d42013-10-23 17:04:501487void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451488 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501489 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451490}
1491
[email protected]54af03522013-09-05 00:43:281492gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341493 // Use the root container layer bounds if it clips to them, otherwise, the
1494 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441495 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341496 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441497 DCHECK(!top_controls_manager_);
1498 DCHECK_EQ(0, overdraw_bottom_height_);
1499 return container_layer->bounds();
1500 }
1501
[email protected]ffb2720f2013-03-15 19:18:371502 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281503 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371504
[email protected]ed511b8d2013-03-25 03:29:291505 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371506 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401507 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291508 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371509}
1510
[email protected]c1bb5af2013-03-13 19:06:271511void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191512 if (resource_provider_)
1513 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011514 // TODO(jamesr): The renderer_ check is needed to make some of the
1515 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1516 // important) in production. We should adjust the test to not need this.
1517 if (renderer_)
1518 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]ce2e8112013-11-28 07:44:361519#ifndef NDEBUG
1520 did_lose_called_ = true;
1521#endif
[email protected]94f206c12012-08-25 00:09:141522}
1523
[email protected]c1bb5af2013-03-13 19:06:271524void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061525 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271526 DCHECK(renderer_);
1527 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141528}
1529
[email protected]59adb112013-04-09 04:48:441530bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471531 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011532}
1533
[email protected]c1bb5af2013-03-13 19:06:271534LayerImpl* LayerTreeHostImpl::RootLayer() const {
1535 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081536}
1537
[email protected]adeda572014-01-31 00:49:471538LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1539 return active_tree_->InnerViewportScrollLayer();
1540}
1541
1542LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1543 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081544}
1545
[email protected]c1bb5af2013-03-13 19:06:271546LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1547 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081548}
1549
[email protected]251699b2013-10-09 00:21:261550bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1551 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471552 (InnerViewportScrollLayer() &&
1553 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1554 (OuterViewportScrollLayer() &&
1555 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261556}
1557
[email protected]94f206c12012-08-25 00:09:141558// Content layers can be either directly scrollable or contained in an outer
1559// scrolling layer which applies the scroll transform. Given a content layer,
1560// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271561static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1562 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021563 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271564
1565 if (layer_impl->scrollable())
1566 return layer_impl;
1567
1568 if (layer_impl->DrawsContent() &&
1569 layer_impl->parent() &&
1570 layer_impl->parent()->scrollable())
1571 return layer_impl->parent();
1572
[email protected]bf1cfd9a2013-09-26 05:43:021573 return NULL;
[email protected]94f206c12012-08-25 00:09:141574}
1575
[email protected]c1bb5af2013-03-13 19:06:271576void LayerTreeHostImpl::CreatePendingTree() {
1577 CHECK(!pending_tree_);
1578 if (recycle_tree_)
1579 recycle_tree_.swap(pending_tree_);
1580 else
1581 pending_tree_ = LayerTreeImpl::create(this);
1582 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111583 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271584}
1585
[email protected]8bb6cfa2013-07-23 00:11:191586void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191587 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1588 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191589 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481590}
1591
[email protected]c1bb5af2013-03-13 19:06:271592void LayerTreeHostImpl::ActivatePendingTree() {
1593 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111594 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351595
[email protected]4f48f6e2013-08-27 06:33:381596 need_to_update_visible_tiles_before_draw_ = true;
1597
[email protected]1960a712013-04-30 17:06:471598 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271599 active_tree_->PushPersistedState(pending_tree_.get());
1600 if (pending_tree_->needs_full_tree_sync()) {
1601 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331602 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271603 active_tree_->DetachLayerTree(),
1604 active_tree_.get()));
1605 }
[email protected]b5651c22013-03-14 15:06:331606 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271607 active_tree_->root_layer());
1608 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511609
[email protected]c9280762013-08-01 06:28:571610 // Process any requests in the UI resource queue. The request queue is given
1611 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1612 // the swap.
1613 pending_tree_->ProcessUIResourceRequestQueue();
1614
[email protected]c1bb5af2013-03-13 19:06:271615 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511616
[email protected]c1bb5af2013-03-13 19:06:271617 // Now that we've synced everything from the pending tree to the active
1618 // tree, rename the pending tree the recycle tree so we can reuse it on the
1619 // next sync.
1620 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511621
[email protected]7d1b07e2013-10-01 17:31:301622 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471623 active_tree_->SetRootLayerScrollOffsetDelegate(
1624 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221625
[email protected]c1bb5af2013-03-13 19:06:271626 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441627 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271628 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241629
[email protected]a848c102013-03-26 08:59:091630 if (debug_state_.continuous_painting) {
1631 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431632 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091633 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1634 stats.main_stats.record_time +
1635 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271636 }
[email protected]2a61ad52013-05-13 14:01:291637
1638 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261639 if (!tree_activation_callback_.is_null())
1640 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011641
1642 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1643 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281644 devtools_instrumentation::DidActivateLayerTree(
1645 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271646}
1647
[email protected]c1bb5af2013-03-13 19:06:271648void LayerTreeHostImpl::SetVisible(bool visible) {
1649 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141650
[email protected]c1bb5af2013-03-13 19:06:271651 if (visible_ == visible)
1652 return;
1653 visible_ = visible;
1654 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121655 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141656
[email protected]3d609bb2014-02-01 01:10:231657 if (!visible_) {
1658 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481659 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231660 }
[email protected]127bdc1a2013-09-11 01:44:481661
[email protected]0fa42e02013-08-20 01:16:051662 // Evict tiles immediately if invisible since this tab may never get another
1663 // draw or timer tick.
1664 if (!visible_)
1665 ManageTiles();
1666
[email protected]c1bb5af2013-03-13 19:06:271667 if (!renderer_)
1668 return;
[email protected]94f206c12012-08-25 00:09:141669
[email protected]c1bb5af2013-03-13 19:06:271670 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141671}
1672
[email protected]6be422b2013-12-08 06:47:311673void LayerTreeHostImpl::SetNeedsRedraw() {
1674 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1675 client_->SetNeedsRedrawOnImplThread();
1676}
1677
[email protected]206a3922013-05-17 06:34:121678ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071679 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011680 // TODO(ernstm): The second condition disables pre-painting for all layers
1681 // when GPU rasterization is enabled. Once we selectively enable GPU
1682 // rasterization per layer, we also need to disable pre-painting selectively:
1683 // crbug.com/335387
1684 if (debug_state_.rasterize_only_visible_content ||
1685 settings_.gpu_rasterization) {
[email protected]3f2ff112013-08-03 02:41:071686 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291687 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071688 }
[email protected]206a3922013-05-17 06:34:121689
[email protected]3f2ff112013-08-03 02:41:071690 if (zero_budget_) {
1691 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071692 }
1693
[email protected]206a3922013-05-17 06:34:121694 return actual;
1695}
1696
[email protected]3f2ff112013-08-03 02:41:071697size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1698 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1699}
1700
[email protected]990e050a2013-09-23 18:50:211701int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1702 return ManagedMemoryPolicy::PriorityCutoffToValue(
1703 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1704}
1705
[email protected]50644642013-06-20 13:58:551706void LayerTreeHostImpl::ReleaseTreeResources() {
1707 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551708 SendReleaseResourcesRecursive(active_tree_->root_layer());
1709 if (pending_tree_ && pending_tree_->root_layer())
1710 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1711 if (recycle_tree_ && recycle_tree_->root_layer())
1712 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571713
[email protected]127bdc1a2013-09-11 01:44:481714 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551715}
1716
1717void LayerTreeHostImpl::CreateAndSetRenderer(
1718 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201719 ResourceProvider* resource_provider,
1720 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551721 DCHECK(!renderer_);
1722 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321723 renderer_ = DelegatingRenderer::Create(
1724 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091725 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551726 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321727 &settings_,
[email protected]50644642013-06-20 13:58:551728 output_surface,
1729 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111730 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241731 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551732 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321733 renderer_ = SoftwareRenderer::Create(
1734 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551735 }
1736
[email protected]84975832013-06-29 00:24:111737 if (renderer_) {
[email protected]50644642013-06-20 13:58:551738 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111739 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561740
1741 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1742 // initialized to get max texture size. Also, after releasing resources,
1743 // trees need another update to generate new ones.
1744 active_tree_->set_needs_update_draw_properties();
1745 if (pending_tree_)
1746 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591747 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111748 }
[email protected]50644642013-06-20 13:58:551749}
1750
[email protected]50af3c22013-07-13 03:50:201751void LayerTreeHostImpl::CreateAndSetTileManager(
1752 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011753 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371754 bool using_map_image,
1755 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201756 DCHECK(settings_.impl_side_painting);
1757 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011758 tile_manager_ =
1759 TileManager::Create(this,
1760 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281761 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011762 rendering_stats_instrumentation_,
1763 using_map_image,
[email protected]4e472a42014-02-11 01:56:371764 allow_rasterize_on_demand,
[email protected]b4664142013-11-08 00:50:311765 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121766 GetMaxRasterTasksUsageBytes(context_provider),
1767 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011768
[email protected]50af3c22013-07-13 03:50:201769 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191770 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201771}
1772
[email protected]50644642013-06-20 13:58:551773void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071774 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551775}
1776
[email protected]c1bb5af2013-03-13 19:06:271777bool LayerTreeHostImpl::InitializeRenderer(
1778 scoped_ptr<OutputSurface> output_surface) {
[email protected]ce2e8112013-11-28 07:44:361779#ifndef NDEBUG
1780 DCHECK(!renderer_ || did_lose_called_);
1781#endif
1782
[email protected]c1bb5af2013-03-13 19:06:271783 // Since we will create a new resource provider, we cannot continue to use
1784 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1785 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551786 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401787
[email protected]c1bb5af2013-03-13 19:06:271788 // Note: order is important here.
1789 renderer_.reset();
1790 tile_manager_.reset();
1791 resource_provider_.reset();
1792 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141793
[email protected]c1bb5af2013-03-13 19:06:271794 if (!output_surface->BindToClient(this))
1795 return false;
[email protected]be3181652012-09-25 13:02:131796
[email protected]a7f35682013-10-22 23:05:571797 scoped_ptr<ResourceProvider> resource_provider =
1798 ResourceProvider::Create(output_surface.get(),
1799 shared_bitmap_manager_,
1800 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341801 settings_.use_rgba_4444_textures,
1802 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551803 if (!resource_provider)
1804 return false;
[email protected]2b154b22013-06-07 09:03:271805
[email protected]50644642013-06-20 13:58:551806 if (output_surface->capabilities().deferred_gl_initialization)
1807 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131808
[email protected]50af3c22013-07-13 03:50:201809 bool skip_gl_renderer = false;
1810 CreateAndSetRenderer(
1811 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131812
[email protected]50644642013-06-20 13:58:551813 if (!renderer_)
1814 return false;
[email protected]6123930c2013-05-23 06:48:371815
[email protected]50644642013-06-20 13:58:551816 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371817 CreateAndSetTileManager(
1818 resource_provider.get(),
1819 output_surface->context_provider().get(),
1820 GetRendererCapabilities().using_map_image,
1821 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241822 }
1823
[email protected]daea3d42013-10-23 17:04:501824 // Setup BeginImplFrameEmulation if it's not supported natively
1825 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351826 const base::TimeDelta display_refresh_interval =
1827 base::TimeDelta::FromMicroseconds(
1828 base::Time::kMicrosecondsPerSecond /
1829 settings_.refresh_rate);
1830
[email protected]daea3d42013-10-23 17:04:501831 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151832 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351833 settings_.throttle_frame_production,
1834 display_refresh_interval);
1835 }
1836
1837 int max_frames_pending =
1838 output_surface->capabilities().max_frames_pending;
1839 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351840 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351841 output_surface->SetMaxFramesPending(max_frames_pending);
1842
[email protected]50644642013-06-20 13:58:551843 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271844 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141845
[email protected]c1bb5af2013-03-13 19:06:271846 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211847
[email protected]c1bb5af2013-03-13 19:06:271848 return true;
[email protected]94f206c12012-08-25 00:09:141849}
1850
[email protected]2b154b22013-06-07 09:03:271851bool LayerTreeHostImpl::DeferredInitialize(
1852 scoped_refptr<ContextProvider> offscreen_context_provider) {
1853 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551854 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091855 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271856
[email protected]50644642013-06-20 13:58:551857 ReleaseTreeResources();
1858 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271859
[email protected]d9ca99e2013-08-28 21:49:481860 bool resource_provider_success = resource_provider_->InitializeGL();
1861
1862 bool success = resource_provider_success;
1863 if (success) {
1864 bool skip_gl_renderer = false;
1865 CreateAndSetRenderer(
1866 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1867 if (!renderer_)
1868 success = false;
1869 }
1870
1871 if (success) {
1872 if (offscreen_context_provider.get() &&
1873 !offscreen_context_provider->BindToCurrentThread())
1874 success = false;
1875 }
1876
[email protected]50644642013-06-20 13:58:551877 if (success) {
1878 EnforceZeroBudget(false);
1879 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481880 } else {
1881 if (offscreen_context_provider.get()) {
1882 if (offscreen_context_provider->BindToCurrentThread())
1883 offscreen_context_provider->VerifyContexts();
1884 offscreen_context_provider = NULL;
1885 }
1886
1887 client_->DidLoseOutputSurfaceOnImplThread();
1888
1889 if (resource_provider_success) {
1890 // If this fails the context provider will be dropped from the output
1891 // surface and destroyed. But the GLRenderer expects the output surface
1892 // to stick around - and hold onto the context3d - as long as it is alive.
1893 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1894 renderer_.reset();
1895
1896 // The resource provider can't stay in GL mode or it tries to clean up GL
1897 // stuff, but the context provider is going away on the output surface
1898 // which contradicts being in GL mode.
1899 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1900 resource_provider_->InitializeSoftware();
1901 }
[email protected]50644642013-06-20 13:58:551902 }
[email protected]b5174d712013-08-28 08:10:431903
1904 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271905 return success;
1906}
1907
[email protected]50af3c22013-07-13 03:50:201908void LayerTreeHostImpl::ReleaseGL() {
1909 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1910 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091911 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201912
1913 ReleaseTreeResources();
1914 renderer_.reset();
1915 tile_manager_.reset();
1916 resource_provider_->InitializeSoftware();
1917
1918 bool skip_gl_renderer = true;
1919 CreateAndSetRenderer(
1920 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1921 DCHECK(renderer_);
1922
1923 EnforceZeroBudget(true);
1924 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011925 NULL,
[email protected]4e472a42014-02-11 01:56:371926 GetRendererCapabilities().using_map_image,
1927 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201928 DCHECK(tile_manager_);
1929
[email protected]b5174d712013-08-28 08:10:431930 SetOffscreenContextProvider(NULL);
1931
[email protected]50af3c22013-07-13 03:50:201932 client_->SetNeedsCommitOnImplThread();
1933}
1934
[email protected]64348ea2014-01-29 22:58:261935void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:401936 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271937 return;
[email protected]94f206c12012-08-25 00:09:141938
[email protected]54af03522013-09-05 00:43:281939 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271940 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271941
[email protected]c1bb5af2013-03-13 19:06:271942 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141943
[email protected]c1bb5af2013-03-13 19:06:271944 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441945 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141946}
1947
[email protected]d9083762013-03-24 01:36:401948void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1949 if (overdraw_bottom_height == overdraw_bottom_height_)
1950 return;
1951 overdraw_bottom_height_ = overdraw_bottom_height;
1952
[email protected]59adb112013-04-09 04:48:441953 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401954}
1955
[email protected]9f4f6a32013-09-04 21:35:121956void LayerTreeHostImpl::SetOverhangUIResource(
1957 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:261958 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:121959 overhang_ui_resource_id_ = overhang_ui_resource_id;
1960 overhang_ui_resource_size_ = overhang_ui_resource_size;
1961}
1962
[email protected]c1bb5af2013-03-13 19:06:271963void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1964 if (device_scale_factor == device_scale_factor_)
1965 return;
1966 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271967
[email protected]59adb112013-04-09 04:48:441968 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141969}
1970
[email protected]54af03522013-09-05 00:43:281971gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1972 return DeviceViewport().size();
1973}
1974
[email protected]f224cc92013-06-06 23:23:321975gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1976 if (external_viewport_.IsEmpty())
1977 return gfx::Rect(device_viewport_size_);
1978
1979 return external_viewport_;
1980}
1981
[email protected]54af03522013-09-05 00:43:281982gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1983 if (external_clip_.IsEmpty())
1984 return DeviceViewport();
1985
1986 return external_clip_;
1987}
1988
1989const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321990 return external_transform_;
1991}
1992
[email protected]59adb112013-04-09 04:48:441993void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]4e4136d2013-11-29 02:22:441994 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:441995 active_tree_->set_needs_update_draw_properties();
1996 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141997}
1998
[email protected]c1bb5af2013-03-13 19:06:271999bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:042000 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:342001 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:142002}
2003
[email protected]200a9c062013-05-20 04:34:372004void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2005 DCHECK(input_handler_client_ == NULL);
2006 input_handler_client_ = client;
2007}
2008
[email protected]420fdf6e2013-08-26 20:36:382009static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2010 if (LayerImpl* scroll_parent = layer->scroll_parent())
2011 return scroll_parent;
2012 return layer->parent();
2013}
2014
[email protected]f620b0e72013-10-01 21:38:242015LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472016 const gfx::PointF& device_viewport_point,
2017 InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:432018 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:022019 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142020
[email protected]c1bb5af2013-03-13 19:06:272021 // Walk up the hierarchy and look for a scrollable layer.
2022 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382023 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272024 // The content layer can also block attempts to scroll outside the main
2025 // thread.
2026 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2027 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022028 *scroll_on_main_thread = true;
2029 return NULL;
[email protected]94f206c12012-08-25 00:09:142030 }
2031
[email protected]c1bb5af2013-03-13 19:06:272032 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2033 if (!scroll_layer_impl)
2034 continue;
[email protected]94f206c12012-08-25 00:09:142035
[email protected]c1bb5af2013-03-13 19:06:272036 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272037 // If any layer wants to divert the scroll event to the main thread, abort.
2038 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022039 *scroll_on_main_thread = true;
2040 return NULL;
[email protected]94f206c12012-08-25 00:09:142041 }
2042
[email protected]c1bb5af2013-03-13 19:06:272043 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2044 potentially_scrolling_layer_impl = scroll_layer_impl;
2045 }
2046
[email protected]bf1cfd9a2013-09-26 05:43:022047 return potentially_scrolling_layer_impl;
2048}
2049
2050InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
2051 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
2052 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2053
2054 if (top_controls_manager_)
2055 top_controls_manager_->ScrollBegin();
2056
2057 DCHECK(!CurrentlyScrollingLayer());
2058 ClearCurrentlyScrollingLayer();
2059
2060 if (!EnsureRenderSurfaceLayerList())
2061 return ScrollIgnored;
2062
[email protected]f620b0e72013-10-01 21:38:242063 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2064 device_scale_factor_);
2065 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2066 device_viewport_point,
2067 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022068 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242069 LayerImpl* potentially_scrolling_layer_impl =
2070 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2071 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022072
2073 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022074 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2075 return ScrollOnMainThread;
2076 }
2077
[email protected]adeda572014-01-31 00:49:472078 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532079 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472080 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2081 ? OuterViewportScrollLayer()
2082 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102083
[email protected]c1bb5af2013-03-13 19:06:272084 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202085 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272086 potentially_scrolling_layer_impl);
2087 should_bubble_scrolls_ = (type != NonBubblingGesture);
2088 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272089 client_->RenewTreePriority();
2090 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2091 return ScrollStarted;
2092 }
2093 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142094}
2095
[email protected]c1bb5af2013-03-13 19:06:272096gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2097 LayerImpl* layer_impl,
2098 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472099 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122100 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272101 // Layers with non-invertible screen space transforms should not have passed
2102 // the scroll hit test in the first place.
2103 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2104 gfx::Transform inverse_screen_space_transform(
2105 gfx::Transform::kSkipInitialization);
2106 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2107 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352108 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2109 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272110 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142111
[email protected]c1bb5af2013-03-13 19:06:272112 gfx::PointF screen_space_point =
2113 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142114
[email protected]c1bb5af2013-03-13 19:06:272115 gfx::Vector2dF screen_space_delta = viewport_delta;
2116 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2117
2118 // First project the scroll start and end points to local layer space to find
2119 // the scroll delta in layer coordinates.
2120 bool start_clipped, end_clipped;
2121 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2122 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212123 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272124 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212125 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272126 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212127 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272128 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212129 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272130
2131 // In general scroll point coordinates should not get clipped.
2132 DCHECK(!start_clipped);
2133 DCHECK(!end_clipped);
2134 if (start_clipped || end_clipped)
2135 return gfx::Vector2dF();
2136
2137 // local_start_point and local_end_point are in content space but we want to
2138 // move them to layer space for scrolling.
2139 float width_scale = 1.f / layer_impl->contents_scale_x();
2140 float height_scale = 1.f / layer_impl->contents_scale_y();
2141 local_start_point.Scale(width_scale, height_scale);
2142 local_end_point.Scale(width_scale, height_scale);
2143
2144 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472145 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272146 layer_impl->ScrollBy(local_end_point - local_start_point);
2147
2148 // Get the end point in the layer's content space so we can apply its
2149 // ScreenSpaceTransform.
2150 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472151 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272152 previous_delta;
2153 gfx::PointF actual_local_content_end_point =
2154 gfx::ScalePoint(actual_local_end_point,
2155 1.f / width_scale,
2156 1.f / height_scale);
2157
2158 // Calculate the applied scroll delta in viewport space coordinates.
2159 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212160 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272161 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212162 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272163 DCHECK(!end_clipped);
2164 if (end_clipped)
2165 return gfx::Vector2dF();
2166 gfx::PointF actual_viewport_end_point =
2167 gfx::ScalePoint(actual_screen_space_end_point,
2168 1.f / scale_from_viewport_to_screen_space);
2169 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142170}
2171
[email protected]c1bb5af2013-03-13 19:06:272172static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122173 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472174 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272175 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472176 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272177}
2178
2179bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
[email protected]3244c9132014-01-23 10:39:122180 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272181 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2182 if (!CurrentlyScrollingLayer())
2183 return false;
2184
2185 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532186 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292187 bool did_scroll_x = false;
2188 bool did_scroll_y = false;
[email protected]adeda572014-01-31 00:49:472189 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2190 // here?
2191 bool consume_by_top_controls =
2192 top_controls_manager_ &&
2193 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2194 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2195 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2196 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062197
[email protected]c1bb5af2013-03-13 19:06:272198 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2199 layer_impl;
2200 layer_impl = layer_impl->parent()) {
2201 if (!layer_impl->scrollable())
2202 continue;
2203
[email protected]adeda572014-01-31 00:49:472204 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532205 // Only allow bubble scrolling when the scroll is in the direction to make
2206 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472207 gfx::Vector2dF applied_delta;
2208 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532209 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472210 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2211 applied_delta = pending_delta - excess_delta;
2212 pending_delta = excess_delta;
2213 // Force updating of vertical adjust values if needed.
2214 if (applied_delta.y() != 0)
2215 layer_impl->ScrollbarParametersDidChange();
[email protected]a2b5ded2013-05-20 21:32:532216 }
2217 // Track root layer deltas for reporting overscroll.
2218 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422219 }
2220
[email protected]c1bb5af2013-03-13 19:06:272221 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272222 // Gesture events need to be transformed from viewport coordinates to local
2223 // layer coordinates so that the scrolling contents exactly follow the
2224 // user's finger. In contrast, wheel events represent a fixed amount of
2225 // scrolling so we can just apply them directly.
2226 if (!wheel_scrolling_) {
2227 float scale_from_viewport_to_screen_space = device_scale_factor_;
2228 applied_delta =
2229 ScrollLayerWithViewportSpaceDelta(layer_impl,
2230 scale_from_viewport_to_screen_space,
2231 viewport_point, pending_delta);
2232 } else {
2233 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142234 }
[email protected]94f206c12012-08-25 00:09:142235
[email protected]c1bb5af2013-03-13 19:06:272236 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292237 float move_threshold = 0.1f;
2238 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2239 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2240 did_scroll_x |= did_move_layer_x;
2241 did_scroll_y |= did_move_layer_y;
2242 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]db102612013-12-20 22:04:122243 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
[email protected]c1bb5af2013-03-13 19:06:272244 continue;
[email protected]db102612013-12-20 22:04:122245 else
2246 break;
[email protected]94f206c12012-08-25 00:09:142247 }
[email protected]a2b5ded2013-05-20 21:32:532248
[email protected]adeda572014-01-31 00:49:472249 if (layer_impl == InnerViewportScrollLayer())
[email protected]a2b5ded2013-05-20 21:32:532250 unused_root_delta.Subtract(applied_delta);
2251
[email protected]c1bb5af2013-03-13 19:06:272252 did_lock_scrolling_layer_ = true;
2253 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202254 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272255 break;
[email protected]94f206c12012-08-25 00:09:142256 }
[email protected]94f206c12012-08-25 00:09:142257
[email protected]c1bb5af2013-03-13 19:06:272258 // If the applied delta is within 45 degrees of the input delta, bail out to
2259 // make it easier to scroll just one layer in one direction without
2260 // affecting any of its parents.
2261 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212262 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272263 applied_delta, pending_delta) < angle_threshold) {
2264 pending_delta = gfx::Vector2d();
2265 break;
[email protected]4a23c374c2012-12-08 08:38:552266 }
[email protected]c1bb5af2013-03-13 19:06:272267
2268 // Allow further movement only on an axis perpendicular to the direction in
2269 // which the layer moved.
2270 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212271 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272272
[email protected]a2b5ded2013-05-20 21:32:532273 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272274 break;
2275 }
2276
[email protected]2bd503f2013-07-23 05:35:292277 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272278 if (did_scroll) {
2279 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442280 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272281 client_->RenewTreePriority();
2282 }
[email protected]a2b5ded2013-05-20 21:32:532283
[email protected]2bd503f2013-07-23 05:35:292284 // Scrolling along an axis resets accumulated root overscroll for that axis.
2285 if (did_scroll_x)
2286 accumulated_root_overscroll_.set_x(0);
2287 if (did_scroll_y)
2288 accumulated_root_overscroll_.set_y(0);
2289
[email protected]a2b5ded2013-05-20 21:32:532290 accumulated_root_overscroll_ += unused_root_delta;
2291 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2292 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112293 DidOverscrollParams params;
2294 params.accumulated_overscroll = accumulated_root_overscroll_;
2295 params.latest_overscroll_delta = unused_root_delta;
2296 params.current_fling_velocity = current_fling_velocity_;
2297 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532298 }
2299
[email protected]c1bb5af2013-03-13 19:06:272300 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552301}
2302
[email protected]be782f52013-03-23 21:36:142303// This implements scrolling by page as described here:
2304// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2305// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492306bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2307 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142308 DCHECK(wheel_scrolling_);
2309
2310 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2311 layer_impl;
2312 layer_impl = layer_impl->parent()) {
2313 if (!layer_impl->scrollable())
2314 continue;
2315
[email protected]adeda572014-01-31 00:49:472316 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142317 continue;
2318
[email protected]adeda572014-01-31 00:49:472319 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142320
2321 // These magical values match WebKit and are designed to scroll nearly the
2322 // entire visible content height but leave a bit of overlap.
2323 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492324 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142325 page = -page;
2326
2327 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2328
2329 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2330
2331 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142332 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442333 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142334 client_->RenewTreePriority();
2335 return true;
2336 }
2337
2338 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2339 }
2340
2341 return false;
2342}
2343
[email protected]1960a712013-04-30 17:06:472344void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2345 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2346 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2347 active_tree_->SetRootLayerScrollOffsetDelegate(
2348 root_layer_scroll_offset_delegate_);
2349}
2350
2351void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2352 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2353 client_->SetNeedsCommitOnImplThread();
2354}
2355
[email protected]c1bb5af2013-03-13 19:06:272356void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2357 active_tree_->ClearCurrentlyScrollingLayer();
2358 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532359 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202360 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142361}
2362
[email protected]c1bb5af2013-03-13 19:06:272363void LayerTreeHostImpl::ScrollEnd() {
2364 if (top_controls_manager_)
2365 top_controls_manager_->ScrollEnd();
2366 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052367 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142368}
2369
[email protected]5ff3c9782013-04-29 17:35:122370InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462371 if (!active_tree_->CurrentlyScrollingLayer())
2372 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212373
[email protected]75147f52013-08-02 19:11:462374 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472375 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2376 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462377 ClearCurrentlyScrollingLayer();
2378 return ScrollIgnored;
2379 }
2380
[email protected]db102612013-12-20 22:04:122381 if (!wheel_scrolling_) {
2382 // Allow the fling to lock to the first layer that moves after the initial
2383 // fling |ScrollBy()| event.
2384 did_lock_scrolling_layer_ = false;
2385 should_bubble_scrolls_ = false;
2386 }
[email protected]df0c42342013-10-08 20:52:122387
[email protected]75147f52013-08-02 19:11:462388 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212389}
2390
[email protected]3244c9132014-01-23 10:39:122391void LayerTreeHostImpl::NotifyCurrentFlingVelocity(
2392 const gfx::Vector2dF& velocity) {
[email protected]6e921bd2013-04-29 21:10:202393 current_fling_velocity_ = velocity;
2394}
2395
[email protected]bf1cfd9a2013-09-26 05:43:022396float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472397 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022398 LayerImpl* layer_impl) {
2399 if (!layer_impl)
2400 return std::numeric_limits<float>::max();
2401
2402 gfx::Rect layer_impl_bounds(
2403 layer_impl->content_bounds());
2404
2405 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2406 layer_impl->screen_space_transform(),
2407 layer_impl_bounds);
2408
2409 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2410 device_viewport_point);
2411}
2412
2413void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2414 if (!EnsureRenderSurfaceLayerList())
2415 return;
2416
[email protected]f620b0e72013-10-01 21:38:242417 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2418 device_scale_factor_);
2419
2420 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2421 device_viewport_point,
2422 active_tree_->RenderSurfaceLayerList());
2423 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2424 return;
2425
2426 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2427 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2428 scroll_layer_id_when_mouse_over_scrollbar_);
2429
[email protected]5cd9ac12014-02-05 21:48:532430 // The check for a null scroll_layer_impl below was added to see if it will
2431 // eliminate the crashes described in http://crbug.com/326635.
2432 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242433 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532434 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2435 : NULL;
[email protected]f620b0e72013-10-01 21:38:242436 if (animation_controller) {
2437 animation_controller->DidMouseMoveOffScrollbar(
2438 CurrentPhysicalTimeTicks());
2439 StartScrollbarAnimation();
2440 }
2441 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2442 }
2443
[email protected]bf1cfd9a2013-09-26 05:43:022444 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242445 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2446 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022447 &scroll_on_main_thread);
2448 if (scroll_on_main_thread || !scroll_layer_impl)
2449 return;
2450
2451 ScrollbarAnimationController* animation_controller =
2452 scroll_layer_impl->scrollbar_animation_controller();
2453 if (!animation_controller)
2454 return;
2455
[email protected]adeda572014-01-31 00:49:472456 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2457 float distance_to_scrollbar = std::numeric_limits<float>::max();
2458 for (LayerImpl::ScrollbarSet::iterator it =
2459 scroll_layer_impl->scrollbars()->begin();
2460 it != scroll_layer_impl->scrollbars()->end();
2461 ++it)
2462 distance_to_scrollbar =
2463 std::min(distance_to_scrollbar,
2464 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022465
2466 bool should_animate = animation_controller->DidMouseMoveNear(
2467 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202468 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022469 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022470}
2471
[email protected]f620b0e72013-10-01 21:38:242472bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472473 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242474 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2475 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2476 layer_impl = active_tree_->LayerById(scroll_layer_id);
2477 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2478 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202479 bool should_animate =
2480 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2481 CurrentPhysicalTimeTicks(), 0);
2482 if (should_animate)
2483 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242484 } else {
2485 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2486 }
2487
2488 return true;
2489 }
2490
2491 return false;
2492}
2493
[email protected]c1bb5af2013-03-13 19:06:272494void LayerTreeHostImpl::PinchGestureBegin() {
2495 pinch_gesture_active_ = true;
2496 previous_pinch_anchor_ = gfx::Point();
2497 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132498 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472499 if (active_tree_->OuterViewportScrollLayer()) {
2500 active_tree_->SetCurrentlyScrollingLayer(
2501 active_tree_->OuterViewportScrollLayer());
2502 } else {
2503 active_tree_->SetCurrentlyScrollingLayer(
2504 active_tree_->InnerViewportScrollLayer());
2505 }
[email protected]a9bda5e2013-10-25 18:43:372506 if (top_controls_manager_)
2507 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142508}
2509
[email protected]c1bb5af2013-03-13 19:06:272510void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2511 gfx::Point anchor) {
2512 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282513
[email protected]adeda572014-01-31 00:49:472514 DCHECK(InnerViewportScrollLayer());
[email protected]d3afa112012-12-08 06:24:282515
[email protected]c1bb5af2013-03-13 19:06:272516 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2517 // position over the course of the magnify.
2518 float page_scale_delta = active_tree_->page_scale_delta();
2519 gfx::PointF previous_scale_anchor =
2520 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2521 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2522 page_scale_delta = active_tree_->page_scale_delta();
2523 gfx::PointF new_scale_anchor =
2524 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2525 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2526
2527 previous_pinch_anchor_ = anchor;
2528
2529 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472530 // If clamping the inner viewport scroll offset causes a change, it should
2531 // be accounted for from the intended move.
2532 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272533
[email protected]adeda572014-01-31 00:49:472534 // We manually manage the bubbling behaviour here as it is different to that
2535 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2536 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2537 // 2) we don't want the directional limitations on the unused parts that
2538 // ScrollBy() implements, and
2539 // 3) pinching should not engage the top controls manager.
2540 gfx::Vector2dF unused = OuterViewportScrollLayer()
2541 ? OuterViewportScrollLayer()->ScrollBy(move)
2542 : move;
2543
2544 if (!unused.IsZero()) {
2545 InnerViewportScrollLayer()->ScrollBy(unused);
2546 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2547 }
[email protected]c1bb5af2013-03-13 19:06:272548
[email protected]c1bb5af2013-03-13 19:06:272549 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442550 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272551 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282552}
2553
[email protected]c1bb5af2013-03-13 19:06:272554void LayerTreeHostImpl::PinchGestureEnd() {
2555 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132556 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2557 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2558 ClearCurrentlyScrollingLayer();
2559 }
[email protected]a9bda5e2013-10-25 18:43:372560 if (top_controls_manager_)
2561 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272562 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142563}
2564
[email protected]c1bb5af2013-03-13 19:06:272565static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2566 LayerImpl* layer_impl) {
2567 if (!layer_impl)
2568 return;
[email protected]94f206c12012-08-25 00:09:142569
[email protected]c1bb5af2013-03-13 19:06:272570 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472571 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272572 if (!scroll_delta.IsZero()) {
2573 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392574 scroll.layer_id = layer_impl->id();
2575 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272576 scroll_info->scrolls.push_back(scroll);
2577 layer_impl->SetSentScrollDelta(scroll_delta);
2578 }
[email protected]94f206c12012-08-25 00:09:142579
[email protected]c1bb5af2013-03-13 19:06:272580 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2581 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142582}
2583
[email protected]c1bb5af2013-03-13 19:06:272584scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2585 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302586
[email protected]c1bb5af2013-03-13 19:06:272587 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392588 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2589 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302590
[email protected]c1bb5af2013-03-13 19:06:272591 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302592}
2593
[email protected]c1bb5af2013-03-13 19:06:272594void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282595 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102596}
2597
[email protected]adeda572014-01-31 00:49:472598void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2599 DCHECK(InnerViewportScrollLayer());
2600 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2601 ? OuterViewportScrollLayer()
2602 : InnerViewportScrollLayer();
2603
2604 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2605
2606 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2607 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2608}
2609
[email protected]c1bb5af2013-03-13 19:06:272610void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
[email protected]adeda572014-01-31 00:49:472611 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272612 return;
2613
2614 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472615 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272616
[email protected]34e1b512013-09-26 17:32:332617 if (!page_scale_animation_->IsAnimationStarted())
2618 page_scale_animation_->StartAnimation(monotonic_time);
2619
[email protected]c1bb5af2013-03-13 19:06:272620 active_tree_->SetPageScaleDelta(
2621 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2622 active_tree_->page_scale_factor());
2623 gfx::Vector2dF next_scroll =
2624 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2625
[email protected]adeda572014-01-31 00:49:472626 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442627 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272628
2629 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2630 page_scale_animation_.reset();
2631 client_->SetNeedsCommitOnImplThread();
2632 client_->RenewTreePriority();
2633 }
[email protected]829ad972013-01-28 23:36:102634}
2635
[email protected]ffb2720f2013-03-15 19:18:372636void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082637 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372638 return;
2639 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472640 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172641 return;
[email protected]5f7d9112014-01-17 21:31:422642 if (scroll.IsZero()) {
2643 // This may happen on the first animation step. Force redraw otherwise
2644 // the animation would stop because of no new frames.
2645 SetNeedsRedraw();
2646 } else {
[email protected]adeda572014-01-31 00:49:472647 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422648 scroll, 1.f / active_tree_->total_page_scale_factor()));
2649 }
[email protected]ffb2720f2013-03-15 19:18:372650}
2651
[email protected]c1bb5af2013-03-13 19:06:272652void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2653 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522654 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272655 animation_registrar_->active_animation_controllers().empty() ||
2656 !active_tree_->root_layer())
2657 return;
2658
2659 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2660
2661 last_animation_time_ = wall_clock_time;
2662 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2663
2664 AnimationRegistrar::AnimationControllerMap copy =
2665 animation_registrar_->active_animation_controllers();
2666 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2667 iter != copy.end();
2668 ++iter)
2669 (*iter).second->Animate(monotonic_seconds);
2670
[email protected]4e4136d2013-11-29 02:22:442671 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082672}
2673
[email protected]3d9f7432013-04-06 00:35:182674void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522675 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272676 animation_registrar_->active_animation_controllers().empty() ||
2677 !active_tree_->root_layer())
2678 return;
2679
2680 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2681 scoped_ptr<AnimationEventsVector> events =
2682 make_scoped_ptr(new AnimationEventsVector);
2683 AnimationRegistrar::AnimationControllerMap copy =
2684 animation_registrar_->active_animation_controllers();
2685 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2686 iter != copy.end();
2687 ++iter)
[email protected]3d9f7432013-04-06 00:35:182688 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272689
2690 if (!events->empty()) {
2691 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2692 last_animation_time_);
2693 }
[email protected]131a0c22013-02-12 18:31:082694}
2695
[email protected]c1bb5af2013-03-13 19:06:272696base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2697 return base::TimeDelta::FromSeconds(1);
2698}
2699
[email protected]50644642013-06-20 13:58:552700void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352701 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502702 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352703 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552704 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352705 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552706 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352707 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552708 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352709}
2710
[email protected]b5174d712013-08-28 08:10:432711void LayerTreeHostImpl::SetOffscreenContextProvider(
2712 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2713 if (!offscreen_context_provider.get()) {
2714 offscreen_context_provider_ = NULL;
2715 return;
2716 }
2717
2718 if (!offscreen_context_provider->BindToCurrentThread()) {
2719 offscreen_context_provider_ = NULL;
2720 return;
2721 }
2722
2723 offscreen_context_provider_ = offscreen_context_provider;
2724}
2725
[email protected]c1bb5af2013-03-13 19:06:272726std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2727 std::string str;
2728 if (active_tree_->root_layer()) {
2729 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2730 base::JSONWriter::WriteWithOptions(
2731 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2732 }
2733 return str;
2734}
2735
[email protected]c1bb5af2013-03-13 19:06:272736int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362737 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272738}
2739
[email protected]c1bb5af2013-03-13 19:06:272740void LayerTreeHostImpl::SendManagedMemoryStats(
2741 size_t memory_visible_bytes,
2742 size_t memory_visible_and_nearby_bytes,
2743 size_t memory_use_bytes) {
2744 if (!renderer_)
2745 return;
2746
2747 // Round the numbers being sent up to the next 8MB, to throttle the rate
2748 // at which we spam the GPU process.
2749 static const size_t rounding_step = 8 * 1024 * 1024;
2750 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2751 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2752 rounding_step);
2753 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2754 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2755 last_sent_memory_visible_and_nearby_bytes_ ==
2756 memory_visible_and_nearby_bytes &&
2757 last_sent_memory_use_bytes_ == memory_use_bytes) {
2758 return;
2759 }
2760 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2761 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2762 last_sent_memory_use_bytes_ = memory_use_bytes;
2763
2764 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2765 last_sent_memory_visible_and_nearby_bytes_,
2766 last_sent_memory_use_bytes_);
2767}
2768
2769void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2770 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2771}
2772
2773void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2774 base::TimeTicks time) {
2775 if (!layer)
2776 return;
2777
2778 ScrollbarAnimationController* scrollbar_controller =
2779 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352780 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202781 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422782 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2783 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442784 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202785 }
[email protected]c1bb5af2013-03-13 19:06:272786
2787 for (size_t i = 0; i < layer->children().size(); ++i)
2788 AnimateScrollbarsRecursive(layer->children()[i], time);
2789}
2790
[email protected]21c9dee72013-06-15 01:20:052791void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202792 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052793 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202794}
2795
2796void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2797 base::TimeTicks time) {
2798 if (!layer)
2799 return;
2800
2801 ScrollbarAnimationController* scrollbar_controller =
2802 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352803 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2804 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202805 if (delay > base::TimeDelta())
2806 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352807 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442808 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202809 }
2810
2811 for (size_t i = 0; i < layer->children().size(); ++i)
2812 StartScrollbarAnimationRecursive(layer->children()[i], time);
2813}
2814
[email protected]c1bb5af2013-03-13 19:06:272815void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2816 if (!tile_manager_)
2817 return;
2818
[email protected]1bcced22013-09-24 13:51:192819 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272820 return;
[email protected]1bcced22013-09-24 13:51:192821 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082822 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272823}
2824
[email protected]8347d692013-05-17 23:22:382825void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552826 current_frame_timeticks_ = base::TimeTicks();
2827 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272828}
2829
[email protected]21c9dee72013-06-15 01:20:052830void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2831 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552832 if (ticks->is_null()) {
2833 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052834 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552835 *now = base::Time::Now();
2836 }
2837}
2838
2839base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2840 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2841 return current_frame_timeticks_;
2842}
2843
2844base::Time LayerTreeHostImpl::CurrentFrameTime() {
2845 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272846 return current_frame_time_;
2847}
2848
[email protected]21c9dee72013-06-15 01:20:052849base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462850 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052851}
2852
[email protected]34806722013-08-09 23:51:212853scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2854 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272855 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222856 if (this->pending_tree_)
2857 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272858 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212859 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272860 if (tile_manager_)
2861 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2862 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222863 if (pending_tree_)
2864 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212865 if (frame)
2866 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222867 return state.PassAs<base::Value>();
2868}
2869
2870scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222871 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2872 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472873 if (tile_manager_)
2874 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272875 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082876}
2877
[email protected]6e7fdeb2013-07-09 14:28:382878void LayerTreeHostImpl::SetDebugState(
2879 const LayerTreeDebugState& new_debug_state) {
2880 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2881 return;
2882 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272883 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242884
[email protected]6e7fdeb2013-07-09 14:28:382885 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122886 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382887 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022888}
2889
[email protected]741fba422013-09-20 03:34:142890void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2891 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572892 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572893
[email protected]efa48412013-09-05 15:30:162894 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142895 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162896 case UIResourceBitmap::CLAMP_TO_EDGE:
2897 wrap_mode = GL_CLAMP_TO_EDGE;
2898 break;
2899 case UIResourceBitmap::REPEAT:
2900 wrap_mode = GL_REPEAT;
2901 break;
2902 }
2903
[email protected]c9280762013-08-01 06:28:572904 // Allow for multiple creation requests with the same UIResourceId. The
2905 // previous resource is simply deleted.
2906 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2907 if (id)
2908 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122909
2910 ResourceFormat format = resource_provider_->best_texture_format();
2911 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2912 format = ETC1;
[email protected]c9280762013-08-01 06:28:572913 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142914 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162915 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142916 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122917 format);
[email protected]c9280762013-08-01 06:28:572918
[email protected]efbdb3a2013-10-04 00:35:132919 UIResourceData data;
2920 data.resource_id = id;
2921 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512922 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132923
2924 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032925
2926 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572927 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032928 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142929 gfx::Rect(bitmap.GetSize()),
2930 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572931 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482932 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572933}
2934
2935void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2936 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2937 if (id) {
2938 resource_provider_->DeleteResource(id);
2939 ui_resource_map_.erase(uid);
2940 }
[email protected]127bdc1a2013-09-11 01:44:482941 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572942}
2943
[email protected]127bdc1a2013-09-11 01:44:482944void LayerTreeHostImpl::EvictAllUIResources() {
2945 if (ui_resource_map_.empty())
2946 return;
2947
[email protected]5f4dc4f2013-09-05 14:58:212948 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2949 iter != ui_resource_map_.end();
2950 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482951 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132952 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212953 }
2954 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482955
2956 client_->SetNeedsCommitOnImplThread();
2957 client_->OnCanDrawStateChanged(CanDraw());
2958 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212959}
2960
[email protected]c9280762013-08-01 06:28:572961ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2962 UIResourceId uid) const {
2963 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2964 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132965 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572966 return 0;
2967}
2968
[email protected]709c9542013-10-26 01:43:512969bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2970 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2971 DCHECK(iter != ui_resource_map_.end());
2972 return iter->second.opaque;
2973}
2974
[email protected]127bdc1a2013-09-11 01:44:482975bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2976 return !evicted_ui_resources_.empty();
2977}
2978
2979void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2980 std::set<UIResourceId>::iterator found_in_evicted =
2981 evicted_ui_resources_.find(uid);
2982 if (found_in_evicted == evicted_ui_resources_.end())
2983 return;
2984 evicted_ui_resources_.erase(found_in_evicted);
2985 if (evicted_ui_resources_.empty())
2986 client_->OnCanDrawStateChanged(CanDraw());
2987}
2988
[email protected]5e5648a2013-11-18 00:39:332989void LayerTreeHostImpl::ScheduleMicroBenchmark(
2990 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2991 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2992}
2993
[email protected]6be422b2013-12-08 06:47:312994void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2995 swap_promise_monitor_.insert(monitor);
2996}
2997
2998void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2999 swap_promise_monitor_.erase(monitor);
3000}
3001
3002void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3003 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3004 for (; it != swap_promise_monitor_.end(); it++)
3005 (*it)->OnSetNeedsRedrawOnImpl();
3006}
3007
[email protected]d3143c732012-10-05 19:17:593008} // namespace cc