blob: e8155dd92037857b1194fa6918776d2c8fae88e8 [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]7785e1002014-02-20 21:04:32649 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27650 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]3744e27b2013-11-06 21:44:08658 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27659 fill_rects.has_rect();
660 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32661 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]3744e27b2013-11-06 21:44:08662 // Skip the quad culler and just append the quads directly to avoid
663 // occlusion checks.
664 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
665 quad->SetNew(
[email protected]7785e1002014-02-20 21:04:32666 shared_quad_state, screen_space_rect, screen_background_color, false);
[email protected]3744e27b2013-11-06 21:44:08667 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
668 }
669 for (Region::Iterator fill_rects(overhang_region);
670 fill_rects.has_rect();
671 fill_rects.next()) {
672 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32673 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]3744e27b2013-11-06 21:44:08674 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
675 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
676 tex_quad->SetNew(
677 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32678 screen_space_rect,
679 screen_space_rect,
[email protected]3744e27b2013-11-06 21:44:08680 overhang_resource_id,
681 false,
[email protected]7785e1002014-02-20 21:04:32682 gfx::PointF(
683 screen_space_rect.x() / overhang_resource_scaled_size.width(),
684 screen_space_rect.y() / overhang_resource_scaled_size.height()),
685 gfx::PointF(
686 screen_space_rect.right() / overhang_resource_scaled_size.width(),
687 screen_space_rect.bottom() /
688 overhang_resource_scaled_size.height()),
[email protected]3744e27b2013-11-06 21:44:08689 screen_background_color,
690 vertex_opacity,
691 false);
692 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27693 }
[email protected]467b3612012-08-28 07:41:16694}
695
[email protected]7ddfe7e82014-01-30 07:22:11696DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::CalculateRenderPasses(
697 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27698 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11699 DCHECK(CanDraw());
700 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32701
[email protected]c1bb5af2013-03-13 19:06:27702 TrackDamageForAllSurfaces(active_tree_->root_layer(),
703 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14704
[email protected]e0341352013-04-06 05:01:20705 // If the root render surface has no visible damage, then don't generate a
706 // frame at all.
707 RenderSurfaceImpl* root_surface =
708 active_tree_->root_layer()->render_surface();
709 bool root_surface_has_no_visible_damage =
710 !root_surface->damage_tracker()->current_damage_rect().Intersects(
711 root_surface->content_rect());
712 bool root_surface_has_contributing_layers =
713 !root_surface->layer_list().empty();
714 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23715 root_surface_has_no_visible_damage &&
716 active_tree_->LayersWithCopyOutputRequest().empty()) {
[email protected]e0341352013-04-06 05:01:20717 TRACE_EVENT0("cc",
718 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
719 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07720 DCHECK(!output_surface_->capabilities()
721 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11722 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20723 }
724
[email protected]c1bb5af2013-03-13 19:06:27725 TRACE_EVENT1("cc",
726 "LayerTreeHostImpl::CalculateRenderPasses",
727 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06728 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14729
[email protected]c1bb5af2013-03-13 19:06:27730 // Create the render passes in dependency order.
731 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06732 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27733 --surface_index) {
734 LayerImpl* render_surface_layer =
735 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45736 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
737
738 bool should_draw_into_render_pass =
739 render_surface_layer->parent() == NULL ||
740 render_surface->contributes_to_drawn_surface() ||
741 render_surface_layer->HasCopyRequest();
742 if (should_draw_into_render_pass)
743 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27744 }
[email protected]94f206c12012-08-25 00:09:14745
[email protected]c1bb5af2013-03-13 19:06:27746 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52747 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27748 base::debug::TraceLog::GetInstance() &&
749 base::debug::TraceLog::GetInstance()->IsEnabled();
750 OcclusionTrackerImpl occlusion_tracker(
751 active_tree_->root_layer()->render_surface()->content_rect(),
752 record_metrics_for_frame);
753 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52754 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14755
[email protected]846f455b2013-03-18 19:07:41756 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27757 occlusion_tracker.set_occluding_screen_space_rects_container(
758 &frame->occluding_screen_space_rects);
759 }
[email protected]846f455b2013-03-18 19:07:41760 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27761 occlusion_tracker.set_non_occluding_screen_space_rects_container(
762 &frame->non_occluding_screen_space_rects);
763 }
[email protected]94f206c12012-08-25 00:09:14764
[email protected]b573e4e2014-02-14 02:54:00765 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27766 // occlusion and performing culling during the tree walk.
[email protected]b573e4e2014-02-14 02:54:00767 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14768
[email protected]c1bb5af2013-03-13 19:06:27769 // Typically when we are missing a texture and use a checkerboard quad, we
770 // still draw the frame. However when the layer being checkerboarded is moving
771 // due to an impl-animation, we drop the frame to avoid flashing due to the
772 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11773 DrawSwapReadbackResult::DrawResult draw_result =
774 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25775 // When we have a copy request for a layer, we need to draw no matter
776 // what, as the layer may disappear after this frame.
777 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14778
[email protected]372bad5f2013-03-21 16:38:43779 int layers_drawn = 0;
780
[email protected]ffbb2212013-06-02 23:47:59781 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
782
[email protected]c1bb5af2013-03-13 19:06:27783 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09784 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27785 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09786 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27787 it != end;
788 ++it) {
789 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09790 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27791 RenderPass* target_render_pass =
792 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14793
[email protected]e47b0a0a2013-11-18 23:26:22794 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14795
[email protected]30fe19ff2013-07-04 00:54:45796 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30797
[email protected]18a70192013-04-26 16:18:25798 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36799 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25800 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11801 it->TakeCopyRequestsAndTransformToTarget(
802 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25803 }
[email protected]30fe19ff2013-07-04 00:54:45804 } else if (it.represents_contributing_render_surface() &&
805 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27806 RenderPass::Id contributing_render_pass_id =
807 it->render_surface()->RenderPassId();
808 RenderPass* contributing_render_pass =
809 frame->render_passes_by_id[contributing_render_pass_id];
810 AppendQuadsForRenderSurfaceLayer(target_render_pass,
811 *it,
812 contributing_render_pass,
813 occlusion_tracker,
814 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45815 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09816 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27817 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15818 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27819 it->render_target(),
820 it->visible_content_rect(),
821 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45822 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15823 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27824 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59825
[email protected]c1bb5af2013-03-13 19:06:27826 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57827
[email protected]c1bb5af2013-03-13 19:06:27828 if (it->HasContributingDelegatedRenderPasses()) {
829 RenderPass::Id contributing_render_pass_id =
830 it->FirstContributingRenderPassId();
831 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
832 frame->render_passes_by_id.end()) {
833 RenderPass* render_pass =
834 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14835
[email protected]c1bb5af2013-03-13 19:06:27836 AppendQuadsData append_quads_data(render_pass->id);
837 AppendQuadsForLayer(render_pass,
838 *it,
839 occlusion_tracker,
840 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57841
[email protected]c1bb5af2013-03-13 19:06:27842 contributing_render_pass_id =
843 it->NextContributingRenderPassId(contributing_render_pass_id);
844 }
[email protected]94f206c12012-08-25 00:09:14845 }
846
[email protected]c1bb5af2013-03-13 19:06:27847 AppendQuadsForLayer(target_render_pass,
848 *it,
849 occlusion_tracker,
850 &append_quads_data);
851 }
[email protected]89228202012-08-29 03:20:30852
[email protected]372bad5f2013-03-21 16:38:43853 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14854 }
855
[email protected]e1e768f2013-03-26 08:48:09856 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27857 bool layer_has_animating_transform =
858 it->screen_space_transform_is_animating() ||
859 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11860 if (layer_has_animating_transform) {
861 draw_result =
862 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
863 }
[email protected]c1bb5af2013-03-13 19:06:27864 }
865
[email protected]3d609bb2014-02-01 01:10:23866 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27867 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23868 if (active_tree()->RequiresHighResToDraw())
869 draw_result =
870 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
871 }
[email protected]c1bb5af2013-03-13 19:06:27872
873 occlusion_tracker.LeaveLayer(it);
874 }
875
[email protected]6133cc232013-07-30 18:47:07876 if (have_copy_request ||
877 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11878 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25879
[email protected]1d993172012-10-18 18:15:04880#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27881 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
882 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
883 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
884 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
885 != frame->render_passes_by_id.end());
886 }
[email protected]94f206c12012-08-25 00:09:14887#endif
[email protected]c1bb5af2013-03-13 19:06:27888 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14889
[email protected]c1bb5af2013-03-13 19:06:27890 if (!active_tree_->has_transparent_background()) {
891 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08892 AppendQuadsToFillScreen(
893 ResourceIdForUIResource(overhang_ui_resource_id_),
894 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
895 active_tree_->RootScrollLayerDeviceViewportBounds(),
896 frame->render_passes.back(),
897 active_tree_->root_layer(),
898 active_tree_->background_color(),
899 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27900 }
[email protected]94f206c12012-08-25 00:09:14901
[email protected]7ddfe7e82014-01-30 07:22:11902 if (draw_result == DrawSwapReadbackResult::DRAW_SUCCESS)
[email protected]c1bb5af2013-03-13 19:06:27903 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25904 else
905 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14906
[email protected]c1bb5af2013-03-13 19:06:27907 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48908 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14909
[email protected]30fe19ff2013-07-04 00:54:45910 // Any copy requests left in the tree are not going to get serviced, and
911 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11912 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
913 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
914 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
915 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45916 }
[email protected]19334ac2013-12-12 23:59:11917 for (size_t i = 0; i < requests_to_abort.size(); ++i)
918 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45919
[email protected]e0341352013-04-06 05:01:20920 // If we're making a frame to draw, it better have at least one render pass.
921 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48922
923 // Should only have one render pass in resourceless software mode.
924 if (output_surface_->ForcedDrawToSoftwareDevice())
925 DCHECK_EQ(1u, frame->render_passes.size());
926
[email protected]7ddfe7e82014-01-30 07:22:11927 return draw_result;
[email protected]94f206c12012-08-25 00:09:14928}
929
[email protected]200a9c062013-05-20 04:34:37930void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
931 if (input_handler_client_)
932 input_handler_client_->MainThreadHasStoppedFlinging();
933}
934
[email protected]3d9f7432013-04-06 00:35:18935void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
936 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15937 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01938 if (should_background_tick)
939 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15940
[email protected]3d9f7432013-04-06 00:35:18941 bool enabled = should_background_tick &&
942 !animation_registrar_->active_animation_controllers().empty();
943
[email protected]c1bb5af2013-03-13 19:06:27944 // Lazily create the time_source adapter so that we can vary the interval for
945 // testing.
946 if (!time_source_client_adapter_) {
947 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
948 this,
[email protected]810d40b72013-06-20 18:26:15949 DelayBasedTimeSource::Create(
950 LowFrequencyAnimationInterval(),
951 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
952 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27953 }
[email protected]94f206c12012-08-25 00:09:14954
[email protected]c1bb5af2013-03-13 19:06:27955 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14956}
957
[email protected]b8384e22013-12-03 02:20:48958void LayerTreeHostImpl::DidAnimateScrollOffset() {
959 client_->SetNeedsCommitOnImplThread();
960 client_->RenewTreePriority();
961}
962
[email protected]0023fc72014-01-10 20:05:06963void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02964 viewport_damage_rect_.Union(damage_rect);
965}
966
[email protected]c1bb5af2013-03-13 19:06:27967static inline RenderPass* FindRenderPassById(
968 RenderPass::Id render_pass_id,
969 const LayerTreeHostImpl::FrameData& frame) {
970 RenderPassIdHashMap::const_iterator it =
971 frame.render_passes_by_id.find(render_pass_id);
972 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14973}
974
[email protected]c1bb5af2013-03-13 19:06:27975static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
976 LayerTreeHostImpl::FrameData* frame) {
977 RenderPass* remove_render_pass =
978 FindRenderPassById(remove_render_pass_id, *frame);
979 // The pass was already removed by another quad - probably the original, and
980 // we are the replica.
981 if (!remove_render_pass)
982 return;
983 RenderPassList& render_passes = frame->render_passes;
984 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
985 render_passes.end(),
986 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14987
[email protected]c1bb5af2013-03-13 19:06:27988 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14989
[email protected]c1bb5af2013-03-13 19:06:27990 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
991 frame->render_passes.erase(to_remove);
992 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14993
[email protected]c1bb5af2013-03-13 19:06:27994 // Now follow up for all RenderPass quads and remove their RenderPasses
995 // recursively.
996 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29997 QuadList::ConstBackToFrontIterator quad_list_iterator =
998 quad_list.BackToFrontBegin();
999 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271000 ++quad_list_iterator) {
1001 DrawQuad* current_quad = (*quad_list_iterator);
1002 if (current_quad->material != DrawQuad::RENDER_PASS)
1003 continue;
[email protected]94f206c12012-08-25 00:09:141004
[email protected]c1bb5af2013-03-13 19:06:271005 RenderPass::Id next_remove_render_pass_id =
1006 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1007 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1008 }
[email protected]94f206c12012-08-25 00:09:141009}
1010
[email protected]c1bb5af2013-03-13 19:06:271011bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1012 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1013 const RenderPass* render_pass =
1014 FindRenderPassById(quad.render_pass_id, frame);
1015 if (!render_pass)
1016 return false;
[email protected]94f206c12012-08-25 00:09:141017
[email protected]c1bb5af2013-03-13 19:06:271018 // If any quad or RenderPass draws into this RenderPass, then keep it.
1019 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291020 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1021 quad_list.BackToFrontBegin();
1022 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271023 ++quad_list_iterator) {
1024 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141025
[email protected]c1bb5af2013-03-13 19:06:271026 if (current_quad->material != DrawQuad::RENDER_PASS)
1027 return false;
[email protected]94f206c12012-08-25 00:09:141028
[email protected]c1bb5af2013-03-13 19:06:271029 const RenderPass* contributing_pass = FindRenderPassById(
1030 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1031 if (contributing_pass)
1032 return false;
1033 }
1034 return true;
[email protected]94f206c12012-08-25 00:09:141035}
1036
1037// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271038template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271039 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1040 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141041
1042// static
[email protected]c1bb5af2013-03-13 19:06:271043template <typename RenderPassCuller>
1044void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1045 FrameData* frame) {
1046 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1047 it != culler.RenderPassListEnd(frame->render_passes);
1048 it = culler.RenderPassListNext(it)) {
1049 const RenderPass* current_pass = frame->render_passes[it];
1050 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291051 QuadList::ConstBackToFrontIterator quad_list_iterator =
1052 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141053
[email protected]ed511b8d2013-03-25 03:29:291054 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271055 ++quad_list_iterator) {
1056 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141057
[email protected]c1bb5af2013-03-13 19:06:271058 if (current_quad->material != DrawQuad::RENDER_PASS)
1059 continue;
[email protected]94f206c12012-08-25 00:09:141060
[email protected]c1bb5af2013-03-13 19:06:271061 const RenderPassDrawQuad* render_pass_quad =
1062 RenderPassDrawQuad::MaterialCast(current_quad);
1063 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1064 continue;
[email protected]94f206c12012-08-25 00:09:141065
[email protected]c1bb5af2013-03-13 19:06:271066 // We are changing the vector in the middle of iteration. Because we
1067 // delete render passes that draw into the current pass, we are
1068 // guaranteed that any data from the iterator to the end will not
1069 // change. So, capture the iterator position from the end of the
1070 // list, and restore it after the change.
1071 size_t position_from_end = frame->render_passes.size() - it;
1072 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1073 it = frame->render_passes.size() - position_from_end;
1074 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141075 }
[email protected]c1bb5af2013-03-13 19:06:271076 }
[email protected]94f206c12012-08-25 00:09:141077}
1078
[email protected]7ddfe7e82014-01-30 07:22:111079DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::PrepareToDraw(
1080 FrameData* frame,
1081 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471082 TRACE_EVENT1("cc",
1083 "LayerTreeHostImpl::PrepareToDraw",
1084 "SourceFrameNumber",
1085 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141086
[email protected]425e5332013-09-21 04:10:031087 if (need_to_update_visible_tiles_before_draw_ &&
1088 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1089 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191090 }
[email protected]425e5332013-09-21 04:10:031091 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191092
[email protected]7d19dc342013-05-02 22:02:041093 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271094
[email protected]c1bb5af2013-03-13 19:06:271095 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1096 frame->render_passes.clear();
1097 frame->render_passes_by_id.clear();
1098 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201099 frame->contains_incomplete_tile = false;
1100 frame->has_no_damage = false;
1101
[email protected]0023fc72014-01-10 20:05:061102 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201103 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021104 device_viewport_damage_rect.Union(viewport_damage_rect_);
1105 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201106
1107 active_tree_->root_layer()->render_surface()->damage_tracker()->
1108 AddDamageNextUpdate(device_viewport_damage_rect);
1109 }
[email protected]94f206c12012-08-25 00:09:141110
[email protected]7ddfe7e82014-01-30 07:22:111111 DrawSwapReadbackResult::DrawResult draw_result = CalculateRenderPasses(frame);
1112 if (draw_result != DrawSwapReadbackResult::DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071113 DCHECK(!output_surface_->capabilities()
1114 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111115 return draw_result;
[email protected]6133cc232013-07-30 18:47:071116 }
[email protected]94f206c12012-08-25 00:09:141117
[email protected]7ddfe7e82014-01-30 07:22:111118 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1119 // this function is called again.
1120 return draw_result;
[email protected]94f206c12012-08-25 00:09:141121}
1122
[email protected]0309ba5e02013-06-26 04:11:081123void LayerTreeHostImpl::EvictTexturesForTesting() {
1124 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1125}
1126
[email protected]4f48f6e2013-08-27 06:33:381127void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1128 NOTREACHED();
1129}
1130
[email protected]425e5332013-09-21 04:10:031131void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1132 DidInitializeVisibleTile();
1133}
1134
[email protected]c1bb5af2013-03-13 19:06:271135void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1136 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121137
[email protected]c1bb5af2013-03-13 19:06:271138 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351139 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351140 ManagedMemoryPolicy::PriorityCutoffToValue(
1141 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351142 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271143 if (evicted_resources) {
1144 active_tree_->SetContentsTexturesPurged();
1145 if (pending_tree_)
1146 pending_tree_->SetContentsTexturesPurged();
1147 client_->SetNeedsCommitOnImplThread();
1148 client_->OnCanDrawStateChanged(CanDraw());
1149 client_->RenewTreePriority();
1150 }
1151 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431152
[email protected]8be1a9bf2013-05-01 03:45:191153 UpdateTileManagerMemoryPolicy(policy);
1154}
1155
1156void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1157 const ManagedMemoryPolicy& policy) {
1158 if (!tile_manager_)
1159 return;
1160
[email protected]8be1a9bf2013-05-01 03:45:191161 // TODO(reveman): We should avoid keeping around unused resources if
1162 // possible. crbug.com/224475
[email protected]25489c02014-02-13 23:18:411163 global_tile_state_.hard_memory_limit_in_bytes = 0;
1164 global_tile_state_.soft_memory_limit_in_bytes = 0;
1165 if (visible_ && policy.bytes_limit_when_visible > 0) {
1166 global_tile_state_.hard_memory_limit_in_bytes =
1167 policy.bytes_limit_when_visible;
1168 global_tile_state_.soft_memory_limit_in_bytes =
[email protected]831414e2014-02-22 02:27:371169 (static_cast<int64>(global_tile_state_.hard_memory_limit_in_bytes) *
1170 settings_.max_memory_for_prepaint_percentage) /
1171 100;
[email protected]25489c02014-02-13 23:18:411172 }
1173 // Unused limit is calculated from soft-limit, as hard-limit may
1174 // be very high and shouldn't typically be exceeded.
[email protected]1bcced22013-09-24 13:51:191175 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
[email protected]25489c02014-02-13 23:18:411176 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1177 settings_.max_unused_resource_memory_percentage) /
1178 100);
[email protected]1bcced22013-09-24 13:51:191179 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191180 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1181 visible_ ?
1182 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351183 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191184 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081185
[email protected]c48536a52013-09-14 00:02:081186 DidModifyTilePriorities();
1187}
1188
1189void LayerTreeHostImpl::DidModifyTilePriorities() {
1190 DCHECK(settings_.impl_side_painting);
1191 // Mark priorities as dirty and schedule a ManageTiles().
1192 tile_priorities_dirty_ = true;
1193 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141194}
1195
[email protected]86126792013-03-16 20:07:541196void LayerTreeHostImpl::DidInitializeVisibleTile() {
1197 // TODO(reveman): Determine tiles that changed and only damage
1198 // what's necessary.
1199 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031200 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541201 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471202}
1203
[email protected]94bf75c2013-06-12 13:20:041204void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381205 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041206}
1207
[email protected]3f2ff112013-08-03 02:41:071208void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1209 SetManagedMemoryPolicy(policy, zero_budget_);
1210}
1211
[email protected]5f07afc2013-07-19 05:17:181212void LayerTreeHostImpl::SetTreeActivationCallback(
1213 const base::Closure& callback) {
1214 DCHECK(proxy_->IsImplThread());
1215 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261216 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181217}
1218
[email protected]c1bb5af2013-03-13 19:06:271219void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071220 const ManagedMemoryPolicy& policy, bool zero_budget) {
1221 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271222 return;
[email protected]61de5812012-11-08 07:03:441223
[email protected]3f2ff112013-08-03 02:41:071224 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1225
1226 cached_managed_memory_policy_ = policy;
1227 zero_budget_ = zero_budget;
1228 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1229
1230 if (old_policy == actual_policy)
1231 return;
1232
1233 if (!proxy_->HasImplThread()) {
1234 // In single-thread mode, this can be called on the main thread by
1235 // GLRenderer::OnMemoryAllocationChanged.
1236 DebugScopedSetImplThread impl_thread(proxy_);
1237 EnforceManagedMemoryPolicy(actual_policy);
1238 } else {
1239 DCHECK(proxy_->IsImplThread());
1240 EnforceManagedMemoryPolicy(actual_policy);
1241 }
[email protected]50644642013-06-20 13:58:551242
[email protected]d7626ffd2013-03-29 00:17:421243 // If there is already enough memory to draw everything imaginable and the
1244 // new memory limit does not change this, then do not re-commit. Don't bother
1245 // skipping commits if this is not visible (commits don't happen when not
1246 // visible, there will almost always be a commit when this becomes visible).
1247 bool needs_commit = true;
1248 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071249 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1250 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1251 actual_policy.priority_cutoff_when_visible ==
1252 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421253 needs_commit = false;
1254 }
1255
[email protected]d7626ffd2013-03-29 00:17:421256 if (needs_commit)
1257 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141258}
1259
[email protected]f224cc92013-06-06 23:23:321260void LayerTreeHostImpl::SetExternalDrawConstraints(
1261 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061262 const gfx::Rect& viewport,
1263 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281264 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321265 external_transform_ = transform;
1266 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281267 external_clip_ = clip;
1268 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321269}
1270
[email protected]0023fc72014-01-10 20:05:061271void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311272 if (damage_rect.IsEmpty())
1273 return;
1274 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031275 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1276}
1277
[email protected]daea3d42013-10-23 17:04:501278void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1279 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451280}
1281
[email protected]4d7e46a2013-11-08 05:33:401282void LayerTreeHostImpl::DidSwapBuffers() {
1283 client_->DidSwapBuffersOnImplThread();
1284}
1285
[email protected]a7335e0b2013-09-18 09:34:511286void LayerTreeHostImpl::OnSwapBuffersComplete() {
1287 client_->OnSwapBuffersCompleteOnImplThread();
1288}
1289
1290void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271291 // TODO(piman): We may need to do some validation on this ack before
1292 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511293 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291294 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161295}
1296
[email protected]c1bb5af2013-03-13 19:06:271297void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1298 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291299}
1300
[email protected]c1bb5af2013-03-13 19:06:271301CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1302 CompositorFrameMetadata metadata;
1303 metadata.device_scale_factor = device_scale_factor_;
1304 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1305 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1306 metadata.root_layer_size = active_tree_->ScrollableSize();
1307 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1308 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1309 if (top_controls_manager_) {
1310 metadata.location_bar_offset =
1311 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1312 metadata.location_bar_content_translation =
1313 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011314 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271315 }
[email protected]bf189f62012-12-18 03:42:111316
[email protected]adeda572014-01-31 00:49:471317 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111318 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271319
[email protected]adeda572014-01-31 00:49:471320 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271321
1322 return metadata;
[email protected]bf189f62012-12-18 03:42:111323}
1324
[email protected]6f50b8fa2013-07-01 19:51:491325static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1326 layer->DidBeginTracing();
1327}
[email protected]37349bc2013-06-04 01:31:521328
[email protected]f0c2a242013-03-15 19:34:521329void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1330 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351331 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271332 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201333
[email protected]3519b872013-07-30 07:17:501334 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191335 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071336 DCHECK(!output_surface_->capabilities()
1337 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201338 return;
[email protected]3519b872013-07-30 07:17:501339 }
[email protected]e0341352013-04-06 05:01:201340
[email protected]c1bb5af2013-03-13 19:06:271341 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141342
[email protected]213fcfc2013-08-24 14:59:271343 fps_counter_->SaveTimeStamp(frame_begin_time,
1344 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351345 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091346 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351347 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431348
[email protected]c1bb5af2013-03-13 19:06:271349 if (tile_manager_) {
1350 memory_history_->SaveEntry(
1351 tile_manager_->memory_stats_from_last_assign());
1352 }
[email protected]1191d9d2013-02-02 06:00:331353
[email protected]846f455b2013-03-18 19:07:411354 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021355 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271356 active_tree_->root_layer(),
1357 *frame->render_surface_layer_list,
1358 frame->occluding_screen_space_rects,
1359 frame->non_occluding_screen_space_rects,
1360 debug_state_);
1361 }
[email protected]94f206c12012-08-25 00:09:141362
[email protected]a848c102013-03-26 08:59:091363 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1364 const RenderingStats& stats =
1365 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181366 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091367 }
1368
[email protected]37349bc2013-06-04 01:31:521369 bool is_new_trace;
1370 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1371 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491372 if (pending_tree_) {
1373 LayerTreeHostCommon::CallFunctionForSubtree(
1374 pending_tree_->root_layer(),
1375 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1376 }
1377 LayerTreeHostCommon::CallFunctionForSubtree(
1378 active_tree_->root_layer(),
1379 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521380 }
1381
[email protected]9197dbcb2013-05-15 20:28:511382 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211383 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1384 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1385 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081386
[email protected]c1bb5af2013-03-13 19:06:271387 // Because the contents of the HUD depend on everything else in the frame, the
1388 // contents of its texture are updated as the last thing before the frame is
1389 // drawn.
[email protected]671f6672013-10-01 07:31:071390 if (active_tree_->hud_layer()) {
1391 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531392 active_tree_->hud_layer()->UpdateHudTexture(
1393 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071394 }
[email protected]94f206c12012-08-25 00:09:141395
[email protected]b09c1942013-05-10 00:06:371396 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321397 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261398 bool disable_picture_quad_image_filtering =
1399 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321400
[email protected]b09c1942013-05-10 00:06:371401 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321402 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301403 temp_software_renderer->DrawFrame(&frame->render_passes,
1404 NULL,
1405 device_scale_factor_,
1406 DeviceViewport(),
1407 DeviceClip(),
1408 allow_partial_swap,
1409 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371410 } else {
[email protected]573e8e32013-09-09 17:24:321411 // We don't track damage on the HUD layer (it interacts with damage tracking
1412 // visualizations), so disable partial swaps to make the HUD layer display
1413 // properly.
1414 bool allow_partial_swap = !debug_state_.ShowHudRects();
1415
[email protected]b5174d712013-08-28 08:10:431416 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321417 offscreen_context_provider_.get(),
1418 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301419 DeviceViewport(),
1420 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261421 allow_partial_swap,
1422 false);
[email protected]b09c1942013-05-10 00:06:371423 }
[email protected]c1bb5af2013-03-13 19:06:271424 // The render passes should be consumed by the renderer.
1425 DCHECK(frame->render_passes.empty());
1426 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141427
[email protected]c1bb5af2013-03-13 19:06:271428 // The next frame should start by assuming nothing has changed, and changes
1429 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321430 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1431 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541432 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271433 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1434 DidDrawDamagedArea();
1435 }
1436 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351437
[email protected]d73094482014-02-13 21:28:281438 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331439 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1440 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181441 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141442}
1443
[email protected]c1bb5af2013-03-13 19:06:271444void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1445 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1446 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521447
[email protected]c1bb5af2013-03-13 19:06:271448 // Once all layers have been drawn, pending texture uploads should no
1449 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151450 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141451}
1452
[email protected]c1bb5af2013-03-13 19:06:271453void LayerTreeHostImpl::FinishAllRendering() {
1454 if (renderer_)
1455 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141456}
1457
[email protected]c1bb5af2013-03-13 19:06:271458bool LayerTreeHostImpl::IsContextLost() {
1459 DCHECK(proxy_->IsImplThread());
1460 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141461}
1462
[email protected]7a8bcd262014-01-15 12:54:581463const RendererCapabilitiesImpl&
1464LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271465 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141466}
1467
[email protected]e0341352013-04-06 05:01:201468bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231469 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191470 if (frame.has_no_damage) {
1471 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201472 return false;
[email protected]b69c1db2013-11-27 00:05:191473 }
[email protected]4a28a612013-11-27 02:06:331474 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551475 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331476 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161477 return true;
[email protected]94f206c12012-08-25 00:09:141478}
1479
[email protected]daea3d42013-10-23 17:04:501480void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451481 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501482 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451483}
1484
[email protected]54af03522013-09-05 00:43:281485gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341486 // Use the root container layer bounds if it clips to them, otherwise, the
1487 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441488 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341489 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441490 DCHECK(!top_controls_manager_);
1491 DCHECK_EQ(0, overdraw_bottom_height_);
1492 return container_layer->bounds();
1493 }
1494
[email protected]ffb2720f2013-03-15 19:18:371495 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281496 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371497
[email protected]ed511b8d2013-03-25 03:29:291498 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371499 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401500 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291501 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371502}
1503
[email protected]c1bb5af2013-03-13 19:06:271504void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191505 if (resource_provider_)
1506 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011507 // TODO(jamesr): The renderer_ check is needed to make some of the
1508 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1509 // important) in production. We should adjust the test to not need this.
1510 if (renderer_)
1511 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]ce2e8112013-11-28 07:44:361512#ifndef NDEBUG
1513 did_lose_called_ = true;
1514#endif
[email protected]94f206c12012-08-25 00:09:141515}
1516
[email protected]c1bb5af2013-03-13 19:06:271517void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061518 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271519 DCHECK(renderer_);
1520 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141521}
1522
[email protected]59adb112013-04-09 04:48:441523bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471524 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011525}
1526
[email protected]c1bb5af2013-03-13 19:06:271527LayerImpl* LayerTreeHostImpl::RootLayer() const {
1528 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081529}
1530
[email protected]adeda572014-01-31 00:49:471531LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1532 return active_tree_->InnerViewportScrollLayer();
1533}
1534
1535LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1536 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081537}
1538
[email protected]c1bb5af2013-03-13 19:06:271539LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1540 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081541}
1542
[email protected]251699b2013-10-09 00:21:261543bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1544 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471545 (InnerViewportScrollLayer() &&
1546 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1547 (OuterViewportScrollLayer() &&
1548 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261549}
1550
[email protected]94f206c12012-08-25 00:09:141551// Content layers can be either directly scrollable or contained in an outer
1552// scrolling layer which applies the scroll transform. Given a content layer,
1553// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271554static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1555 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021556 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271557
1558 if (layer_impl->scrollable())
1559 return layer_impl;
1560
1561 if (layer_impl->DrawsContent() &&
1562 layer_impl->parent() &&
1563 layer_impl->parent()->scrollable())
1564 return layer_impl->parent();
1565
[email protected]bf1cfd9a2013-09-26 05:43:021566 return NULL;
[email protected]94f206c12012-08-25 00:09:141567}
1568
[email protected]c1bb5af2013-03-13 19:06:271569void LayerTreeHostImpl::CreatePendingTree() {
1570 CHECK(!pending_tree_);
1571 if (recycle_tree_)
1572 recycle_tree_.swap(pending_tree_);
1573 else
1574 pending_tree_ = LayerTreeImpl::create(this);
1575 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111576 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271577}
1578
[email protected]8bb6cfa2013-07-23 00:11:191579void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191580 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1581 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191582 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481583}
1584
[email protected]c1bb5af2013-03-13 19:06:271585void LayerTreeHostImpl::ActivatePendingTree() {
1586 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111587 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351588
[email protected]4f48f6e2013-08-27 06:33:381589 need_to_update_visible_tiles_before_draw_ = true;
1590
[email protected]1960a712013-04-30 17:06:471591 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271592 active_tree_->PushPersistedState(pending_tree_.get());
1593 if (pending_tree_->needs_full_tree_sync()) {
1594 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331595 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271596 active_tree_->DetachLayerTree(),
1597 active_tree_.get()));
1598 }
[email protected]b5651c22013-03-14 15:06:331599 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271600 active_tree_->root_layer());
1601 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511602
[email protected]c9280762013-08-01 06:28:571603 // Process any requests in the UI resource queue. The request queue is given
1604 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1605 // the swap.
1606 pending_tree_->ProcessUIResourceRequestQueue();
1607
[email protected]c1bb5af2013-03-13 19:06:271608 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511609
[email protected]c1bb5af2013-03-13 19:06:271610 // Now that we've synced everything from the pending tree to the active
1611 // tree, rename the pending tree the recycle tree so we can reuse it on the
1612 // next sync.
1613 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511614
[email protected]7d1b07e2013-10-01 17:31:301615 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471616 active_tree_->SetRootLayerScrollOffsetDelegate(
1617 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221618
[email protected]c1bb5af2013-03-13 19:06:271619 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441620 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271621 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241622
[email protected]a848c102013-03-26 08:59:091623 if (debug_state_.continuous_painting) {
1624 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431625 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091626 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1627 stats.main_stats.record_time +
1628 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271629 }
[email protected]2a61ad52013-05-13 14:01:291630
1631 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261632 if (!tree_activation_callback_.is_null())
1633 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011634
1635 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1636 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281637 devtools_instrumentation::DidActivateLayerTree(
1638 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271639}
1640
[email protected]c1bb5af2013-03-13 19:06:271641void LayerTreeHostImpl::SetVisible(bool visible) {
1642 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141643
[email protected]c1bb5af2013-03-13 19:06:271644 if (visible_ == visible)
1645 return;
1646 visible_ = visible;
1647 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121648 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141649
[email protected]3d609bb2014-02-01 01:10:231650 if (!visible_) {
1651 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481652 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231653 }
[email protected]127bdc1a2013-09-11 01:44:481654
[email protected]0fa42e02013-08-20 01:16:051655 // Evict tiles immediately if invisible since this tab may never get another
1656 // draw or timer tick.
1657 if (!visible_)
1658 ManageTiles();
1659
[email protected]c1bb5af2013-03-13 19:06:271660 if (!renderer_)
1661 return;
[email protected]94f206c12012-08-25 00:09:141662
[email protected]c1bb5af2013-03-13 19:06:271663 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141664}
1665
[email protected]6be422b2013-12-08 06:47:311666void LayerTreeHostImpl::SetNeedsRedraw() {
1667 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1668 client_->SetNeedsRedrawOnImplThread();
1669}
1670
[email protected]206a3922013-05-17 06:34:121671ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071672 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011673 // TODO(ernstm): The second condition disables pre-painting for all layers
1674 // when GPU rasterization is enabled. Once we selectively enable GPU
1675 // rasterization per layer, we also need to disable pre-painting selectively:
1676 // crbug.com/335387
1677 if (debug_state_.rasterize_only_visible_content ||
1678 settings_.gpu_rasterization) {
[email protected]3f2ff112013-08-03 02:41:071679 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291680 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071681 }
[email protected]206a3922013-05-17 06:34:121682
[email protected]3f2ff112013-08-03 02:41:071683 if (zero_budget_) {
1684 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071685 }
1686
[email protected]206a3922013-05-17 06:34:121687 return actual;
1688}
1689
[email protected]3f2ff112013-08-03 02:41:071690size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1691 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1692}
1693
[email protected]990e050a2013-09-23 18:50:211694int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1695 return ManagedMemoryPolicy::PriorityCutoffToValue(
1696 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1697}
1698
[email protected]50644642013-06-20 13:58:551699void LayerTreeHostImpl::ReleaseTreeResources() {
1700 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551701 SendReleaseResourcesRecursive(active_tree_->root_layer());
1702 if (pending_tree_ && pending_tree_->root_layer())
1703 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1704 if (recycle_tree_ && recycle_tree_->root_layer())
1705 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571706
[email protected]127bdc1a2013-09-11 01:44:481707 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551708}
1709
1710void LayerTreeHostImpl::CreateAndSetRenderer(
1711 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201712 ResourceProvider* resource_provider,
1713 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551714 DCHECK(!renderer_);
1715 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321716 renderer_ = DelegatingRenderer::Create(
1717 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091718 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551719 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321720 &settings_,
[email protected]50644642013-06-20 13:58:551721 output_surface,
1722 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111723 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241724 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551725 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321726 renderer_ = SoftwareRenderer::Create(
1727 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551728 }
1729
[email protected]84975832013-06-29 00:24:111730 if (renderer_) {
[email protected]50644642013-06-20 13:58:551731 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111732 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561733
1734 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1735 // initialized to get max texture size. Also, after releasing resources,
1736 // trees need another update to generate new ones.
1737 active_tree_->set_needs_update_draw_properties();
1738 if (pending_tree_)
1739 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591740 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111741 }
[email protected]50644642013-06-20 13:58:551742}
1743
[email protected]50af3c22013-07-13 03:50:201744void LayerTreeHostImpl::CreateAndSetTileManager(
1745 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011746 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371747 bool using_map_image,
1748 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201749 DCHECK(settings_.impl_side_painting);
1750 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011751 tile_manager_ =
1752 TileManager::Create(this,
1753 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281754 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011755 rendering_stats_instrumentation_,
1756 using_map_image,
[email protected]4e472a42014-02-11 01:56:371757 allow_rasterize_on_demand,
[email protected]b4664142013-11-08 00:50:311758 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121759 GetMaxRasterTasksUsageBytes(context_provider),
1760 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011761
[email protected]50af3c22013-07-13 03:50:201762 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191763 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201764}
1765
[email protected]50644642013-06-20 13:58:551766void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071767 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551768}
1769
[email protected]c1bb5af2013-03-13 19:06:271770bool LayerTreeHostImpl::InitializeRenderer(
1771 scoped_ptr<OutputSurface> output_surface) {
[email protected]ce2e8112013-11-28 07:44:361772#ifndef NDEBUG
1773 DCHECK(!renderer_ || did_lose_called_);
1774#endif
1775
[email protected]c1bb5af2013-03-13 19:06:271776 // Since we will create a new resource provider, we cannot continue to use
1777 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1778 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551779 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401780
[email protected]c1bb5af2013-03-13 19:06:271781 // Note: order is important here.
1782 renderer_.reset();
1783 tile_manager_.reset();
1784 resource_provider_.reset();
1785 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141786
[email protected]c1bb5af2013-03-13 19:06:271787 if (!output_surface->BindToClient(this))
1788 return false;
[email protected]be3181652012-09-25 13:02:131789
[email protected]a7f35682013-10-22 23:05:571790 scoped_ptr<ResourceProvider> resource_provider =
1791 ResourceProvider::Create(output_surface.get(),
1792 shared_bitmap_manager_,
1793 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341794 settings_.use_rgba_4444_textures,
1795 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551796 if (!resource_provider)
1797 return false;
[email protected]2b154b22013-06-07 09:03:271798
[email protected]50644642013-06-20 13:58:551799 if (output_surface->capabilities().deferred_gl_initialization)
1800 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131801
[email protected]50af3c22013-07-13 03:50:201802 bool skip_gl_renderer = false;
1803 CreateAndSetRenderer(
1804 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131805
[email protected]50644642013-06-20 13:58:551806 if (!renderer_)
1807 return false;
[email protected]6123930c2013-05-23 06:48:371808
[email protected]50644642013-06-20 13:58:551809 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371810 CreateAndSetTileManager(
1811 resource_provider.get(),
1812 output_surface->context_provider().get(),
1813 GetRendererCapabilities().using_map_image,
1814 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241815 }
1816
[email protected]daea3d42013-10-23 17:04:501817 // Setup BeginImplFrameEmulation if it's not supported natively
1818 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351819 const base::TimeDelta display_refresh_interval =
1820 base::TimeDelta::FromMicroseconds(
1821 base::Time::kMicrosecondsPerSecond /
1822 settings_.refresh_rate);
1823
[email protected]daea3d42013-10-23 17:04:501824 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151825 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351826 settings_.throttle_frame_production,
1827 display_refresh_interval);
1828 }
1829
1830 int max_frames_pending =
1831 output_surface->capabilities().max_frames_pending;
1832 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351833 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351834 output_surface->SetMaxFramesPending(max_frames_pending);
1835
[email protected]50644642013-06-20 13:58:551836 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271837 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141838
[email protected]c1bb5af2013-03-13 19:06:271839 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211840
[email protected]c1bb5af2013-03-13 19:06:271841 return true;
[email protected]94f206c12012-08-25 00:09:141842}
1843
[email protected]2b154b22013-06-07 09:03:271844bool LayerTreeHostImpl::DeferredInitialize(
1845 scoped_refptr<ContextProvider> offscreen_context_provider) {
1846 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551847 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091848 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271849
[email protected]50644642013-06-20 13:58:551850 ReleaseTreeResources();
1851 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271852
[email protected]d9ca99e2013-08-28 21:49:481853 bool resource_provider_success = resource_provider_->InitializeGL();
1854
1855 bool success = resource_provider_success;
1856 if (success) {
1857 bool skip_gl_renderer = false;
1858 CreateAndSetRenderer(
1859 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1860 if (!renderer_)
1861 success = false;
1862 }
1863
1864 if (success) {
1865 if (offscreen_context_provider.get() &&
1866 !offscreen_context_provider->BindToCurrentThread())
1867 success = false;
1868 }
1869
[email protected]50644642013-06-20 13:58:551870 if (success) {
1871 EnforceZeroBudget(false);
1872 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481873 } else {
1874 if (offscreen_context_provider.get()) {
1875 if (offscreen_context_provider->BindToCurrentThread())
1876 offscreen_context_provider->VerifyContexts();
1877 offscreen_context_provider = NULL;
1878 }
1879
1880 client_->DidLoseOutputSurfaceOnImplThread();
1881
1882 if (resource_provider_success) {
1883 // If this fails the context provider will be dropped from the output
1884 // surface and destroyed. But the GLRenderer expects the output surface
1885 // to stick around - and hold onto the context3d - as long as it is alive.
1886 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1887 renderer_.reset();
1888
1889 // The resource provider can't stay in GL mode or it tries to clean up GL
1890 // stuff, but the context provider is going away on the output surface
1891 // which contradicts being in GL mode.
1892 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1893 resource_provider_->InitializeSoftware();
1894 }
[email protected]50644642013-06-20 13:58:551895 }
[email protected]b5174d712013-08-28 08:10:431896
1897 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271898 return success;
1899}
1900
[email protected]50af3c22013-07-13 03:50:201901void LayerTreeHostImpl::ReleaseGL() {
1902 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1903 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091904 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201905
1906 ReleaseTreeResources();
1907 renderer_.reset();
1908 tile_manager_.reset();
1909 resource_provider_->InitializeSoftware();
1910
1911 bool skip_gl_renderer = true;
1912 CreateAndSetRenderer(
1913 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1914 DCHECK(renderer_);
1915
1916 EnforceZeroBudget(true);
1917 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011918 NULL,
[email protected]4e472a42014-02-11 01:56:371919 GetRendererCapabilities().using_map_image,
1920 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201921 DCHECK(tile_manager_);
1922
[email protected]b5174d712013-08-28 08:10:431923 SetOffscreenContextProvider(NULL);
1924
[email protected]50af3c22013-07-13 03:50:201925 client_->SetNeedsCommitOnImplThread();
1926}
1927
[email protected]64348ea2014-01-29 22:58:261928void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:401929 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271930 return;
[email protected]94f206c12012-08-25 00:09:141931
[email protected]54af03522013-09-05 00:43:281932 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271933 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271934
[email protected]c1bb5af2013-03-13 19:06:271935 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141936
[email protected]c1bb5af2013-03-13 19:06:271937 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441938 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141939}
1940
[email protected]d9083762013-03-24 01:36:401941void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1942 if (overdraw_bottom_height == overdraw_bottom_height_)
1943 return;
1944 overdraw_bottom_height_ = overdraw_bottom_height;
1945
[email protected]59adb112013-04-09 04:48:441946 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401947}
1948
[email protected]9f4f6a32013-09-04 21:35:121949void LayerTreeHostImpl::SetOverhangUIResource(
1950 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:261951 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:121952 overhang_ui_resource_id_ = overhang_ui_resource_id;
1953 overhang_ui_resource_size_ = overhang_ui_resource_size;
1954}
1955
[email protected]c1bb5af2013-03-13 19:06:271956void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1957 if (device_scale_factor == device_scale_factor_)
1958 return;
1959 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271960
[email protected]59adb112013-04-09 04:48:441961 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141962}
1963
[email protected]54af03522013-09-05 00:43:281964gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1965 return DeviceViewport().size();
1966}
1967
[email protected]f224cc92013-06-06 23:23:321968gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1969 if (external_viewport_.IsEmpty())
1970 return gfx::Rect(device_viewport_size_);
1971
1972 return external_viewport_;
1973}
1974
[email protected]54af03522013-09-05 00:43:281975gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1976 if (external_clip_.IsEmpty())
1977 return DeviceViewport();
1978
1979 return external_clip_;
1980}
1981
1982const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321983 return external_transform_;
1984}
1985
[email protected]59adb112013-04-09 04:48:441986void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]4e4136d2013-11-29 02:22:441987 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:441988 active_tree_->set_needs_update_draw_properties();
1989 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141990}
1991
[email protected]c1bb5af2013-03-13 19:06:271992bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041993 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341994 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141995}
1996
[email protected]200a9c062013-05-20 04:34:371997void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1998 DCHECK(input_handler_client_ == NULL);
1999 input_handler_client_ = client;
2000}
2001
[email protected]420fdf6e2013-08-26 20:36:382002static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2003 if (LayerImpl* scroll_parent = layer->scroll_parent())
2004 return scroll_parent;
2005 return layer->parent();
2006}
2007
[email protected]f620b0e72013-10-01 21:38:242008LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472009 const gfx::PointF& device_viewport_point,
2010 InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:432011 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:022012 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142013
[email protected]c1bb5af2013-03-13 19:06:272014 // Walk up the hierarchy and look for a scrollable layer.
2015 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382016 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272017 // The content layer can also block attempts to scroll outside the main
2018 // thread.
2019 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2020 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022021 *scroll_on_main_thread = true;
2022 return NULL;
[email protected]94f206c12012-08-25 00:09:142023 }
2024
[email protected]c1bb5af2013-03-13 19:06:272025 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2026 if (!scroll_layer_impl)
2027 continue;
[email protected]94f206c12012-08-25 00:09:142028
[email protected]c1bb5af2013-03-13 19:06:272029 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272030 // If any layer wants to divert the scroll event to the main thread, abort.
2031 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022032 *scroll_on_main_thread = true;
2033 return NULL;
[email protected]94f206c12012-08-25 00:09:142034 }
2035
[email protected]c1bb5af2013-03-13 19:06:272036 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2037 potentially_scrolling_layer_impl = scroll_layer_impl;
2038 }
2039
[email protected]bf1cfd9a2013-09-26 05:43:022040 return potentially_scrolling_layer_impl;
2041}
2042
2043InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
2044 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
2045 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2046
2047 if (top_controls_manager_)
2048 top_controls_manager_->ScrollBegin();
2049
2050 DCHECK(!CurrentlyScrollingLayer());
2051 ClearCurrentlyScrollingLayer();
2052
2053 if (!EnsureRenderSurfaceLayerList())
2054 return ScrollIgnored;
2055
[email protected]f620b0e72013-10-01 21:38:242056 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2057 device_scale_factor_);
2058 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2059 device_viewport_point,
2060 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022061 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242062 LayerImpl* potentially_scrolling_layer_impl =
2063 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2064 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022065
2066 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022067 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2068 return ScrollOnMainThread;
2069 }
2070
[email protected]adeda572014-01-31 00:49:472071 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532072 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472073 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2074 ? OuterViewportScrollLayer()
2075 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102076
[email protected]c1bb5af2013-03-13 19:06:272077 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202078 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272079 potentially_scrolling_layer_impl);
2080 should_bubble_scrolls_ = (type != NonBubblingGesture);
2081 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272082 client_->RenewTreePriority();
2083 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2084 return ScrollStarted;
2085 }
2086 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142087}
2088
[email protected]c1bb5af2013-03-13 19:06:272089gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2090 LayerImpl* layer_impl,
2091 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472092 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122093 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272094 // Layers with non-invertible screen space transforms should not have passed
2095 // the scroll hit test in the first place.
2096 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2097 gfx::Transform inverse_screen_space_transform(
2098 gfx::Transform::kSkipInitialization);
2099 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2100 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352101 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2102 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272103 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142104
[email protected]c1bb5af2013-03-13 19:06:272105 gfx::PointF screen_space_point =
2106 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142107
[email protected]c1bb5af2013-03-13 19:06:272108 gfx::Vector2dF screen_space_delta = viewport_delta;
2109 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2110
2111 // First project the scroll start and end points to local layer space to find
2112 // the scroll delta in layer coordinates.
2113 bool start_clipped, end_clipped;
2114 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2115 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212116 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272117 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212118 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272119 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212120 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272121 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212122 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272123
2124 // In general scroll point coordinates should not get clipped.
2125 DCHECK(!start_clipped);
2126 DCHECK(!end_clipped);
2127 if (start_clipped || end_clipped)
2128 return gfx::Vector2dF();
2129
2130 // local_start_point and local_end_point are in content space but we want to
2131 // move them to layer space for scrolling.
2132 float width_scale = 1.f / layer_impl->contents_scale_x();
2133 float height_scale = 1.f / layer_impl->contents_scale_y();
2134 local_start_point.Scale(width_scale, height_scale);
2135 local_end_point.Scale(width_scale, height_scale);
2136
2137 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472138 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272139 layer_impl->ScrollBy(local_end_point - local_start_point);
2140
2141 // Get the end point in the layer's content space so we can apply its
2142 // ScreenSpaceTransform.
2143 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472144 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272145 previous_delta;
2146 gfx::PointF actual_local_content_end_point =
2147 gfx::ScalePoint(actual_local_end_point,
2148 1.f / width_scale,
2149 1.f / height_scale);
2150
2151 // Calculate the applied scroll delta in viewport space coordinates.
2152 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212153 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272154 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212155 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272156 DCHECK(!end_clipped);
2157 if (end_clipped)
2158 return gfx::Vector2dF();
2159 gfx::PointF actual_viewport_end_point =
2160 gfx::ScalePoint(actual_screen_space_end_point,
2161 1.f / scale_from_viewport_to_screen_space);
2162 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142163}
2164
[email protected]c1bb5af2013-03-13 19:06:272165static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122166 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472167 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272168 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472169 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272170}
2171
2172bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
[email protected]3244c9132014-01-23 10:39:122173 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272174 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2175 if (!CurrentlyScrollingLayer())
2176 return false;
2177
2178 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532179 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292180 bool did_scroll_x = false;
2181 bool did_scroll_y = false;
[email protected]adeda572014-01-31 00:49:472182 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2183 // here?
2184 bool consume_by_top_controls =
2185 top_controls_manager_ &&
2186 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2187 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2188 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2189 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062190
[email protected]c1bb5af2013-03-13 19:06:272191 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2192 layer_impl;
2193 layer_impl = layer_impl->parent()) {
2194 if (!layer_impl->scrollable())
2195 continue;
2196
[email protected]adeda572014-01-31 00:49:472197 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532198 // Only allow bubble scrolling when the scroll is in the direction to make
2199 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472200 gfx::Vector2dF applied_delta;
2201 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532202 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472203 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2204 applied_delta = pending_delta - excess_delta;
2205 pending_delta = excess_delta;
2206 // Force updating of vertical adjust values if needed.
2207 if (applied_delta.y() != 0)
2208 layer_impl->ScrollbarParametersDidChange();
[email protected]a2b5ded2013-05-20 21:32:532209 }
2210 // Track root layer deltas for reporting overscroll.
2211 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422212 }
2213
[email protected]c1bb5af2013-03-13 19:06:272214 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272215 // Gesture events need to be transformed from viewport coordinates to local
2216 // layer coordinates so that the scrolling contents exactly follow the
2217 // user's finger. In contrast, wheel events represent a fixed amount of
2218 // scrolling so we can just apply them directly.
2219 if (!wheel_scrolling_) {
2220 float scale_from_viewport_to_screen_space = device_scale_factor_;
2221 applied_delta =
2222 ScrollLayerWithViewportSpaceDelta(layer_impl,
2223 scale_from_viewport_to_screen_space,
2224 viewport_point, pending_delta);
2225 } else {
2226 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142227 }
[email protected]94f206c12012-08-25 00:09:142228
[email protected]c1bb5af2013-03-13 19:06:272229 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292230 float move_threshold = 0.1f;
2231 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2232 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2233 did_scroll_x |= did_move_layer_x;
2234 did_scroll_y |= did_move_layer_y;
2235 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142236 // Scrolls should always bubble between the outer and inner viewports
2237 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2238 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272239 continue;
[email protected]db102612013-12-20 22:04:122240 else
2241 break;
[email protected]94f206c12012-08-25 00:09:142242 }
[email protected]a2b5ded2013-05-20 21:32:532243
[email protected]adeda572014-01-31 00:49:472244 if (layer_impl == InnerViewportScrollLayer())
[email protected]a2b5ded2013-05-20 21:32:532245 unused_root_delta.Subtract(applied_delta);
2246
[email protected]c1bb5af2013-03-13 19:06:272247 did_lock_scrolling_layer_ = true;
2248 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202249 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272250 break;
[email protected]94f206c12012-08-25 00:09:142251 }
[email protected]94f206c12012-08-25 00:09:142252
[email protected]c1bb5af2013-03-13 19:06:272253 // If the applied delta is within 45 degrees of the input delta, bail out to
2254 // make it easier to scroll just one layer in one direction without
2255 // affecting any of its parents.
2256 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212257 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272258 applied_delta, pending_delta) < angle_threshold) {
2259 pending_delta = gfx::Vector2d();
2260 break;
[email protected]4a23c374c2012-12-08 08:38:552261 }
[email protected]c1bb5af2013-03-13 19:06:272262
2263 // Allow further movement only on an axis perpendicular to the direction in
2264 // which the layer moved.
2265 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212266 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272267
[email protected]a2b5ded2013-05-20 21:32:532268 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272269 break;
2270 }
2271
[email protected]2bd503f2013-07-23 05:35:292272 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272273 if (did_scroll) {
2274 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442275 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272276 client_->RenewTreePriority();
2277 }
[email protected]a2b5ded2013-05-20 21:32:532278
[email protected]2bd503f2013-07-23 05:35:292279 // Scrolling along an axis resets accumulated root overscroll for that axis.
2280 if (did_scroll_x)
2281 accumulated_root_overscroll_.set_x(0);
2282 if (did_scroll_y)
2283 accumulated_root_overscroll_.set_y(0);
2284
[email protected]a2b5ded2013-05-20 21:32:532285 accumulated_root_overscroll_ += unused_root_delta;
2286 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2287 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112288 DidOverscrollParams params;
2289 params.accumulated_overscroll = accumulated_root_overscroll_;
2290 params.latest_overscroll_delta = unused_root_delta;
2291 params.current_fling_velocity = current_fling_velocity_;
2292 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532293 }
2294
[email protected]c1bb5af2013-03-13 19:06:272295 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552296}
2297
[email protected]be782f52013-03-23 21:36:142298// This implements scrolling by page as described here:
2299// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2300// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492301bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2302 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142303 DCHECK(wheel_scrolling_);
2304
2305 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2306 layer_impl;
2307 layer_impl = layer_impl->parent()) {
2308 if (!layer_impl->scrollable())
2309 continue;
2310
[email protected]adeda572014-01-31 00:49:472311 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142312 continue;
2313
[email protected]adeda572014-01-31 00:49:472314 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142315
2316 // These magical values match WebKit and are designed to scroll nearly the
2317 // entire visible content height but leave a bit of overlap.
2318 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492319 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142320 page = -page;
2321
2322 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2323
2324 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2325
2326 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142327 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442328 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142329 client_->RenewTreePriority();
2330 return true;
2331 }
2332
2333 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2334 }
2335
2336 return false;
2337}
2338
[email protected]1960a712013-04-30 17:06:472339void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2340 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2341 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2342 active_tree_->SetRootLayerScrollOffsetDelegate(
2343 root_layer_scroll_offset_delegate_);
2344}
2345
2346void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2347 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2348 client_->SetNeedsCommitOnImplThread();
2349}
2350
[email protected]c1bb5af2013-03-13 19:06:272351void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2352 active_tree_->ClearCurrentlyScrollingLayer();
2353 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532354 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202355 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142356}
2357
[email protected]c1bb5af2013-03-13 19:06:272358void LayerTreeHostImpl::ScrollEnd() {
2359 if (top_controls_manager_)
2360 top_controls_manager_->ScrollEnd();
2361 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052362 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142363}
2364
[email protected]5ff3c9782013-04-29 17:35:122365InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462366 if (!active_tree_->CurrentlyScrollingLayer())
2367 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212368
[email protected]75147f52013-08-02 19:11:462369 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472370 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2371 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462372 ClearCurrentlyScrollingLayer();
2373 return ScrollIgnored;
2374 }
2375
[email protected]db102612013-12-20 22:04:122376 if (!wheel_scrolling_) {
2377 // Allow the fling to lock to the first layer that moves after the initial
2378 // fling |ScrollBy()| event.
2379 did_lock_scrolling_layer_ = false;
2380 should_bubble_scrolls_ = false;
2381 }
[email protected]df0c42342013-10-08 20:52:122382
[email protected]75147f52013-08-02 19:11:462383 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212384}
2385
[email protected]3244c9132014-01-23 10:39:122386void LayerTreeHostImpl::NotifyCurrentFlingVelocity(
2387 const gfx::Vector2dF& velocity) {
[email protected]6e921bd2013-04-29 21:10:202388 current_fling_velocity_ = velocity;
2389}
2390
[email protected]bf1cfd9a2013-09-26 05:43:022391float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472392 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022393 LayerImpl* layer_impl) {
2394 if (!layer_impl)
2395 return std::numeric_limits<float>::max();
2396
2397 gfx::Rect layer_impl_bounds(
2398 layer_impl->content_bounds());
2399
2400 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2401 layer_impl->screen_space_transform(),
2402 layer_impl_bounds);
2403
2404 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2405 device_viewport_point);
2406}
2407
2408void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2409 if (!EnsureRenderSurfaceLayerList())
2410 return;
2411
[email protected]f620b0e72013-10-01 21:38:242412 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2413 device_scale_factor_);
2414
2415 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2416 device_viewport_point,
2417 active_tree_->RenderSurfaceLayerList());
2418 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2419 return;
2420
2421 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2422 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2423 scroll_layer_id_when_mouse_over_scrollbar_);
2424
[email protected]5cd9ac12014-02-05 21:48:532425 // The check for a null scroll_layer_impl below was added to see if it will
2426 // eliminate the crashes described in http://crbug.com/326635.
2427 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242428 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532429 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2430 : NULL;
[email protected]f620b0e72013-10-01 21:38:242431 if (animation_controller) {
2432 animation_controller->DidMouseMoveOffScrollbar(
2433 CurrentPhysicalTimeTicks());
2434 StartScrollbarAnimation();
2435 }
2436 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2437 }
2438
[email protected]bf1cfd9a2013-09-26 05:43:022439 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242440 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2441 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022442 &scroll_on_main_thread);
2443 if (scroll_on_main_thread || !scroll_layer_impl)
2444 return;
2445
2446 ScrollbarAnimationController* animation_controller =
2447 scroll_layer_impl->scrollbar_animation_controller();
2448 if (!animation_controller)
2449 return;
2450
[email protected]adeda572014-01-31 00:49:472451 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2452 float distance_to_scrollbar = std::numeric_limits<float>::max();
2453 for (LayerImpl::ScrollbarSet::iterator it =
2454 scroll_layer_impl->scrollbars()->begin();
2455 it != scroll_layer_impl->scrollbars()->end();
2456 ++it)
2457 distance_to_scrollbar =
2458 std::min(distance_to_scrollbar,
2459 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022460
2461 bool should_animate = animation_controller->DidMouseMoveNear(
2462 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202463 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022464 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022465}
2466
[email protected]f620b0e72013-10-01 21:38:242467bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472468 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242469 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2470 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2471 layer_impl = active_tree_->LayerById(scroll_layer_id);
2472 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2473 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202474 bool should_animate =
2475 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2476 CurrentPhysicalTimeTicks(), 0);
2477 if (should_animate)
2478 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242479 } else {
2480 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2481 }
2482
2483 return true;
2484 }
2485
2486 return false;
2487}
2488
[email protected]c1bb5af2013-03-13 19:06:272489void LayerTreeHostImpl::PinchGestureBegin() {
2490 pinch_gesture_active_ = true;
2491 previous_pinch_anchor_ = gfx::Point();
2492 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132493 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472494 if (active_tree_->OuterViewportScrollLayer()) {
2495 active_tree_->SetCurrentlyScrollingLayer(
2496 active_tree_->OuterViewportScrollLayer());
2497 } else {
2498 active_tree_->SetCurrentlyScrollingLayer(
2499 active_tree_->InnerViewportScrollLayer());
2500 }
[email protected]a9bda5e2013-10-25 18:43:372501 if (top_controls_manager_)
2502 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142503}
2504
[email protected]c1bb5af2013-03-13 19:06:272505void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2506 gfx::Point anchor) {
[email protected]b93480862014-02-20 13:07:032507 if (!InnerViewportScrollLayer())
2508 return;
[email protected]d3afa112012-12-08 06:24:282509
[email protected]b93480862014-02-20 13:07:032510 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282511
[email protected]c1bb5af2013-03-13 19:06:272512 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2513 // position over the course of the magnify.
2514 float page_scale_delta = active_tree_->page_scale_delta();
2515 gfx::PointF previous_scale_anchor =
2516 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2517 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2518 page_scale_delta = active_tree_->page_scale_delta();
2519 gfx::PointF new_scale_anchor =
2520 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2521 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2522
2523 previous_pinch_anchor_ = anchor;
2524
2525 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472526 // If clamping the inner viewport scroll offset causes a change, it should
2527 // be accounted for from the intended move.
2528 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272529
[email protected]adeda572014-01-31 00:49:472530 // We manually manage the bubbling behaviour here as it is different to that
2531 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2532 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2533 // 2) we don't want the directional limitations on the unused parts that
2534 // ScrollBy() implements, and
2535 // 3) pinching should not engage the top controls manager.
2536 gfx::Vector2dF unused = OuterViewportScrollLayer()
2537 ? OuterViewportScrollLayer()->ScrollBy(move)
2538 : move;
2539
2540 if (!unused.IsZero()) {
2541 InnerViewportScrollLayer()->ScrollBy(unused);
2542 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2543 }
[email protected]c1bb5af2013-03-13 19:06:272544
[email protected]c1bb5af2013-03-13 19:06:272545 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442546 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272547 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282548}
2549
[email protected]c1bb5af2013-03-13 19:06:272550void LayerTreeHostImpl::PinchGestureEnd() {
2551 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132552 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2553 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2554 ClearCurrentlyScrollingLayer();
2555 }
[email protected]a9bda5e2013-10-25 18:43:372556 if (top_controls_manager_)
2557 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272558 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142559}
2560
[email protected]c1bb5af2013-03-13 19:06:272561static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2562 LayerImpl* layer_impl) {
2563 if (!layer_impl)
2564 return;
[email protected]94f206c12012-08-25 00:09:142565
[email protected]c1bb5af2013-03-13 19:06:272566 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472567 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272568 if (!scroll_delta.IsZero()) {
2569 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392570 scroll.layer_id = layer_impl->id();
2571 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272572 scroll_info->scrolls.push_back(scroll);
2573 layer_impl->SetSentScrollDelta(scroll_delta);
2574 }
[email protected]94f206c12012-08-25 00:09:142575
[email protected]c1bb5af2013-03-13 19:06:272576 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2577 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142578}
2579
[email protected]c1bb5af2013-03-13 19:06:272580scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2581 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302582
[email protected]c1bb5af2013-03-13 19:06:272583 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392584 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2585 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302586
[email protected]c1bb5af2013-03-13 19:06:272587 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302588}
2589
[email protected]c1bb5af2013-03-13 19:06:272590void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282591 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102592}
2593
[email protected]adeda572014-01-31 00:49:472594void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2595 DCHECK(InnerViewportScrollLayer());
2596 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2597 ? OuterViewportScrollLayer()
2598 : InnerViewportScrollLayer();
2599
2600 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2601
2602 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2603 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2604}
2605
[email protected]c1bb5af2013-03-13 19:06:272606void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
[email protected]adeda572014-01-31 00:49:472607 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272608 return;
2609
2610 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472611 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272612
[email protected]34e1b512013-09-26 17:32:332613 if (!page_scale_animation_->IsAnimationStarted())
2614 page_scale_animation_->StartAnimation(monotonic_time);
2615
[email protected]c1bb5af2013-03-13 19:06:272616 active_tree_->SetPageScaleDelta(
2617 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2618 active_tree_->page_scale_factor());
2619 gfx::Vector2dF next_scroll =
2620 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2621
[email protected]adeda572014-01-31 00:49:472622 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442623 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272624
2625 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2626 page_scale_animation_.reset();
2627 client_->SetNeedsCommitOnImplThread();
2628 client_->RenewTreePriority();
2629 }
[email protected]829ad972013-01-28 23:36:102630}
2631
[email protected]ffb2720f2013-03-15 19:18:372632void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082633 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372634 return;
2635 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472636 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172637 return;
[email protected]5f7d9112014-01-17 21:31:422638 if (scroll.IsZero()) {
2639 // This may happen on the first animation step. Force redraw otherwise
2640 // the animation would stop because of no new frames.
2641 SetNeedsRedraw();
2642 } else {
[email protected]adeda572014-01-31 00:49:472643 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422644 scroll, 1.f / active_tree_->total_page_scale_factor()));
2645 }
[email protected]ffb2720f2013-03-15 19:18:372646}
2647
[email protected]c1bb5af2013-03-13 19:06:272648void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2649 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522650 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272651 animation_registrar_->active_animation_controllers().empty() ||
2652 !active_tree_->root_layer())
2653 return;
2654
2655 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2656
2657 last_animation_time_ = wall_clock_time;
2658 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2659
2660 AnimationRegistrar::AnimationControllerMap copy =
2661 animation_registrar_->active_animation_controllers();
2662 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2663 iter != copy.end();
2664 ++iter)
2665 (*iter).second->Animate(monotonic_seconds);
2666
[email protected]4e4136d2013-11-29 02:22:442667 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082668}
2669
[email protected]3d9f7432013-04-06 00:35:182670void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522671 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272672 animation_registrar_->active_animation_controllers().empty() ||
2673 !active_tree_->root_layer())
2674 return;
2675
2676 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2677 scoped_ptr<AnimationEventsVector> events =
2678 make_scoped_ptr(new AnimationEventsVector);
2679 AnimationRegistrar::AnimationControllerMap copy =
2680 animation_registrar_->active_animation_controllers();
2681 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2682 iter != copy.end();
2683 ++iter)
[email protected]3d9f7432013-04-06 00:35:182684 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272685
2686 if (!events->empty()) {
2687 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2688 last_animation_time_);
2689 }
[email protected]131a0c22013-02-12 18:31:082690}
2691
[email protected]c1bb5af2013-03-13 19:06:272692base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2693 return base::TimeDelta::FromSeconds(1);
2694}
2695
[email protected]50644642013-06-20 13:58:552696void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352697 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502698 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352699 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552700 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352701 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552702 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352703 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552704 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352705}
2706
[email protected]b5174d712013-08-28 08:10:432707void LayerTreeHostImpl::SetOffscreenContextProvider(
2708 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2709 if (!offscreen_context_provider.get()) {
2710 offscreen_context_provider_ = NULL;
2711 return;
2712 }
2713
2714 if (!offscreen_context_provider->BindToCurrentThread()) {
2715 offscreen_context_provider_ = NULL;
2716 return;
2717 }
2718
2719 offscreen_context_provider_ = offscreen_context_provider;
2720}
2721
[email protected]c1bb5af2013-03-13 19:06:272722std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2723 std::string str;
2724 if (active_tree_->root_layer()) {
2725 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2726 base::JSONWriter::WriteWithOptions(
2727 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2728 }
2729 return str;
2730}
2731
[email protected]c1bb5af2013-03-13 19:06:272732int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362733 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272734}
2735
[email protected]c1bb5af2013-03-13 19:06:272736void LayerTreeHostImpl::SendManagedMemoryStats(
2737 size_t memory_visible_bytes,
2738 size_t memory_visible_and_nearby_bytes,
2739 size_t memory_use_bytes) {
2740 if (!renderer_)
2741 return;
2742
2743 // Round the numbers being sent up to the next 8MB, to throttle the rate
2744 // at which we spam the GPU process.
2745 static const size_t rounding_step = 8 * 1024 * 1024;
2746 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2747 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2748 rounding_step);
2749 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2750 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2751 last_sent_memory_visible_and_nearby_bytes_ ==
2752 memory_visible_and_nearby_bytes &&
2753 last_sent_memory_use_bytes_ == memory_use_bytes) {
2754 return;
2755 }
2756 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2757 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2758 last_sent_memory_use_bytes_ = memory_use_bytes;
2759
2760 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2761 last_sent_memory_visible_and_nearby_bytes_,
2762 last_sent_memory_use_bytes_);
2763}
2764
2765void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2766 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2767}
2768
2769void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2770 base::TimeTicks time) {
2771 if (!layer)
2772 return;
2773
2774 ScrollbarAnimationController* scrollbar_controller =
2775 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352776 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202777 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422778 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2779 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442780 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202781 }
[email protected]c1bb5af2013-03-13 19:06:272782
2783 for (size_t i = 0; i < layer->children().size(); ++i)
2784 AnimateScrollbarsRecursive(layer->children()[i], time);
2785}
2786
[email protected]21c9dee72013-06-15 01:20:052787void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202788 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052789 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202790}
2791
2792void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2793 base::TimeTicks time) {
2794 if (!layer)
2795 return;
2796
2797 ScrollbarAnimationController* scrollbar_controller =
2798 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352799 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2800 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202801 if (delay > base::TimeDelta())
2802 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352803 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442804 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202805 }
2806
2807 for (size_t i = 0; i < layer->children().size(); ++i)
2808 StartScrollbarAnimationRecursive(layer->children()[i], time);
2809}
2810
[email protected]c1bb5af2013-03-13 19:06:272811void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2812 if (!tile_manager_)
2813 return;
2814
[email protected]1bcced22013-09-24 13:51:192815 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272816 return;
[email protected]1bcced22013-09-24 13:51:192817 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082818 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272819}
2820
[email protected]8347d692013-05-17 23:22:382821void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552822 current_frame_timeticks_ = base::TimeTicks();
2823 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272824}
2825
[email protected]21c9dee72013-06-15 01:20:052826void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2827 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552828 if (ticks->is_null()) {
2829 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052830 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552831 *now = base::Time::Now();
2832 }
2833}
2834
2835base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2836 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2837 return current_frame_timeticks_;
2838}
2839
2840base::Time LayerTreeHostImpl::CurrentFrameTime() {
2841 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272842 return current_frame_time_;
2843}
2844
[email protected]21c9dee72013-06-15 01:20:052845base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462846 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052847}
2848
[email protected]34806722013-08-09 23:51:212849scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2850 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272851 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222852 if (this->pending_tree_)
2853 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272854 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212855 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272856 if (tile_manager_)
2857 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2858 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222859 if (pending_tree_)
2860 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212861 if (frame)
2862 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222863 return state.PassAs<base::Value>();
2864}
2865
2866scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222867 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2868 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472869 if (tile_manager_)
2870 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272871 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082872}
2873
[email protected]6e7fdeb2013-07-09 14:28:382874void LayerTreeHostImpl::SetDebugState(
2875 const LayerTreeDebugState& new_debug_state) {
2876 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2877 return;
2878 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272879 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242880
[email protected]6e7fdeb2013-07-09 14:28:382881 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122882 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382883 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022884}
2885
[email protected]741fba422013-09-20 03:34:142886void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2887 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572888 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572889
[email protected]efa48412013-09-05 15:30:162890 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142891 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162892 case UIResourceBitmap::CLAMP_TO_EDGE:
2893 wrap_mode = GL_CLAMP_TO_EDGE;
2894 break;
2895 case UIResourceBitmap::REPEAT:
2896 wrap_mode = GL_REPEAT;
2897 break;
2898 }
2899
[email protected]c9280762013-08-01 06:28:572900 // Allow for multiple creation requests with the same UIResourceId. The
2901 // previous resource is simply deleted.
2902 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2903 if (id)
2904 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122905
2906 ResourceFormat format = resource_provider_->best_texture_format();
2907 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2908 format = ETC1;
[email protected]c9280762013-08-01 06:28:572909 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142910 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162911 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142912 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122913 format);
[email protected]c9280762013-08-01 06:28:572914
[email protected]efbdb3a2013-10-04 00:35:132915 UIResourceData data;
2916 data.resource_id = id;
2917 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512918 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132919
2920 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032921
2922 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572923 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032924 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142925 gfx::Rect(bitmap.GetSize()),
2926 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572927 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482928 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572929}
2930
2931void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2932 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2933 if (id) {
2934 resource_provider_->DeleteResource(id);
2935 ui_resource_map_.erase(uid);
2936 }
[email protected]127bdc1a2013-09-11 01:44:482937 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572938}
2939
[email protected]127bdc1a2013-09-11 01:44:482940void LayerTreeHostImpl::EvictAllUIResources() {
2941 if (ui_resource_map_.empty())
2942 return;
2943
[email protected]5f4dc4f2013-09-05 14:58:212944 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2945 iter != ui_resource_map_.end();
2946 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482947 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132948 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212949 }
2950 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482951
2952 client_->SetNeedsCommitOnImplThread();
2953 client_->OnCanDrawStateChanged(CanDraw());
2954 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212955}
2956
[email protected]c9280762013-08-01 06:28:572957ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2958 UIResourceId uid) const {
2959 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2960 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132961 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572962 return 0;
2963}
2964
[email protected]709c9542013-10-26 01:43:512965bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2966 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2967 DCHECK(iter != ui_resource_map_.end());
2968 return iter->second.opaque;
2969}
2970
[email protected]127bdc1a2013-09-11 01:44:482971bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2972 return !evicted_ui_resources_.empty();
2973}
2974
2975void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2976 std::set<UIResourceId>::iterator found_in_evicted =
2977 evicted_ui_resources_.find(uid);
2978 if (found_in_evicted == evicted_ui_resources_.end())
2979 return;
2980 evicted_ui_resources_.erase(found_in_evicted);
2981 if (evicted_ui_resources_.empty())
2982 client_->OnCanDrawStateChanged(CanDraw());
2983}
2984
[email protected]5e5648a2013-11-18 00:39:332985void LayerTreeHostImpl::ScheduleMicroBenchmark(
2986 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2987 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2988}
2989
[email protected]6be422b2013-12-08 06:47:312990void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2991 swap_promise_monitor_.insert(monitor);
2992}
2993
2994void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2995 swap_promise_monitor_.erase(monitor);
2996}
2997
2998void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
2999 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3000 for (; it != swap_promise_monitor_.end(); it++)
3001 (*it)->OnSetNeedsRedrawOnImpl();
3002}
3003
[email protected]d3143c732012-10-05 19:17:593004} // namespace cc