blob: 45e1ec1dc8c4e1ae326ffdfd79f5013acbe68bf5 [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"
[email protected]6e84de22013-03-18 06:54:2725#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4326#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2227#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2128#include "cc/input/page_scale_animation.h"
29#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5230#include "cc/layers/append_quads_data.h"
31#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2832#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5233#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1734#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2835#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0236#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3037#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4538#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3039#include "cc/output/delegating_renderer.h"
40#include "cc/output/gl_renderer.h"
41#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5642#include "cc/quads/render_pass_draw_quad.h"
43#include "cc/quads/shared_quad_state.h"
44#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1245#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4046#include "cc/resources/memory_history.h"
47#include "cc/resources/picture_layer_tiling.h"
48#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1149#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5750#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3151#include "cc/scheduler/delay_based_time_source.h"
[email protected]556fd292013-03-18 08:03:0452#include "cc/trees/damage_tracker.h"
53#include "cc/trees/layer_tree_host.h"
54#include "cc/trees/layer_tree_host_common.h"
55#include "cc/trees/layer_tree_impl.h"
[email protected]34ba1ffb2014-03-05 06:55:0356#include "cc/trees/occlusion_tracker.h"
[email protected]556fd292013-03-18 08:03:0457#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
[email protected]900db4412014-01-12 02:19:01112 if (context_provider->ContextCapabilities().gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12113 return GL_TEXTURE_EXTERNAL_OES;
[email protected]900db4412014-01-12 02:19:01114 if (context_provider->ContextCapabilities().gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12115 return GL_TEXTURE_RECTANGLE_ARB;
[email protected]8d7aa012013-11-23 21:19:12116
117 return GL_TEXTURE_2D;
118}
119
[email protected]c1bb5af2013-03-13 19:06:27120} // namespace
[email protected]94f206c12012-08-25 00:09:14121
[email protected]9c88e562012-09-14 22:21:30122namespace cc {
[email protected]94f206c12012-08-25 00:09:14123
[email protected]96baf3e2012-10-22 23:09:55124class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27125 public:
126 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
127 LayerTreeHostImpl* layer_tree_host_impl,
128 scoped_refptr<DelayBasedTimeSource> time_source) {
129 return make_scoped_ptr(
130 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
131 time_source));
132 }
133 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26134 time_source_->SetClient(NULL);
135 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27136 }
137
[email protected]6d0e69d2013-03-20 14:53:26138 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27139 // In single threaded mode we attempt to simulate changing the current
140 // thread by maintaining a fake thread id. When we switch from one
141 // thread to another, we construct DebugScopedSetXXXThread objects that
142 // update the thread id. This lets DCHECKS that ensure we're on the
143 // right thread to work correctly in single threaded mode. The problem
144 // here is that the timer tasks are run via the message loop, and when
145 // they run, we've had no chance to construct a DebugScopedSetXXXThread
146 // object. The result is that we report that we're running on the main
147 // thread. In multi-threaded mode, this timer is run on the compositor
148 // thread, so to keep this consistent in single-threaded mode, we'll
149 // construct a DebugScopedSetImplThread object. There is no need to do
150 // this in multi-threaded mode since the real thread id's will be
151 // correct. In fact, setting fake thread id's interferes with the real
152 // thread id's and causes breakage.
153 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
154 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
155 set_impl_thread.reset(
156 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14157 }
158
[email protected]94bf75c2013-06-12 13:20:04159 // TODO(enne): This should probably happen post-animate.
160 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38161 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
162 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04163 }
164
[email protected]fb7425a2013-04-22 16:28:55165 layer_tree_host_impl_->Animate(
[email protected]27152642014-03-11 20:42:00166 layer_tree_host_impl_->CurrentFrameTimeTicks());
[email protected]3d9f7432013-04-06 00:35:18167 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
168 bool start_ready_animations = true;
169 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38170 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27171 }
[email protected]373974232013-01-10 22:20:50172
[email protected]c1bb5af2013-03-13 19:06:27173 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26174 if (active != time_source_->Active())
175 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27176 }
[email protected]94f206c12012-08-25 00:09:14177
[email protected]d9fce6722013-08-30 01:10:01178 bool Active() const { return time_source_->Active(); }
179
[email protected]c1bb5af2013-03-13 19:06:27180 private:
181 LayerTreeHostImplTimeSourceAdapter(
182 LayerTreeHostImpl* layer_tree_host_impl,
183 scoped_refptr<DelayBasedTimeSource> time_source)
184 : layer_tree_host_impl_(layer_tree_host_impl),
185 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26186 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27187 }
[email protected]94f206c12012-08-25 00:09:14188
[email protected]c1bb5af2013-03-13 19:06:27189 LayerTreeHostImpl* layer_tree_host_impl_;
190 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14191
[email protected]c1bb5af2013-03-13 19:06:27192 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14193};
194
[email protected]96baf3e2012-10-22 23:09:55195LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20196 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27197
198LayerTreeHostImpl::FrameData::~FrameData() {}
199
200scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
201 const LayerTreeSettings& settings,
202 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43203 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57204 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02205 SharedBitmapManager* manager,
206 int id) {
[email protected]a7f35682013-10-22 23:05:57207 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02208 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10209}
210
[email protected]372bad5f2013-03-21 16:38:43211LayerTreeHostImpl::LayerTreeHostImpl(
212 const LayerTreeSettings& settings,
213 LayerTreeHostImplClient* client,
214 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57215 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02216 SharedBitmapManager* manager,
217 int id)
[email protected]c1bb5af2013-03-13 19:06:27218 : client_(client),
219 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37220 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27221 did_lock_scrolling_layer_(false),
222 should_bubble_scrolls_(false),
223 wheel_scrolling_(false),
[email protected]f161ca9e2014-04-01 13:57:01224 scroll_affects_scroll_handler_(false),
[email protected]f620b0e72013-10-01 21:38:24225 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08226 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47227 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27228 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27229 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07230 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34231 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29232 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07233 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27234 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13235 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36236 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29237 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27238 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02239 debug_rect_history_(DebugRectHistory::Create()),
[email protected]dd909b1f2014-03-18 00:19:52240 texture_mailbox_deleter_(new TextureMailboxDeleter(
241 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
242 : proxy_->MainThreadTaskRunner())),
[email protected]d7626ffd2013-03-29 00:17:42243 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27244 last_sent_memory_visible_bytes_(0),
245 last_sent_memory_visible_and_nearby_bytes_(0),
246 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55247 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32248 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12249 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32250 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28251 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17252 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43253 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19254 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33255 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57256 need_to_update_visible_tiles_before_draw_(false),
[email protected]767f38d72014-03-18 21:26:41257#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:36258 did_lose_called_(false),
259#endif
[email protected]dbe759a2013-12-02 19:23:02260 shared_bitmap_manager_(manager),
261 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27262 DCHECK(proxy_->IsImplThread());
263 DidVisibilityChange(this, visible_);
264
[email protected]8e0176d2013-03-21 03:14:52265 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27266
[email protected]8e0176d2013-03-21 03:14:52267 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27268 top_controls_manager_ =
269 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52270 settings.top_controls_height,
271 settings.top_controls_show_threshold,
272 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27273 }
274
[email protected]8e0176d2013-03-21 03:14:52275 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27276
277 // LTHI always has an active tree.
278 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51279 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
280 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10281}
282
[email protected]c1bb5af2013-03-13 19:06:27283LayerTreeHostImpl::~LayerTreeHostImpl() {
284 DCHECK(proxy_->IsImplThread());
285 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51286 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
287 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27288
[email protected]200a9c062013-05-20 04:34:37289 if (input_handler_client_) {
290 input_handler_client_->WillShutdown();
291 input_handler_client_ = NULL;
292 }
293
[email protected]75deb742013-06-24 20:19:18294 // The layer trees must be destroyed before the layer tree host. We've
295 // made a contract with our animation controllers that the registrar
296 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40297 if (recycle_tree_)
298 recycle_tree_->Shutdown();
299 if (pending_tree_)
300 pending_tree_->Shutdown();
301 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18302 recycle_tree_.reset();
303 pending_tree_.reset();
304 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14305}
306
[email protected]097a0cbf2013-12-12 17:04:58307void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
308 // If the begin frame data was handled, then scroll and scale set was applied
309 // by the main thread, so the active tree needs to be updated as if these sent
310 // values were applied and committed.
311 if (did_handle) {
312 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
313 active_tree_->ResetContentsTexturesPurged();
314 }
315}
316
[email protected]c1bb5af2013-03-13 19:06:27317void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29318
[email protected]c1bb5af2013-03-13 19:06:27319void LayerTreeHostImpl::CommitComplete() {
320 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08321
[email protected]8e0176d2013-03-21 03:14:52322 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25323 // Impl-side painting needs an update immediately post-commit to have the
324 // opportunity to create tilings. Other paths can call UpdateDrawProperties
325 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50326 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27327 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04328 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26329 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08330 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38331 NotifyReadyToActivate();
332 else
333 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27334 } else {
335 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01336 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
337 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27338 }
[email protected]3ba4cae2013-01-16 03:58:38339
[email protected]c1bb5af2013-03-13 19:06:27340 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33341
342 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14343}
344
[email protected]6133cc232013-07-30 18:47:07345bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27346 // Note: If you are changing this function or any other function that might
347 // affect the result of CanDraw, make sure to call
348 // client_->OnCanDrawStateChanged in the proper places and update the
349 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14350
[email protected]6133cc232013-07-30 18:47:07351 if (!renderer_) {
352 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
353 TRACE_EVENT_SCOPE_THREAD);
354 return false;
355 }
356
357 // Must have an OutputSurface if |renderer_| is not NULL.
358 DCHECK(output_surface_);
359
360 // TODO(boliu): Make draws without root_layer work and move this below
361 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27362 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42363 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
364 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22365 return false;
[email protected]c1bb5af2013-03-13 19:06:27366 }
[email protected]6133cc232013-07-30 18:47:07367
368 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
369 return true;
370
[email protected]54af03522013-09-05 00:43:28371 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42372 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
373 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27374 return false;
375 }
376 if (active_tree_->ViewportSizeInvalid()) {
377 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42378 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
379 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27380 return false;
381 }
[email protected]c1bb5af2013-03-13 19:06:27382 if (active_tree_->ContentsTexturesPurged()) {
383 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42384 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
385 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27386 return false;
387 }
[email protected]127bdc1a2013-09-11 01:44:48388 if (EvictedUIResourcesExist()) {
389 TRACE_EVENT_INSTANT0(
390 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
391 TRACE_EVENT_SCOPE_THREAD);
392 return false;
393 }
[email protected]c1bb5af2013-03-13 19:06:27394 return true;
[email protected]2f1acc262012-11-16 21:42:22395}
396
[email protected]27152642014-03-11 20:42:00397void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time) {
[email protected]200a9c062013-05-20 04:34:37398 if (input_handler_client_)
399 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27400 AnimatePageScale(monotonic_time);
[email protected]27152642014-03-11 20:42:00401 AnimateLayers(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27402 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37403 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14404}
405
[email protected]c1bb5af2013-03-13 19:06:27406void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26407 if (!tile_manager_)
408 return;
[email protected]c48536a52013-09-14 00:02:08409 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26410 return;
[email protected]54af03522013-09-05 00:43:28411
[email protected]c48536a52013-09-14 00:02:08412 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19413 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27414
415 size_t memory_required_bytes;
416 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03417 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27418 size_t memory_used_bytes;
419 tile_manager_->GetMemoryStats(&memory_required_bytes,
420 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03421 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27422 &memory_used_bytes);
423 SendManagedMemoryStats(memory_required_bytes,
424 memory_nice_to_have_bytes,
425 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57426
427 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15428}
429
[email protected]243e4f12014-02-05 09:18:42430void LayerTreeHostImpl::StartPageScaleAnimation(
431 const gfx::Vector2d& target_offset,
432 bool anchor_point,
433 float page_scale,
434 base::TimeDelta duration) {
[email protected]adeda572014-01-31 00:49:47435 if (!InnerViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:27436 return;
437
[email protected]adeda572014-01-31 00:49:47438 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27439 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28440 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27441
[email protected]85d136f782013-04-26 22:04:40442 // Easing constants experimentally determined.
443 scoped_ptr<TimingFunction> timing_function =
444 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
445
[email protected]c1bb5af2013-03-13 19:06:27446 page_scale_animation_ =
447 PageScaleAnimation::Create(scroll_total,
448 active_tree_->total_page_scale_factor(),
449 viewport_size,
450 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40451 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27452
453 if (anchor_point) {
454 gfx::Vector2dF anchor(target_offset);
455 page_scale_animation_->ZoomWithAnchor(anchor,
456 page_scale,
457 duration.InSecondsF());
458 } else {
459 gfx::Vector2dF scaled_target_offset = target_offset;
460 page_scale_animation_->ZoomTo(scaled_target_offset,
461 page_scale,
462 duration.InSecondsF());
463 }
464
[email protected]4e4136d2013-11-29 02:22:44465 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27466 client_->SetNeedsCommitOnImplThread();
467 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15468}
469
[email protected]c1bb5af2013-03-13 19:06:27470void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44471 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15472}
473
[email protected]47a723f2014-03-05 12:42:49474bool LayerTreeHostImpl::HaveTouchEventHandlersAt(
475 const gfx::Point& viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07476 if (!settings_.touch_hit_testing)
477 return true;
[email protected]c1bb5af2013-03-13 19:06:27478 if (!EnsureRenderSurfaceLayerList())
479 return false;
[email protected]f57bbc02012-11-21 07:02:15480
[email protected]c1bb5af2013-03-13 19:06:27481 gfx::PointF device_viewport_point =
482 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15483
[email protected]c974d5d2013-10-24 01:02:48484 LayerImpl* layer_impl =
485 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
486 device_viewport_point,
487 active_tree_->RenderSurfaceLayerList());
488 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27489}
490
[email protected]6be422b2013-12-08 06:47:31491scoped_ptr<SwapPromiseMonitor>
492LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
493 ui::LatencyInfo* latency) {
494 return scoped_ptr<SwapPromiseMonitor>(
495 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08496}
497
[email protected]c1bb5af2013-03-13 19:06:27498void LayerTreeHostImpl::TrackDamageForAllSurfaces(
499 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28500 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27501 // For now, we use damage tracking to compute a global scissor. To do this, we
502 // must compute all damage tracking before drawing anything, so that we know
503 // the root damage rect. The root damage rect is then used to scissor each
504 // surface.
505
506 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06507 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27508 --surface_index) {
509 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
510 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
511 DCHECK(render_surface);
512 render_surface->damage_tracker()->UpdateDamageTrackingState(
513 render_surface->layer_list(),
514 render_surface_layer->id(),
515 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
516 render_surface->content_rect(),
517 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48518 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27519 }
520}
521
[email protected]34806722013-08-09 23:51:21522scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
523 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
524 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
525 value->SetBoolean("has_no_damage", has_no_damage);
526
527 // Quad data can be quite large, so only dump render passes if we select
528 // cc.debug.quads.
529 bool quads_enabled;
530 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
531 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
532 if (quads_enabled) {
533 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
534 for (size_t i = 0; i < render_passes.size(); ++i)
535 render_pass_list->Append(render_passes[i]->AsValue().release());
536 value->Set("render_passes", render_pass_list.release());
537 }
538 return value.PassAs<base::Value>();
539}
540
[email protected]c1bb5af2013-03-13 19:06:27541void LayerTreeHostImpl::FrameData::AppendRenderPass(
542 scoped_ptr<RenderPass> render_pass) {
543 render_passes_by_id[render_pass->id] = render_pass.get();
544 render_passes.push_back(render_pass.Pass());
545}
546
[email protected]ffbb2212013-06-02 23:47:59547static DrawMode GetDrawMode(OutputSurface* output_surface) {
548 if (output_surface->ForcedDrawToSoftwareDevice()) {
549 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09550 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59551 return DRAW_MODE_HARDWARE;
552 } else {
[email protected]a7f35682013-10-22 23:05:57553 DCHECK_EQ(!output_surface->software_device(),
554 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59555 return DRAW_MODE_SOFTWARE;
556 }
557}
558
[email protected]34ba1ffb2014-03-05 06:55:03559static void AppendQuadsForLayer(
560 RenderPass* target_render_pass,
561 LayerImpl* layer,
562 const OcclusionTracker<LayerImpl>& occlusion_tracker,
563 AppendQuadsData* append_quads_data) {
[email protected]c1bb5af2013-03-13 19:06:27564 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49565 QuadCuller quad_culler(&target_render_pass->quad_list,
566 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27567 layer,
568 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27569 for_surface);
570 layer->AppendQuads(&quad_culler, append_quads_data);
571}
572
573static void AppendQuadsForRenderSurfaceLayer(
574 RenderPass* target_render_pass,
575 LayerImpl* layer,
576 const RenderPass* contributing_render_pass,
[email protected]34ba1ffb2014-03-05 06:55:03577 const OcclusionTracker<LayerImpl>& occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27578 AppendQuadsData* append_quads_data) {
579 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49580 QuadCuller quad_culler(&target_render_pass->quad_list,
581 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27582 layer,
583 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27584 for_surface);
585
586 bool is_replica = false;
587 layer->render_surface()->AppendQuads(&quad_culler,
588 append_quads_data,
589 is_replica,
590 contributing_render_pass->id);
591
592 // Add replica after the surface so that it appears below the surface.
593 if (layer->has_replica()) {
594 is_replica = true;
595 layer->render_surface()->AppendQuads(&quad_culler,
596 append_quads_data,
597 is_replica,
598 contributing_render_pass->id);
599 }
600}
601
602static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08603 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20604 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06605 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27606 RenderPass* target_render_pass,
607 LayerImpl* root_layer,
608 SkColor screen_background_color,
[email protected]34ba1ffb2014-03-05 06:55:03609 const OcclusionTracker<LayerImpl>& occlusion_tracker) {
[email protected]c1bb5af2013-03-13 19:06:27610 if (!root_layer || !SkColorGetA(screen_background_color))
611 return;
612
613 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
614 if (fill_region.IsEmpty())
615 return;
616
[email protected]3744e27b2013-11-06 21:44:08617 // Divide the fill region into the part to be filled with the overhang
618 // resource and the part to be filled with the background color.
619 Region screen_background_color_region = fill_region;
620 Region overhang_region;
621 if (overhang_resource_id) {
622 overhang_region = fill_region;
623 overhang_region.Subtract(root_scroll_layer_rect);
624 screen_background_color_region.Intersect(root_scroll_layer_rect);
625 }
626
[email protected]c1bb5af2013-03-13 19:06:27627 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49628 QuadCuller quad_culler(&target_render_pass->quad_list,
629 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27630 root_layer,
631 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27632 for_surface);
633
634 // Manually create the quad state for the gutter quads, as the root layer
635 // doesn't have any bounds and so can't generate this itself.
636 // TODO(danakj): Make the gutter quads generated by the solid color layer
637 // (make it smarter about generating quads to fill unoccluded areas).
638
639 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
640 float opacity = 1.f;
641 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49642 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]7785e1002014-02-20 21:04:32643 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27644 root_target_rect.size(),
645 root_target_rect,
646 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01647 false,
[email protected]7bbeaf4e2013-11-26 10:27:22648 opacity,
649 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15650
[email protected]3744e27b2013-11-06 21:44:08651 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27652 fill_rects.has_rect();
653 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32654 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24655 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08656 // Skip the quad culler and just append the quads directly to avoid
657 // occlusion checks.
658 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]9bf06c72014-03-07 18:16:24659 quad->SetNew(shared_quad_state,
660 screen_space_rect,
661 visible_screen_space_rect,
662 screen_background_color,
663 false);
[email protected]0d8c7eaa2014-03-13 05:26:30664 quad_culler.MaybeAppend(quad.PassAs<DrawQuad>());
[email protected]3744e27b2013-11-06 21:44:08665 }
666 for (Region::Iterator fill_rects(overhang_region);
667 fill_rects.has_rect();
668 fill_rects.next()) {
669 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32670 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24671 gfx::Rect opaque_screen_space_rect = screen_space_rect;
672 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08673 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
674 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
675 tex_quad->SetNew(
676 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32677 screen_space_rect,
[email protected]9bf06c72014-03-07 18:16:24678 opaque_screen_space_rect,
679 visible_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);
[email protected]0d8c7eaa2014-03-13 05:26:30692 quad_culler.MaybeAppend(tex_quad.PassAs<DrawQuad>());
[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();
[email protected]5fda7f32014-03-12 20:44:29714 bool hud_wants_to_draw_ = active_tree_->hud_layer() &&
715 active_tree_->hud_layer()->IsAnimatingHUDContents();
[email protected]e0341352013-04-06 05:01:20716 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23717 root_surface_has_no_visible_damage &&
[email protected]5fda7f32014-03-12 20:44:29718 active_tree_->LayersWithCopyOutputRequest().empty() &&
719 !hud_wants_to_draw_) {
[email protected]e0341352013-04-06 05:01:20720 TRACE_EVENT0("cc",
721 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
722 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07723 DCHECK(!output_surface_->capabilities()
724 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11725 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20726 }
727
[email protected]c1bb5af2013-03-13 19:06:27728 TRACE_EVENT1("cc",
729 "LayerTreeHostImpl::CalculateRenderPasses",
730 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06731 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14732
[email protected]c1bb5af2013-03-13 19:06:27733 // Create the render passes in dependency order.
734 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06735 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27736 --surface_index) {
737 LayerImpl* render_surface_layer =
738 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45739 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
740
741 bool should_draw_into_render_pass =
742 render_surface_layer->parent() == NULL ||
743 render_surface->contributes_to_drawn_surface() ||
744 render_surface_layer->HasCopyRequest();
745 if (should_draw_into_render_pass)
746 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27747 }
[email protected]94f206c12012-08-25 00:09:14748
[email protected]f636ce82014-03-10 22:33:08749 // When we are displaying the HUD, change the root damage rect to cover the
750 // entire root surface. This will disable partial-swap/scissor optimizations
751 // that would prevent the HUD from updating, since the HUD does not cause
752 // damage itself, to prevent it from messing with damage visualizations. Since
753 // damage visualizations are done off the LayerImpls and RenderSurfaceImpls,
754 // changing the RenderPass does not affect them.
755 if (active_tree_->hud_layer()) {
756 RenderPass* root_pass = frame->render_passes.back();
757 root_pass->damage_rect = root_pass->output_rect;
758 }
759
[email protected]34ba1ffb2014-03-05 06:55:03760 OcclusionTracker<LayerImpl> occlusion_tracker(
[email protected]97c6a7342014-03-12 20:36:43761 active_tree_->root_layer()->render_surface()->content_rect());
[email protected]c1bb5af2013-03-13 19:06:27762 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52763 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14764
[email protected]846f455b2013-03-18 19:07:41765 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27766 occlusion_tracker.set_occluding_screen_space_rects_container(
767 &frame->occluding_screen_space_rects);
768 }
[email protected]846f455b2013-03-18 19:07:41769 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27770 occlusion_tracker.set_non_occluding_screen_space_rects_container(
771 &frame->non_occluding_screen_space_rects);
772 }
[email protected]94f206c12012-08-25 00:09:14773
[email protected]ba1b33e2014-02-28 16:44:51774 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27775 // occlusion and performing culling during the tree walk.
[email protected]ba1b33e2014-02-28 16:44:51776 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14777
[email protected]c1bb5af2013-03-13 19:06:27778 // Typically when we are missing a texture and use a checkerboard quad, we
779 // still draw the frame. However when the layer being checkerboarded is moving
780 // due to an impl-animation, we drop the frame to avoid flashing due to the
781 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11782 DrawSwapReadbackResult::DrawResult draw_result =
783 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25784 // When we have a copy request for a layer, we need to draw no matter
785 // what, as the layer may disappear after this frame.
786 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14787
[email protected]372bad5f2013-03-21 16:38:43788 int layers_drawn = 0;
789
[email protected]ffbb2212013-06-02 23:47:59790 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
791
[email protected]c1bb5af2013-03-13 19:06:27792 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09793 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27794 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09795 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27796 it != end;
797 ++it) {
798 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09799 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27800 RenderPass* target_render_pass =
801 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14802
[email protected]e47b0a0a2013-11-18 23:26:22803 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14804
[email protected]30fe19ff2013-07-04 00:54:45805 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30806
[email protected]18a70192013-04-26 16:18:25807 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36808 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25809 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11810 it->TakeCopyRequestsAndTransformToTarget(
811 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25812 }
[email protected]30fe19ff2013-07-04 00:54:45813 } else if (it.represents_contributing_render_surface() &&
814 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27815 RenderPass::Id contributing_render_pass_id =
816 it->render_surface()->RenderPassId();
817 RenderPass* contributing_render_pass =
818 frame->render_passes_by_id[contributing_render_pass_id];
819 AppendQuadsForRenderSurfaceLayer(target_render_pass,
820 *it,
821 contributing_render_pass,
822 occlusion_tracker,
823 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45824 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09825 !it->visible_content_rect().IsEmpty()) {
[email protected]7d6d6c92014-03-06 01:18:50826 bool occluded = occlusion_tracker.Occluded(it->render_target(),
827 it->visible_content_rect(),
828 it->draw_transform());
[email protected]fbc293322013-10-01 05:07:15829 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27830 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59831
[email protected]c1bb5af2013-03-13 19:06:27832 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57833
[email protected]c1bb5af2013-03-13 19:06:27834 if (it->HasContributingDelegatedRenderPasses()) {
835 RenderPass::Id contributing_render_pass_id =
836 it->FirstContributingRenderPassId();
837 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
838 frame->render_passes_by_id.end()) {
839 RenderPass* render_pass =
840 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14841
[email protected]c1bb5af2013-03-13 19:06:27842 AppendQuadsData append_quads_data(render_pass->id);
843 AppendQuadsForLayer(render_pass,
844 *it,
845 occlusion_tracker,
846 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57847
[email protected]c1bb5af2013-03-13 19:06:27848 contributing_render_pass_id =
849 it->NextContributingRenderPassId(contributing_render_pass_id);
850 }
[email protected]94f206c12012-08-25 00:09:14851 }
852
[email protected]c1bb5af2013-03-13 19:06:27853 AppendQuadsForLayer(target_render_pass,
854 *it,
855 occlusion_tracker,
856 &append_quads_data);
857 }
[email protected]89228202012-08-29 03:20:30858
[email protected]372bad5f2013-03-21 16:38:43859 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14860 }
861
[email protected]e1e768f2013-03-26 08:48:09862 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27863 bool layer_has_animating_transform =
864 it->screen_space_transform_is_animating() ||
865 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11866 if (layer_has_animating_transform) {
867 draw_result =
868 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
869 }
[email protected]c1bb5af2013-03-13 19:06:27870 }
871
[email protected]3d609bb2014-02-01 01:10:23872 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27873 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23874 if (active_tree()->RequiresHighResToDraw())
875 draw_result =
876 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
877 }
[email protected]c1bb5af2013-03-13 19:06:27878
879 occlusion_tracker.LeaveLayer(it);
880 }
881
[email protected]6133cc232013-07-30 18:47:07882 if (have_copy_request ||
883 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11884 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25885
[email protected]767f38d72014-03-18 21:26:41886#if DCHECK_IS_ON
[email protected]c1bb5af2013-03-13 19:06:27887 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
888 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
889 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
890 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
891 != frame->render_passes_by_id.end());
892 }
[email protected]94f206c12012-08-25 00:09:14893#endif
[email protected]c1bb5af2013-03-13 19:06:27894 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14895
[email protected]c1bb5af2013-03-13 19:06:27896 if (!active_tree_->has_transparent_background()) {
897 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08898 AppendQuadsToFillScreen(
899 ResourceIdForUIResource(overhang_ui_resource_id_),
900 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
901 active_tree_->RootScrollLayerDeviceViewportBounds(),
902 frame->render_passes.back(),
903 active_tree_->root_layer(),
904 active_tree_->background_color(),
905 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27906 }
[email protected]94f206c12012-08-25 00:09:14907
[email protected]c1bb5af2013-03-13 19:06:27908 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48909 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14910
[email protected]30fe19ff2013-07-04 00:54:45911 // Any copy requests left in the tree are not going to get serviced, and
912 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11913 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
914 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
915 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
916 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45917 }
[email protected]19334ac2013-12-12 23:59:11918 for (size_t i = 0; i < requests_to_abort.size(); ++i)
919 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45920
[email protected]e0341352013-04-06 05:01:20921 // If we're making a frame to draw, it better have at least one render pass.
922 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48923
924 // Should only have one render pass in resourceless software mode.
925 if (output_surface_->ForcedDrawToSoftwareDevice())
926 DCHECK_EQ(1u, frame->render_passes.size());
927
[email protected]7ddfe7e82014-01-30 07:22:11928 return draw_result;
[email protected]94f206c12012-08-25 00:09:14929}
930
[email protected]200a9c062013-05-20 04:34:37931void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
932 if (input_handler_client_)
933 input_handler_client_->MainThreadHasStoppedFlinging();
934}
935
[email protected]3d9f7432013-04-06 00:35:18936void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
937 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15938 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01939 if (should_background_tick)
940 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15941
[email protected]cd77bb62014-03-27 05:16:36942 bool enabled = should_background_tick && needs_animate_layers();
[email protected]3d9f7432013-04-06 00:35:18943
[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(),
[email protected]f636ce82014-03-10 22:33:081357 active_tree_->hud_layer(),
[email protected]c1bb5af2013-03-13 19:06:271358 *frame->render_surface_layer_list,
1359 frame->occluding_screen_space_rects,
1360 frame->non_occluding_screen_space_rects,
1361 debug_state_);
1362 }
[email protected]94f206c12012-08-25 00:09:141363
[email protected]a848c102013-03-26 08:59:091364 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1365 const RenderingStats& stats =
1366 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181367 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091368 }
1369
[email protected]37349bc2013-06-04 01:31:521370 bool is_new_trace;
1371 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1372 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491373 if (pending_tree_) {
1374 LayerTreeHostCommon::CallFunctionForSubtree(
1375 pending_tree_->root_layer(),
1376 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1377 }
1378 LayerTreeHostCommon::CallFunctionForSubtree(
1379 active_tree_->root_layer(),
1380 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521381 }
1382
[email protected]9197dbcb2013-05-15 20:28:511383 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211384 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1385 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1386 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081387
[email protected]c1bb5af2013-03-13 19:06:271388 // Because the contents of the HUD depend on everything else in the frame, the
1389 // contents of its texture are updated as the last thing before the frame is
1390 // drawn.
[email protected]671f6672013-10-01 07:31:071391 if (active_tree_->hud_layer()) {
1392 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531393 active_tree_->hud_layer()->UpdateHudTexture(
1394 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071395 }
[email protected]94f206c12012-08-25 00:09:141396
[email protected]b09c1942013-05-10 00:06:371397 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[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(),
[email protected]59fee302013-12-10 17:53:301408 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371409 } else {
[email protected]b5174d712013-08-28 08:10:431410 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321411 offscreen_context_provider_.get(),
1412 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301413 DeviceViewport(),
1414 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261415 false);
[email protected]b09c1942013-05-10 00:06:371416 }
[email protected]c1bb5af2013-03-13 19:06:271417 // The render passes should be consumed by the renderer.
1418 DCHECK(frame->render_passes.empty());
1419 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141420
[email protected]c1bb5af2013-03-13 19:06:271421 // The next frame should start by assuming nothing has changed, and changes
1422 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321423 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1424 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541425 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271426 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1427 DidDrawDamagedArea();
1428 }
1429 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351430
[email protected]d73094482014-02-13 21:28:281431 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331432 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1433 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181434 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141435}
1436
[email protected]c1bb5af2013-03-13 19:06:271437void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1438 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1439 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521440
[email protected]c1bb5af2013-03-13 19:06:271441 // Once all layers have been drawn, pending texture uploads should no
1442 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151443 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141444}
1445
[email protected]c1bb5af2013-03-13 19:06:271446void LayerTreeHostImpl::FinishAllRendering() {
1447 if (renderer_)
1448 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141449}
1450
[email protected]c1bb5af2013-03-13 19:06:271451bool LayerTreeHostImpl::IsContextLost() {
1452 DCHECK(proxy_->IsImplThread());
1453 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141454}
1455
[email protected]7a8bcd262014-01-15 12:54:581456const RendererCapabilitiesImpl&
1457LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271458 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141459}
1460
[email protected]e0341352013-04-06 05:01:201461bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231462 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191463 if (frame.has_no_damage) {
1464 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201465 return false;
[email protected]b69c1db2013-11-27 00:05:191466 }
[email protected]4a28a612013-11-27 02:06:331467 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551468 active_tree()->FinishSwapPromises(&metadata);
[email protected]15ba6dea2014-04-02 01:44:131469 for (size_t i = 0; i < metadata.latency_info.size(); i++) {
1470 TRACE_EVENT_FLOW_STEP0(
1471 "input",
1472 "LatencyInfo.Flow",
1473 TRACE_ID_DONT_MANGLE(metadata.latency_info[i].trace_id),
1474 "SwapBuffers");
1475 }
[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]fef74fd2014-02-27 06:28:171485gfx::SizeF LayerTreeHostImpl::ComputeInnerViewportContainerSize() const {
1486 gfx::SizeF dip_size =
1487 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
1488
1489 float top_offset =
1490 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
1491
1492 return gfx::SizeF(dip_size.width(),
1493 dip_size.height() - top_offset - overdraw_bottom_height_);
1494}
1495
1496void LayerTreeHostImpl::UpdateInnerViewportContainerSize() {
1497 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
1498 if (!container_layer)
1499 return;
1500
[email protected]05dfe4f2014-02-27 19:52:501501 // We pass the value returned from UnscaledScrollableViewportSize() here as
1502 // it accounts for scrollbar dimensions when
1503 // container_layer->masks_to_bounds() is set.
1504 container_layer->SetTemporaryImplBounds(UnscaledScrollableViewportSize());
[email protected]fef74fd2014-02-27 06:28:171505}
1506
[email protected]54af03522013-09-05 00:43:281507gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341508 // Use the root container layer bounds if it clips to them, otherwise, the
1509 // true viewport size should be used.
[email protected]fef74fd2014-02-27 06:28:171510 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341511 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441512 DCHECK(!top_controls_manager_);
1513 DCHECK_EQ(0, overdraw_bottom_height_);
1514 return container_layer->bounds();
1515 }
1516
[email protected]fef74fd2014-02-27 06:28:171517 return ComputeInnerViewportContainerSize();
1518}
[email protected]ffb2720f2013-03-15 19:18:371519
[email protected]fef74fd2014-02-27 06:28:171520float LayerTreeHostImpl::VerticalAdjust() const {
1521 if (!active_tree_->InnerViewportContainerLayer())
1522 return 0;
1523
1524 return active_tree_->InnerViewportContainerLayer()->BoundsDelta().y();
[email protected]ffb2720f2013-03-15 19:18:371525}
1526
[email protected]c1bb5af2013-03-13 19:06:271527void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191528 if (resource_provider_)
1529 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011530 // TODO(jamesr): The renderer_ check is needed to make some of the
1531 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1532 // important) in production. We should adjust the test to not need this.
1533 if (renderer_)
1534 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]767f38d72014-03-18 21:26:411535#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361536 did_lose_called_ = true;
1537#endif
[email protected]94f206c12012-08-25 00:09:141538}
1539
[email protected]c1bb5af2013-03-13 19:06:271540void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061541 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271542 DCHECK(renderer_);
1543 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141544}
1545
[email protected]59adb112013-04-09 04:48:441546bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471547 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011548}
1549
[email protected]c1bb5af2013-03-13 19:06:271550LayerImpl* LayerTreeHostImpl::RootLayer() const {
1551 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081552}
1553
[email protected]adeda572014-01-31 00:49:471554LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1555 return active_tree_->InnerViewportScrollLayer();
1556}
1557
1558LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1559 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081560}
1561
[email protected]c1bb5af2013-03-13 19:06:271562LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1563 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081564}
1565
[email protected]251699b2013-10-09 00:21:261566bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1567 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471568 (InnerViewportScrollLayer() &&
1569 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1570 (OuterViewportScrollLayer() &&
1571 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261572}
1573
[email protected]94f206c12012-08-25 00:09:141574// Content layers can be either directly scrollable or contained in an outer
1575// scrolling layer which applies the scroll transform. Given a content layer,
1576// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271577static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1578 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021579 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271580
1581 if (layer_impl->scrollable())
1582 return layer_impl;
1583
1584 if (layer_impl->DrawsContent() &&
1585 layer_impl->parent() &&
1586 layer_impl->parent()->scrollable())
1587 return layer_impl->parent();
1588
[email protected]bf1cfd9a2013-09-26 05:43:021589 return NULL;
[email protected]94f206c12012-08-25 00:09:141590}
1591
[email protected]c1bb5af2013-03-13 19:06:271592void LayerTreeHostImpl::CreatePendingTree() {
1593 CHECK(!pending_tree_);
1594 if (recycle_tree_)
1595 recycle_tree_.swap(pending_tree_);
1596 else
1597 pending_tree_ = LayerTreeImpl::create(this);
1598 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111599 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271600}
1601
[email protected]8bb6cfa2013-07-23 00:11:191602void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191603 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1604 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191605 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481606}
1607
[email protected]c1bb5af2013-03-13 19:06:271608void LayerTreeHostImpl::ActivatePendingTree() {
1609 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111610 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351611
[email protected]4f48f6e2013-08-27 06:33:381612 need_to_update_visible_tiles_before_draw_ = true;
1613
[email protected]1960a712013-04-30 17:06:471614 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271615 active_tree_->PushPersistedState(pending_tree_.get());
1616 if (pending_tree_->needs_full_tree_sync()) {
1617 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331618 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271619 active_tree_->DetachLayerTree(),
1620 active_tree_.get()));
1621 }
[email protected]b5651c22013-03-14 15:06:331622 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271623 active_tree_->root_layer());
1624 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511625
[email protected]c9280762013-08-01 06:28:571626 // Process any requests in the UI resource queue. The request queue is given
1627 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1628 // the swap.
1629 pending_tree_->ProcessUIResourceRequestQueue();
1630
[email protected]c1bb5af2013-03-13 19:06:271631 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511632
[email protected]c1bb5af2013-03-13 19:06:271633 // Now that we've synced everything from the pending tree to the active
1634 // tree, rename the pending tree the recycle tree so we can reuse it on the
1635 // next sync.
1636 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511637
[email protected]7d1b07e2013-10-01 17:31:301638 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471639 active_tree_->SetRootLayerScrollOffsetDelegate(
1640 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221641
[email protected]c1bb5af2013-03-13 19:06:271642 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441643 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271644 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241645
[email protected]a848c102013-03-26 08:59:091646 if (debug_state_.continuous_painting) {
1647 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431648 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091649 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1650 stats.main_stats.record_time +
1651 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271652 }
[email protected]2a61ad52013-05-13 14:01:291653
[email protected]fef74fd2014-02-27 06:28:171654 UpdateInnerViewportContainerSize();
[email protected]2a61ad52013-05-13 14:01:291655 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261656 if (!tree_activation_callback_.is_null())
1657 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011658
1659 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1660 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281661 devtools_instrumentation::DidActivateLayerTree(
1662 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271663}
1664
[email protected]c1bb5af2013-03-13 19:06:271665void LayerTreeHostImpl::SetVisible(bool visible) {
1666 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141667
[email protected]c1bb5af2013-03-13 19:06:271668 if (visible_ == visible)
1669 return;
1670 visible_ = visible;
1671 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121672 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141673
[email protected]3d609bb2014-02-01 01:10:231674 if (!visible_) {
1675 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481676 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231677 }
[email protected]127bdc1a2013-09-11 01:44:481678
[email protected]0fa42e02013-08-20 01:16:051679 // Evict tiles immediately if invisible since this tab may never get another
1680 // draw or timer tick.
1681 if (!visible_)
1682 ManageTiles();
1683
[email protected]c1bb5af2013-03-13 19:06:271684 if (!renderer_)
1685 return;
[email protected]94f206c12012-08-25 00:09:141686
[email protected]c1bb5af2013-03-13 19:06:271687 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141688}
1689
[email protected]6be422b2013-12-08 06:47:311690void LayerTreeHostImpl::SetNeedsRedraw() {
1691 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1692 client_->SetNeedsRedrawOnImplThread();
1693}
1694
[email protected]206a3922013-05-17 06:34:121695ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071696 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011697 // TODO(ernstm): The second condition disables pre-painting for all layers
1698 // when GPU rasterization is enabled. Once we selectively enable GPU
1699 // rasterization per layer, we also need to disable pre-painting selectively:
1700 // crbug.com/335387
1701 if (debug_state_.rasterize_only_visible_content ||
[email protected]a23530d2014-03-11 06:04:141702 settings_.rasterization_site == LayerTreeSettings::GpuRasterization) {
[email protected]3f2ff112013-08-03 02:41:071703 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291704 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071705 }
[email protected]206a3922013-05-17 06:34:121706
[email protected]3f2ff112013-08-03 02:41:071707 if (zero_budget_) {
1708 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071709 }
1710
[email protected]206a3922013-05-17 06:34:121711 return actual;
1712}
1713
[email protected]3f2ff112013-08-03 02:41:071714size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1715 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1716}
1717
[email protected]990e050a2013-09-23 18:50:211718int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1719 return ManagedMemoryPolicy::PriorityCutoffToValue(
1720 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1721}
1722
[email protected]50644642013-06-20 13:58:551723void LayerTreeHostImpl::ReleaseTreeResources() {
1724 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551725 SendReleaseResourcesRecursive(active_tree_->root_layer());
1726 if (pending_tree_ && pending_tree_->root_layer())
1727 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1728 if (recycle_tree_ && recycle_tree_->root_layer())
1729 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571730
[email protected]127bdc1a2013-09-11 01:44:481731 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551732}
1733
1734void LayerTreeHostImpl::CreateAndSetRenderer(
1735 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201736 ResourceProvider* resource_provider,
1737 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551738 DCHECK(!renderer_);
1739 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321740 renderer_ = DelegatingRenderer::Create(
1741 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091742 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551743 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321744 &settings_,
[email protected]50644642013-06-20 13:58:551745 output_surface,
1746 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111747 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241748 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551749 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321750 renderer_ = SoftwareRenderer::Create(
1751 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551752 }
1753
[email protected]84975832013-06-29 00:24:111754 if (renderer_) {
[email protected]50644642013-06-20 13:58:551755 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111756 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561757
1758 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1759 // initialized to get max texture size. Also, after releasing resources,
1760 // trees need another update to generate new ones.
1761 active_tree_->set_needs_update_draw_properties();
1762 if (pending_tree_)
1763 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591764 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111765 }
[email protected]50644642013-06-20 13:58:551766}
1767
[email protected]50af3c22013-07-13 03:50:201768void LayerTreeHostImpl::CreateAndSetTileManager(
1769 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011770 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371771 bool using_map_image,
1772 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201773 DCHECK(settings_.impl_side_painting);
1774 DCHECK(resource_provider);
[email protected]98805e62014-03-05 01:33:591775 DCHECK(proxy_->ImplThreadTaskRunner());
[email protected]b6eb8e332013-09-10 00:51:011776 tile_manager_ =
1777 TileManager::Create(this,
[email protected]98805e62014-03-05 01:33:591778 proxy_->ImplThreadTaskRunner(),
[email protected]b6eb8e332013-09-10 00:51:011779 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281780 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011781 rendering_stats_instrumentation_,
1782 using_map_image,
[email protected]4e472a42014-02-11 01:56:371783 allow_rasterize_on_demand,
[email protected]b4664142013-11-08 00:50:311784 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121785 GetMaxRasterTasksUsageBytes(context_provider),
1786 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011787
[email protected]50af3c22013-07-13 03:50:201788 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191789 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201790}
1791
[email protected]50644642013-06-20 13:58:551792void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071793 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551794}
1795
[email protected]c1bb5af2013-03-13 19:06:271796bool LayerTreeHostImpl::InitializeRenderer(
1797 scoped_ptr<OutputSurface> output_surface) {
[email protected]767f38d72014-03-18 21:26:411798#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361799 DCHECK(!renderer_ || did_lose_called_);
1800#endif
1801
[email protected]c1bb5af2013-03-13 19:06:271802 // Since we will create a new resource provider, we cannot continue to use
1803 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1804 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551805 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401806
[email protected]c1bb5af2013-03-13 19:06:271807 // Note: order is important here.
1808 renderer_.reset();
1809 tile_manager_.reset();
1810 resource_provider_.reset();
1811 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141812
[email protected]c1bb5af2013-03-13 19:06:271813 if (!output_surface->BindToClient(this))
1814 return false;
[email protected]be3181652012-09-25 13:02:131815
[email protected]a7f35682013-10-22 23:05:571816 scoped_ptr<ResourceProvider> resource_provider =
1817 ResourceProvider::Create(output_surface.get(),
1818 shared_bitmap_manager_,
1819 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341820 settings_.use_rgba_4444_textures,
1821 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551822 if (!resource_provider)
1823 return false;
[email protected]2b154b22013-06-07 09:03:271824
[email protected]50644642013-06-20 13:58:551825 if (output_surface->capabilities().deferred_gl_initialization)
1826 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131827
[email protected]50af3c22013-07-13 03:50:201828 bool skip_gl_renderer = false;
1829 CreateAndSetRenderer(
1830 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131831
[email protected]50644642013-06-20 13:58:551832 if (!renderer_)
1833 return false;
[email protected]6123930c2013-05-23 06:48:371834
[email protected]50644642013-06-20 13:58:551835 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371836 CreateAndSetTileManager(
1837 resource_provider.get(),
1838 output_surface->context_provider().get(),
1839 GetRendererCapabilities().using_map_image,
1840 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241841 }
1842
[email protected]daea3d42013-10-23 17:04:501843 // Setup BeginImplFrameEmulation if it's not supported natively
1844 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351845 const base::TimeDelta display_refresh_interval =
1846 base::TimeDelta::FromMicroseconds(
1847 base::Time::kMicrosecondsPerSecond /
1848 settings_.refresh_rate);
1849
[email protected]daea3d42013-10-23 17:04:501850 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151851 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351852 settings_.throttle_frame_production,
1853 display_refresh_interval);
1854 }
1855
1856 int max_frames_pending =
1857 output_surface->capabilities().max_frames_pending;
1858 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351859 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351860 output_surface->SetMaxFramesPending(max_frames_pending);
1861
[email protected]50644642013-06-20 13:58:551862 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271863 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141864
[email protected]c1bb5af2013-03-13 19:06:271865 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211866
[email protected]c1bb5af2013-03-13 19:06:271867 return true;
[email protected]94f206c12012-08-25 00:09:141868}
1869
[email protected]2b154b22013-06-07 09:03:271870bool LayerTreeHostImpl::DeferredInitialize(
1871 scoped_refptr<ContextProvider> offscreen_context_provider) {
1872 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551873 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091874 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271875
[email protected]50644642013-06-20 13:58:551876 ReleaseTreeResources();
1877 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271878
[email protected]d9ca99e2013-08-28 21:49:481879 bool resource_provider_success = resource_provider_->InitializeGL();
1880
1881 bool success = resource_provider_success;
1882 if (success) {
1883 bool skip_gl_renderer = false;
1884 CreateAndSetRenderer(
1885 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1886 if (!renderer_)
1887 success = false;
1888 }
1889
1890 if (success) {
1891 if (offscreen_context_provider.get() &&
1892 !offscreen_context_provider->BindToCurrentThread())
1893 success = false;
1894 }
1895
[email protected]50644642013-06-20 13:58:551896 if (success) {
1897 EnforceZeroBudget(false);
1898 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481899 } else {
1900 if (offscreen_context_provider.get()) {
1901 if (offscreen_context_provider->BindToCurrentThread())
1902 offscreen_context_provider->VerifyContexts();
1903 offscreen_context_provider = NULL;
1904 }
1905
1906 client_->DidLoseOutputSurfaceOnImplThread();
1907
1908 if (resource_provider_success) {
1909 // If this fails the context provider will be dropped from the output
1910 // surface and destroyed. But the GLRenderer expects the output surface
1911 // to stick around - and hold onto the context3d - as long as it is alive.
1912 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1913 renderer_.reset();
1914
1915 // The resource provider can't stay in GL mode or it tries to clean up GL
1916 // stuff, but the context provider is going away on the output surface
1917 // which contradicts being in GL mode.
1918 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1919 resource_provider_->InitializeSoftware();
1920 }
[email protected]50644642013-06-20 13:58:551921 }
[email protected]b5174d712013-08-28 08:10:431922
1923 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271924 return success;
1925}
1926
[email protected]50af3c22013-07-13 03:50:201927void LayerTreeHostImpl::ReleaseGL() {
1928 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1929 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091930 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201931
1932 ReleaseTreeResources();
1933 renderer_.reset();
1934 tile_manager_.reset();
1935 resource_provider_->InitializeSoftware();
1936
1937 bool skip_gl_renderer = true;
1938 CreateAndSetRenderer(
1939 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1940 DCHECK(renderer_);
1941
1942 EnforceZeroBudget(true);
1943 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011944 NULL,
[email protected]4e472a42014-02-11 01:56:371945 GetRendererCapabilities().using_map_image,
1946 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201947 DCHECK(tile_manager_);
1948
[email protected]b5174d712013-08-28 08:10:431949 SetOffscreenContextProvider(NULL);
1950
[email protected]50af3c22013-07-13 03:50:201951 client_->SetNeedsCommitOnImplThread();
1952}
1953
[email protected]64348ea2014-01-29 22:58:261954void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:401955 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271956 return;
[email protected]94f206c12012-08-25 00:09:141957
[email protected]54af03522013-09-05 00:43:281958 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271959 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271960
[email protected]c1bb5af2013-03-13 19:06:271961 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141962
[email protected]fef74fd2014-02-27 06:28:171963 UpdateInnerViewportContainerSize();
[email protected]c1bb5af2013-03-13 19:06:271964 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441965 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141966}
1967
[email protected]d9083762013-03-24 01:36:401968void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1969 if (overdraw_bottom_height == overdraw_bottom_height_)
1970 return;
1971 overdraw_bottom_height_ = overdraw_bottom_height;
1972
[email protected]fef74fd2014-02-27 06:28:171973 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441974 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401975}
1976
[email protected]9f4f6a32013-09-04 21:35:121977void LayerTreeHostImpl::SetOverhangUIResource(
1978 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:261979 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:121980 overhang_ui_resource_id_ = overhang_ui_resource_id;
1981 overhang_ui_resource_size_ = overhang_ui_resource_size;
1982}
1983
[email protected]c1bb5af2013-03-13 19:06:271984void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1985 if (device_scale_factor == device_scale_factor_)
1986 return;
1987 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271988
[email protected]fef74fd2014-02-27 06:28:171989 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441990 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141991}
1992
[email protected]54af03522013-09-05 00:43:281993gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1994 return DeviceViewport().size();
1995}
1996
[email protected]f224cc92013-06-06 23:23:321997gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1998 if (external_viewport_.IsEmpty())
1999 return gfx::Rect(device_viewport_size_);
2000
2001 return external_viewport_;
2002}
2003
[email protected]54af03522013-09-05 00:43:282004gfx::Rect LayerTreeHostImpl::DeviceClip() const {
2005 if (external_clip_.IsEmpty())
2006 return DeviceViewport();
2007
2008 return external_clip_;
2009}
2010
2011const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:322012 return external_transform_;
2013}
2014
[email protected]59adb112013-04-09 04:48:442015void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]fef74fd2014-02-27 06:28:172016 UpdateInnerViewportContainerSize();
[email protected]4e4136d2013-11-29 02:22:442017 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:442018 active_tree_->set_needs_update_draw_properties();
2019 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142020}
2021
[email protected]c1bb5af2013-03-13 19:06:272022bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:042023 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:342024 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:142025}
2026
[email protected]200a9c062013-05-20 04:34:372027void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2028 DCHECK(input_handler_client_ == NULL);
2029 input_handler_client_ = client;
2030}
2031
[email protected]420fdf6e2013-08-26 20:36:382032static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2033 if (LayerImpl* scroll_parent = layer->scroll_parent())
2034 return scroll_parent;
2035 return layer->parent();
2036}
2037
[email protected]f620b0e72013-10-01 21:38:242038LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472039 const gfx::PointF& device_viewport_point,
2040 InputHandler::ScrollInputType type,
[email protected]f161ca9e2014-04-01 13:57:012041 LayerImpl* layer_impl,
2042 bool* scroll_on_main_thread,
2043 bool* has_ancestor_scroll_handler) const {
[email protected]bf1cfd9a2013-09-26 05:43:022044 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142045
[email protected]c1bb5af2013-03-13 19:06:272046 // Walk up the hierarchy and look for a scrollable layer.
2047 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382048 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272049 // The content layer can also block attempts to scroll outside the main
2050 // thread.
2051 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2052 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022053 *scroll_on_main_thread = true;
2054 return NULL;
[email protected]94f206c12012-08-25 00:09:142055 }
2056
[email protected]c1bb5af2013-03-13 19:06:272057 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2058 if (!scroll_layer_impl)
2059 continue;
[email protected]94f206c12012-08-25 00:09:142060
[email protected]c1bb5af2013-03-13 19:06:272061 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272062 // If any layer wants to divert the scroll event to the main thread, abort.
2063 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022064 *scroll_on_main_thread = true;
2065 return NULL;
[email protected]94f206c12012-08-25 00:09:142066 }
2067
[email protected]f161ca9e2014-04-01 13:57:012068 if (has_ancestor_scroll_handler &&
2069 scroll_layer_impl->have_scroll_event_handlers())
2070 *has_ancestor_scroll_handler = true;
2071
[email protected]c1bb5af2013-03-13 19:06:272072 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2073 potentially_scrolling_layer_impl = scroll_layer_impl;
2074 }
2075
[email protected]bf1cfd9a2013-09-26 05:43:022076 return potentially_scrolling_layer_impl;
2077}
2078
2079InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
[email protected]47a723f2014-03-05 12:42:492080 const gfx::Point& viewport_point,
2081 InputHandler::ScrollInputType type) {
[email protected]bf1cfd9a2013-09-26 05:43:022082 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2083
2084 if (top_controls_manager_)
2085 top_controls_manager_->ScrollBegin();
2086
2087 DCHECK(!CurrentlyScrollingLayer());
2088 ClearCurrentlyScrollingLayer();
2089
2090 if (!EnsureRenderSurfaceLayerList())
2091 return ScrollIgnored;
2092
[email protected]f620b0e72013-10-01 21:38:242093 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2094 device_scale_factor_);
2095 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2096 device_viewport_point,
2097 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022098 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242099 LayerImpl* potentially_scrolling_layer_impl =
[email protected]f161ca9e2014-04-01 13:57:012100 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2101 type,
2102 layer_impl,
2103 &scroll_on_main_thread,
2104 &scroll_affects_scroll_handler_);
[email protected]bf1cfd9a2013-09-26 05:43:022105
2106 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022107 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2108 return ScrollOnMainThread;
2109 }
2110
[email protected]adeda572014-01-31 00:49:472111 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532112 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472113 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2114 ? OuterViewportScrollLayer()
2115 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102116
[email protected]c1bb5af2013-03-13 19:06:272117 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202118 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272119 potentially_scrolling_layer_impl);
2120 should_bubble_scrolls_ = (type != NonBubblingGesture);
2121 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272122 client_->RenewTreePriority();
2123 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2124 return ScrollStarted;
2125 }
2126 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142127}
2128
[email protected]c1bb5af2013-03-13 19:06:272129gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2130 LayerImpl* layer_impl,
2131 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472132 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122133 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272134 // Layers with non-invertible screen space transforms should not have passed
2135 // the scroll hit test in the first place.
2136 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2137 gfx::Transform inverse_screen_space_transform(
2138 gfx::Transform::kSkipInitialization);
2139 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2140 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352141 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2142 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272143 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142144
[email protected]c1bb5af2013-03-13 19:06:272145 gfx::PointF screen_space_point =
2146 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142147
[email protected]c1bb5af2013-03-13 19:06:272148 gfx::Vector2dF screen_space_delta = viewport_delta;
2149 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2150
2151 // First project the scroll start and end points to local layer space to find
2152 // the scroll delta in layer coordinates.
2153 bool start_clipped, end_clipped;
2154 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2155 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212156 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272157 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212158 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272159 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212160 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272161 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212162 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272163
2164 // In general scroll point coordinates should not get clipped.
2165 DCHECK(!start_clipped);
2166 DCHECK(!end_clipped);
2167 if (start_clipped || end_clipped)
2168 return gfx::Vector2dF();
2169
2170 // local_start_point and local_end_point are in content space but we want to
2171 // move them to layer space for scrolling.
2172 float width_scale = 1.f / layer_impl->contents_scale_x();
2173 float height_scale = 1.f / layer_impl->contents_scale_y();
2174 local_start_point.Scale(width_scale, height_scale);
2175 local_end_point.Scale(width_scale, height_scale);
2176
2177 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472178 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272179 layer_impl->ScrollBy(local_end_point - local_start_point);
2180
2181 // Get the end point in the layer's content space so we can apply its
2182 // ScreenSpaceTransform.
2183 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472184 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272185 previous_delta;
2186 gfx::PointF actual_local_content_end_point =
2187 gfx::ScalePoint(actual_local_end_point,
2188 1.f / width_scale,
2189 1.f / height_scale);
2190
2191 // Calculate the applied scroll delta in viewport space coordinates.
2192 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212193 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272194 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212195 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272196 DCHECK(!end_clipped);
2197 if (end_clipped)
2198 return gfx::Vector2dF();
2199 gfx::PointF actual_viewport_end_point =
2200 gfx::ScalePoint(actual_screen_space_end_point,
2201 1.f / scale_from_viewport_to_screen_space);
2202 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142203}
2204
[email protected]c1bb5af2013-03-13 19:06:272205static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122206 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472207 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272208 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472209 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272210}
2211
[email protected]47a723f2014-03-05 12:42:492212bool LayerTreeHostImpl::ScrollBy(const gfx::Point& viewport_point,
[email protected]3244c9132014-01-23 10:39:122213 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272214 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2215 if (!CurrentlyScrollingLayer())
2216 return false;
2217
2218 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532219 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292220 bool did_scroll_x = false;
2221 bool did_scroll_y = false;
[email protected]f42cffb2014-03-08 18:03:252222 bool did_scroll_top_controls = false;
[email protected]adeda572014-01-31 00:49:472223 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2224 // here?
2225 bool consume_by_top_controls =
2226 top_controls_manager_ &&
2227 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2228 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2229 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2230 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062231
[email protected]c1bb5af2013-03-13 19:06:272232 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2233 layer_impl;
2234 layer_impl = layer_impl->parent()) {
2235 if (!layer_impl->scrollable())
2236 continue;
2237
[email protected]adeda572014-01-31 00:49:472238 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532239 // Only allow bubble scrolling when the scroll is in the direction to make
2240 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472241 gfx::Vector2dF applied_delta;
2242 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532243 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472244 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2245 applied_delta = pending_delta - excess_delta;
2246 pending_delta = excess_delta;
2247 // Force updating of vertical adjust values if needed.
[email protected]f42cffb2014-03-08 18:03:252248 if (applied_delta.y() != 0) {
2249 did_scroll_top_controls = true;
[email protected]adeda572014-01-31 00:49:472250 layer_impl->ScrollbarParametersDidChange();
[email protected]f42cffb2014-03-08 18:03:252251 }
[email protected]a2b5ded2013-05-20 21:32:532252 }
2253 // Track root layer deltas for reporting overscroll.
2254 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422255 }
2256
[email protected]c1bb5af2013-03-13 19:06:272257 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272258 // Gesture events need to be transformed from viewport coordinates to local
2259 // layer coordinates so that the scrolling contents exactly follow the
2260 // user's finger. In contrast, wheel events represent a fixed amount of
2261 // scrolling so we can just apply them directly.
2262 if (!wheel_scrolling_) {
2263 float scale_from_viewport_to_screen_space = device_scale_factor_;
2264 applied_delta =
2265 ScrollLayerWithViewportSpaceDelta(layer_impl,
2266 scale_from_viewport_to_screen_space,
2267 viewport_point, pending_delta);
2268 } else {
2269 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142270 }
[email protected]94f206c12012-08-25 00:09:142271
[email protected]5b8a865c2014-04-03 00:35:512272 if (layer_impl == InnerViewportScrollLayer()) {
2273 unused_root_delta.Subtract(applied_delta);
2274 const float kOverscrollEpsilon = 0.01f;
2275 if (std::abs(unused_root_delta.x()) < kOverscrollEpsilon)
2276 unused_root_delta.set_x(0.0f);
2277 if (std::abs(unused_root_delta.y()) < kOverscrollEpsilon)
2278 unused_root_delta.set_y(0.0f);
2279 }
2280
[email protected]c1bb5af2013-03-13 19:06:272281 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292282 float move_threshold = 0.1f;
2283 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2284 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2285 did_scroll_x |= did_move_layer_x;
2286 did_scroll_y |= did_move_layer_y;
2287 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142288 // Scrolls should always bubble between the outer and inner viewports
2289 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2290 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272291 continue;
[email protected]db102612013-12-20 22:04:122292 else
2293 break;
[email protected]94f206c12012-08-25 00:09:142294 }
[email protected]a2b5ded2013-05-20 21:32:532295
[email protected]c1bb5af2013-03-13 19:06:272296 did_lock_scrolling_layer_ = true;
2297 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202298 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272299 break;
[email protected]94f206c12012-08-25 00:09:142300 }
[email protected]94f206c12012-08-25 00:09:142301
[email protected]c1bb5af2013-03-13 19:06:272302 // If the applied delta is within 45 degrees of the input delta, bail out to
2303 // make it easier to scroll just one layer in one direction without
2304 // affecting any of its parents.
2305 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212306 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272307 applied_delta, pending_delta) < angle_threshold) {
2308 pending_delta = gfx::Vector2d();
2309 break;
[email protected]4a23c374c2012-12-08 08:38:552310 }
[email protected]c1bb5af2013-03-13 19:06:272311
2312 // Allow further movement only on an axis perpendicular to the direction in
2313 // which the layer moved.
2314 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212315 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272316
[email protected]a2b5ded2013-05-20 21:32:532317 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272318 break;
2319 }
2320
[email protected]f42cffb2014-03-08 18:03:252321 bool did_scroll_content = did_scroll_x || did_scroll_y;
2322 if (did_scroll_content) {
[email protected]c1bb5af2013-03-13 19:06:272323 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442324 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272325 client_->RenewTreePriority();
2326 }
[email protected]a2b5ded2013-05-20 21:32:532327
[email protected]2bd503f2013-07-23 05:35:292328 // Scrolling along an axis resets accumulated root overscroll for that axis.
2329 if (did_scroll_x)
2330 accumulated_root_overscroll_.set_x(0);
2331 if (did_scroll_y)
2332 accumulated_root_overscroll_.set_y(0);
2333
[email protected]a2b5ded2013-05-20 21:32:532334 accumulated_root_overscroll_ += unused_root_delta;
[email protected]485a42dc2014-03-26 22:44:342335 bool did_overscroll = !unused_root_delta.IsZero();
[email protected]a2b5ded2013-05-20 21:32:532336 if (did_overscroll && input_handler_client_) {
[email protected]635b0e92014-04-02 21:48:012337 input_handler_client_->DidOverscroll(accumulated_root_overscroll_,
2338 unused_root_delta);
[email protected]a2b5ded2013-05-20 21:32:532339 }
2340
[email protected]f42cffb2014-03-08 18:03:252341 return did_scroll_content || did_scroll_top_controls;
[email protected]4a23c374c2012-12-08 08:38:552342}
2343
[email protected]be782f52013-03-23 21:36:142344// This implements scrolling by page as described here:
2345// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2346// for events with WHEEL_PAGESCROLL set.
[email protected]47a723f2014-03-05 12:42:492347bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
[email protected]c28df4c12013-05-22 17:36:492348 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142349 DCHECK(wheel_scrolling_);
2350
2351 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2352 layer_impl;
2353 layer_impl = layer_impl->parent()) {
2354 if (!layer_impl->scrollable())
2355 continue;
2356
[email protected]adeda572014-01-31 00:49:472357 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142358 continue;
2359
[email protected]adeda572014-01-31 00:49:472360 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142361
2362 // These magical values match WebKit and are designed to scroll nearly the
2363 // entire visible content height but leave a bit of overlap.
2364 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492365 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142366 page = -page;
2367
2368 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2369
2370 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2371
2372 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142373 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442374 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142375 client_->RenewTreePriority();
2376 return true;
2377 }
2378
2379 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2380 }
2381
2382 return false;
2383}
2384
[email protected]1960a712013-04-30 17:06:472385void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2386 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2387 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2388 active_tree_->SetRootLayerScrollOffsetDelegate(
2389 root_layer_scroll_offset_delegate_);
2390}
2391
2392void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2393 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2394 client_->SetNeedsCommitOnImplThread();
2395}
2396
[email protected]c1bb5af2013-03-13 19:06:272397void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2398 active_tree_->ClearCurrentlyScrollingLayer();
2399 did_lock_scrolling_layer_ = false;
[email protected]f161ca9e2014-04-01 13:57:012400 scroll_affects_scroll_handler_ = false;
[email protected]a2b5ded2013-05-20 21:32:532401 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142402}
2403
[email protected]c1bb5af2013-03-13 19:06:272404void LayerTreeHostImpl::ScrollEnd() {
2405 if (top_controls_manager_)
2406 top_controls_manager_->ScrollEnd();
2407 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052408 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142409}
2410
[email protected]5ff3c9782013-04-29 17:35:122411InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462412 if (!active_tree_->CurrentlyScrollingLayer())
2413 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212414
[email protected]75147f52013-08-02 19:11:462415 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472416 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2417 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462418 ClearCurrentlyScrollingLayer();
2419 return ScrollIgnored;
2420 }
2421
[email protected]db102612013-12-20 22:04:122422 if (!wheel_scrolling_) {
2423 // Allow the fling to lock to the first layer that moves after the initial
2424 // fling |ScrollBy()| event.
2425 did_lock_scrolling_layer_ = false;
2426 should_bubble_scrolls_ = false;
2427 }
[email protected]df0c42342013-10-08 20:52:122428
[email protected]75147f52013-08-02 19:11:462429 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212430}
2431
[email protected]bf1cfd9a2013-09-26 05:43:022432float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472433 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022434 LayerImpl* layer_impl) {
2435 if (!layer_impl)
2436 return std::numeric_limits<float>::max();
2437
2438 gfx::Rect layer_impl_bounds(
2439 layer_impl->content_bounds());
2440
2441 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2442 layer_impl->screen_space_transform(),
2443 layer_impl_bounds);
2444
2445 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2446 device_viewport_point);
2447}
2448
[email protected]47a723f2014-03-05 12:42:492449void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) {
[email protected]bf1cfd9a2013-09-26 05:43:022450 if (!EnsureRenderSurfaceLayerList())
2451 return;
2452
[email protected]f620b0e72013-10-01 21:38:242453 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2454 device_scale_factor_);
2455
2456 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2457 device_viewport_point,
2458 active_tree_->RenderSurfaceLayerList());
2459 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2460 return;
2461
2462 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2463 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2464 scroll_layer_id_when_mouse_over_scrollbar_);
2465
[email protected]5cd9ac12014-02-05 21:48:532466 // The check for a null scroll_layer_impl below was added to see if it will
2467 // eliminate the crashes described in http://crbug.com/326635.
2468 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242469 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532470 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2471 : NULL;
[email protected]f620b0e72013-10-01 21:38:242472 if (animation_controller) {
[email protected]1dc06162014-03-26 22:54:452473 animation_controller->DidMouseMoveOffScrollbar(CurrentFrameTimeTicks());
[email protected]f620b0e72013-10-01 21:38:242474 StartScrollbarAnimation();
2475 }
2476 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2477 }
2478
[email protected]bf1cfd9a2013-09-26 05:43:022479 bool scroll_on_main_thread = false;
[email protected]f161ca9e2014-04-01 13:57:012480 LayerImpl* scroll_layer_impl =
2481 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2482 InputHandler::Gesture,
2483 layer_impl,
2484 &scroll_on_main_thread,
2485 NULL);
[email protected]bf1cfd9a2013-09-26 05:43:022486 if (scroll_on_main_thread || !scroll_layer_impl)
2487 return;
2488
2489 ScrollbarAnimationController* animation_controller =
2490 scroll_layer_impl->scrollbar_animation_controller();
2491 if (!animation_controller)
2492 return;
2493
[email protected]adeda572014-01-31 00:49:472494 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2495 float distance_to_scrollbar = std::numeric_limits<float>::max();
2496 for (LayerImpl::ScrollbarSet::iterator it =
2497 scroll_layer_impl->scrollbars()->begin();
2498 it != scroll_layer_impl->scrollbars()->end();
2499 ++it)
2500 distance_to_scrollbar =
2501 std::min(distance_to_scrollbar,
2502 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022503
2504 bool should_animate = animation_controller->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452505 CurrentFrameTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202506 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022507 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022508}
2509
[email protected]f620b0e72013-10-01 21:38:242510bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472511 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242512 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2513 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2514 layer_impl = active_tree_->LayerById(scroll_layer_id);
2515 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2516 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202517 bool should_animate =
2518 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452519 CurrentFrameTimeTicks(), 0);
[email protected]534236b2013-10-25 21:19:202520 if (should_animate)
2521 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242522 } else {
2523 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2524 }
2525
2526 return true;
2527 }
2528
2529 return false;
2530}
2531
[email protected]c1bb5af2013-03-13 19:06:272532void LayerTreeHostImpl::PinchGestureBegin() {
2533 pinch_gesture_active_ = true;
2534 previous_pinch_anchor_ = gfx::Point();
2535 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132536 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472537 if (active_tree_->OuterViewportScrollLayer()) {
2538 active_tree_->SetCurrentlyScrollingLayer(
2539 active_tree_->OuterViewportScrollLayer());
2540 } else {
2541 active_tree_->SetCurrentlyScrollingLayer(
2542 active_tree_->InnerViewportScrollLayer());
2543 }
[email protected]a9bda5e2013-10-25 18:43:372544 if (top_controls_manager_)
2545 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142546}
2547
[email protected]c1bb5af2013-03-13 19:06:272548void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
[email protected]47a723f2014-03-05 12:42:492549 const gfx::Point& anchor) {
[email protected]b93480862014-02-20 13:07:032550 if (!InnerViewportScrollLayer())
2551 return;
[email protected]d3afa112012-12-08 06:24:282552
[email protected]b93480862014-02-20 13:07:032553 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282554
[email protected]c1bb5af2013-03-13 19:06:272555 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2556 // position over the course of the magnify.
2557 float page_scale_delta = active_tree_->page_scale_delta();
2558 gfx::PointF previous_scale_anchor =
2559 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2560 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2561 page_scale_delta = active_tree_->page_scale_delta();
2562 gfx::PointF new_scale_anchor =
2563 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2564 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2565
2566 previous_pinch_anchor_ = anchor;
2567
2568 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472569 // If clamping the inner viewport scroll offset causes a change, it should
2570 // be accounted for from the intended move.
2571 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272572
[email protected]adeda572014-01-31 00:49:472573 // We manually manage the bubbling behaviour here as it is different to that
2574 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2575 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2576 // 2) we don't want the directional limitations on the unused parts that
2577 // ScrollBy() implements, and
2578 // 3) pinching should not engage the top controls manager.
2579 gfx::Vector2dF unused = OuterViewportScrollLayer()
2580 ? OuterViewportScrollLayer()->ScrollBy(move)
2581 : move;
2582
2583 if (!unused.IsZero()) {
2584 InnerViewportScrollLayer()->ScrollBy(unused);
2585 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2586 }
[email protected]c1bb5af2013-03-13 19:06:272587
[email protected]c1bb5af2013-03-13 19:06:272588 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442589 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272590 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282591}
2592
[email protected]c1bb5af2013-03-13 19:06:272593void LayerTreeHostImpl::PinchGestureEnd() {
2594 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132595 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2596 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2597 ClearCurrentlyScrollingLayer();
2598 }
[email protected]a9bda5e2013-10-25 18:43:372599 if (top_controls_manager_)
2600 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272601 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142602}
2603
[email protected]c1bb5af2013-03-13 19:06:272604static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2605 LayerImpl* layer_impl) {
2606 if (!layer_impl)
2607 return;
[email protected]94f206c12012-08-25 00:09:142608
[email protected]c1bb5af2013-03-13 19:06:272609 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472610 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272611 if (!scroll_delta.IsZero()) {
2612 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392613 scroll.layer_id = layer_impl->id();
2614 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272615 scroll_info->scrolls.push_back(scroll);
2616 layer_impl->SetSentScrollDelta(scroll_delta);
2617 }
[email protected]94f206c12012-08-25 00:09:142618
[email protected]c1bb5af2013-03-13 19:06:272619 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2620 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142621}
2622
[email protected]c1bb5af2013-03-13 19:06:272623scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2624 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302625
[email protected]c1bb5af2013-03-13 19:06:272626 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392627 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2628 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302629
[email protected]c1bb5af2013-03-13 19:06:272630 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302631}
2632
[email protected]c1bb5af2013-03-13 19:06:272633void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282634 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102635}
2636
[email protected]adeda572014-01-31 00:49:472637void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2638 DCHECK(InnerViewportScrollLayer());
2639 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2640 ? OuterViewportScrollLayer()
2641 : InnerViewportScrollLayer();
2642
2643 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2644
2645 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2646 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2647}
2648
[email protected]27152642014-03-11 20:42:002649void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks monotonic_time) {
[email protected]adeda572014-01-31 00:49:472650 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272651 return;
2652
[email protected]27152642014-03-11 20:42:002653 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2654 // time.
2655 double monotonic_time_for_cc_animations =
2656 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472657 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272658
[email protected]34e1b512013-09-26 17:32:332659 if (!page_scale_animation_->IsAnimationStarted())
[email protected]27152642014-03-11 20:42:002660 page_scale_animation_->StartAnimation(monotonic_time_for_cc_animations);
[email protected]34e1b512013-09-26 17:32:332661
[email protected]27152642014-03-11 20:42:002662 active_tree_->SetPageScaleDelta(page_scale_animation_->PageScaleFactorAtTime(
2663 monotonic_time_for_cc_animations) /
2664 active_tree_->page_scale_factor());
2665 gfx::Vector2dF next_scroll = page_scale_animation_->ScrollOffsetAtTime(
2666 monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272667
[email protected]adeda572014-01-31 00:49:472668 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442669 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272670
[email protected]27152642014-03-11 20:42:002671 if (page_scale_animation_->IsAnimationCompleteAtTime(
2672 monotonic_time_for_cc_animations)) {
[email protected]c1bb5af2013-03-13 19:06:272673 page_scale_animation_.reset();
2674 client_->SetNeedsCommitOnImplThread();
2675 client_->RenewTreePriority();
2676 }
[email protected]829ad972013-01-28 23:36:102677}
2678
[email protected]ffb2720f2013-03-15 19:18:372679void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082680 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372681 return;
2682 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472683 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172684 return;
[email protected]5f7d9112014-01-17 21:31:422685 if (scroll.IsZero()) {
2686 // This may happen on the first animation step. Force redraw otherwise
2687 // the animation would stop because of no new frames.
2688 SetNeedsRedraw();
2689 } else {
[email protected]adeda572014-01-31 00:49:472690 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422691 scroll, 1.f / active_tree_->total_page_scale_factor()));
2692 }
[email protected]ffb2720f2013-03-15 19:18:372693}
2694
[email protected]27152642014-03-11 20:42:002695void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time) {
[email protected]8e0176d2013-03-21 03:14:522696 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362697 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272698 !active_tree_->root_layer())
2699 return;
2700
2701 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2702
[email protected]27152642014-03-11 20:42:002703 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2704 // time.
2705 double monotonic_time_for_cc_animations =
2706 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]c1bb5af2013-03-13 19:06:272707 AnimationRegistrar::AnimationControllerMap copy =
2708 animation_registrar_->active_animation_controllers();
2709 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2710 iter != copy.end();
2711 ++iter)
[email protected]27152642014-03-11 20:42:002712 (*iter).second->Animate(monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272713
[email protected]4e4136d2013-11-29 02:22:442714 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082715}
2716
[email protected]3d9f7432013-04-06 00:35:182717void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522718 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362719 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272720 !active_tree_->root_layer())
2721 return;
2722
2723 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2724 scoped_ptr<AnimationEventsVector> events =
2725 make_scoped_ptr(new AnimationEventsVector);
2726 AnimationRegistrar::AnimationControllerMap copy =
2727 animation_registrar_->active_animation_controllers();
2728 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2729 iter != copy.end();
2730 ++iter)
[email protected]3d9f7432013-04-06 00:35:182731 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272732
2733 if (!events->empty()) {
[email protected]85b57502014-03-11 15:37:482734 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass());
[email protected]c1bb5af2013-03-13 19:06:272735 }
[email protected]131a0c22013-02-12 18:31:082736}
2737
[email protected]c1bb5af2013-03-13 19:06:272738base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2739 return base::TimeDelta::FromSeconds(1);
2740}
2741
[email protected]50644642013-06-20 13:58:552742void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352743 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502744 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352745 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552746 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352747 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552748 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352749 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552750 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352751}
2752
[email protected]b5174d712013-08-28 08:10:432753void LayerTreeHostImpl::SetOffscreenContextProvider(
2754 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2755 if (!offscreen_context_provider.get()) {
2756 offscreen_context_provider_ = NULL;
2757 return;
2758 }
2759
2760 if (!offscreen_context_provider->BindToCurrentThread()) {
2761 offscreen_context_provider_ = NULL;
2762 return;
2763 }
2764
2765 offscreen_context_provider_ = offscreen_context_provider;
2766}
2767
[email protected]c1bb5af2013-03-13 19:06:272768std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2769 std::string str;
2770 if (active_tree_->root_layer()) {
2771 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2772 base::JSONWriter::WriteWithOptions(
2773 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2774 }
2775 return str;
2776}
2777
[email protected]c1bb5af2013-03-13 19:06:272778int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362779 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272780}
2781
[email protected]c1bb5af2013-03-13 19:06:272782void LayerTreeHostImpl::SendManagedMemoryStats(
2783 size_t memory_visible_bytes,
2784 size_t memory_visible_and_nearby_bytes,
2785 size_t memory_use_bytes) {
2786 if (!renderer_)
2787 return;
2788
2789 // Round the numbers being sent up to the next 8MB, to throttle the rate
2790 // at which we spam the GPU process.
2791 static const size_t rounding_step = 8 * 1024 * 1024;
2792 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2793 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2794 rounding_step);
2795 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2796 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2797 last_sent_memory_visible_and_nearby_bytes_ ==
2798 memory_visible_and_nearby_bytes &&
2799 last_sent_memory_use_bytes_ == memory_use_bytes) {
2800 return;
2801 }
2802 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2803 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2804 last_sent_memory_use_bytes_ = memory_use_bytes;
2805
2806 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2807 last_sent_memory_visible_and_nearby_bytes_,
2808 last_sent_memory_use_bytes_);
2809}
2810
2811void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2812 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2813}
2814
2815void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2816 base::TimeTicks time) {
2817 if (!layer)
2818 return;
2819
2820 ScrollbarAnimationController* scrollbar_controller =
2821 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352822 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202823 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422824 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2825 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442826 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202827 }
[email protected]c1bb5af2013-03-13 19:06:272828
2829 for (size_t i = 0; i < layer->children().size(); ++i)
2830 AnimateScrollbarsRecursive(layer->children()[i], time);
2831}
2832
[email protected]21c9dee72013-06-15 01:20:052833void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202834 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]1dc06162014-03-26 22:54:452835 StartScrollbarAnimationRecursive(RootLayer(), CurrentFrameTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202836}
2837
2838void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2839 base::TimeTicks time) {
2840 if (!layer)
2841 return;
2842
2843 ScrollbarAnimationController* scrollbar_controller =
2844 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352845 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2846 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202847 if (delay > base::TimeDelta())
2848 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352849 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442850 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202851 }
2852
2853 for (size_t i = 0; i < layer->children().size(); ++i)
2854 StartScrollbarAnimationRecursive(layer->children()[i], time);
2855}
2856
[email protected]c1bb5af2013-03-13 19:06:272857void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2858 if (!tile_manager_)
2859 return;
2860
[email protected]1bcced22013-09-24 13:51:192861 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272862 return;
[email protected]1bcced22013-09-24 13:51:192863 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082864 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272865}
2866
[email protected]1dc06162014-03-26 22:54:452867void LayerTreeHostImpl::UpdateCurrentFrameTime() {
2868 DCHECK(current_frame_timeticks_.is_null());
2869 current_frame_timeticks_ = gfx::FrameTime::Now();
2870}
2871
[email protected]8347d692013-05-17 23:22:382872void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552873 current_frame_timeticks_ = base::TimeTicks();
[email protected]c1bb5af2013-03-13 19:06:272874}
2875
[email protected]fb7425a2013-04-22 16:28:552876base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
[email protected]1dc06162014-03-26 22:54:452877 // Try to use the current frame time to keep animations non-jittery. But if
2878 // we're not in a frame (because this is during an input event or a delayed
2879 // task), fall back to physical time. This should still be monotonic.
2880 if (!current_frame_timeticks_.is_null())
2881 return current_frame_timeticks_;
[email protected]de2cf8c2013-10-25 19:46:462882 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052883}
2884
[email protected]34806722013-08-09 23:51:212885scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2886 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272887 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222888 if (this->pending_tree_)
2889 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272890 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212891 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272892 if (tile_manager_)
2893 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2894 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222895 if (pending_tree_)
2896 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212897 if (frame)
2898 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222899 return state.PassAs<base::Value>();
2900}
2901
2902scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222903 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2904 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472905 if (tile_manager_)
2906 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272907 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082908}
2909
[email protected]6e7fdeb2013-07-09 14:28:382910void LayerTreeHostImpl::SetDebugState(
2911 const LayerTreeDebugState& new_debug_state) {
2912 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2913 return;
2914 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272915 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242916
[email protected]6e7fdeb2013-07-09 14:28:382917 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122918 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382919 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022920}
2921
[email protected]741fba422013-09-20 03:34:142922void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2923 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572924 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572925
[email protected]efa48412013-09-05 15:30:162926 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142927 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162928 case UIResourceBitmap::CLAMP_TO_EDGE:
2929 wrap_mode = GL_CLAMP_TO_EDGE;
2930 break;
2931 case UIResourceBitmap::REPEAT:
2932 wrap_mode = GL_REPEAT;
2933 break;
2934 }
2935
[email protected]c9280762013-08-01 06:28:572936 // Allow for multiple creation requests with the same UIResourceId. The
2937 // previous resource is simply deleted.
2938 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2939 if (id)
2940 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122941
2942 ResourceFormat format = resource_provider_->best_texture_format();
2943 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2944 format = ETC1;
[email protected]c9280762013-08-01 06:28:572945 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142946 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162947 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142948 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122949 format);
[email protected]c9280762013-08-01 06:28:572950
[email protected]efbdb3a2013-10-04 00:35:132951 UIResourceData data;
2952 data.resource_id = id;
2953 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512954 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132955
2956 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032957
2958 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572959 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032960 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142961 gfx::Rect(bitmap.GetSize()),
2962 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572963 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482964 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572965}
2966
2967void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2968 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2969 if (id) {
2970 resource_provider_->DeleteResource(id);
2971 ui_resource_map_.erase(uid);
2972 }
[email protected]127bdc1a2013-09-11 01:44:482973 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572974}
2975
[email protected]127bdc1a2013-09-11 01:44:482976void LayerTreeHostImpl::EvictAllUIResources() {
2977 if (ui_resource_map_.empty())
2978 return;
2979
[email protected]5f4dc4f2013-09-05 14:58:212980 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2981 iter != ui_resource_map_.end();
2982 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482983 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132984 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212985 }
2986 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482987
2988 client_->SetNeedsCommitOnImplThread();
2989 client_->OnCanDrawStateChanged(CanDraw());
2990 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212991}
2992
[email protected]c9280762013-08-01 06:28:572993ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2994 UIResourceId uid) const {
2995 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2996 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132997 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572998 return 0;
2999}
3000
[email protected]709c9542013-10-26 01:43:513001bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
3002 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3003 DCHECK(iter != ui_resource_map_.end());
3004 return iter->second.opaque;
3005}
3006
[email protected]127bdc1a2013-09-11 01:44:483007bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
3008 return !evicted_ui_resources_.empty();
3009}
3010
3011void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
3012 std::set<UIResourceId>::iterator found_in_evicted =
3013 evicted_ui_resources_.find(uid);
3014 if (found_in_evicted == evicted_ui_resources_.end())
3015 return;
3016 evicted_ui_resources_.erase(found_in_evicted);
3017 if (evicted_ui_resources_.empty())
3018 client_->OnCanDrawStateChanged(CanDraw());
3019}
3020
[email protected]5e5648a2013-11-18 00:39:333021void LayerTreeHostImpl::ScheduleMicroBenchmark(
3022 scoped_ptr<MicroBenchmarkImpl> benchmark) {
3023 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
3024}
3025
[email protected]6be422b2013-12-08 06:47:313026void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3027 swap_promise_monitor_.insert(monitor);
3028}
3029
3030void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3031 swap_promise_monitor_.erase(monitor);
3032}
3033
3034void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3035 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3036 for (; it != swap_promise_monitor_.end(); it++)
3037 (*it)->OnSetNeedsRedrawOnImpl();
3038}
3039
[email protected]d3143c732012-10-05 19:17:593040} // namespace cc