blob: 072e8e7efbf2d6ae30186f8514e2d47b790961b8 [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]f620b0e72013-10-01 21:38:24224 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08225 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47226 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27227 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27228 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07229 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34230 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29231 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07232 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27233 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13234 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36235 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29236 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27237 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02238 debug_rect_history_(DebugRectHistory::Create()),
[email protected]dd909b1f2014-03-18 00:19:52239 texture_mailbox_deleter_(new TextureMailboxDeleter(
240 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
241 : proxy_->MainThreadTaskRunner())),
[email protected]d7626ffd2013-03-29 00:17:42242 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27243 last_sent_memory_visible_bytes_(0),
244 last_sent_memory_visible_and_nearby_bytes_(0),
245 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55246 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32247 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12248 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32249 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28250 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17251 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43252 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19253 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33254 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57255 need_to_update_visible_tiles_before_draw_(false),
[email protected]767f38d72014-03-18 21:26:41256#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:36257 did_lose_called_(false),
258#endif
[email protected]dbe759a2013-12-02 19:23:02259 shared_bitmap_manager_(manager),
260 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27261 DCHECK(proxy_->IsImplThread());
262 DidVisibilityChange(this, visible_);
263
[email protected]8e0176d2013-03-21 03:14:52264 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27265
[email protected]8e0176d2013-03-21 03:14:52266 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27267 top_controls_manager_ =
268 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52269 settings.top_controls_height,
270 settings.top_controls_show_threshold,
271 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27272 }
273
[email protected]8e0176d2013-03-21 03:14:52274 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27275
276 // LTHI always has an active tree.
277 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51278 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
279 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10280}
281
[email protected]c1bb5af2013-03-13 19:06:27282LayerTreeHostImpl::~LayerTreeHostImpl() {
283 DCHECK(proxy_->IsImplThread());
284 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51285 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
286 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27287
[email protected]200a9c062013-05-20 04:34:37288 if (input_handler_client_) {
289 input_handler_client_->WillShutdown();
290 input_handler_client_ = NULL;
291 }
292
[email protected]75deb742013-06-24 20:19:18293 // The layer trees must be destroyed before the layer tree host. We've
294 // made a contract with our animation controllers that the registrar
295 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40296 if (recycle_tree_)
297 recycle_tree_->Shutdown();
298 if (pending_tree_)
299 pending_tree_->Shutdown();
300 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18301 recycle_tree_.reset();
302 pending_tree_.reset();
303 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14304}
305
[email protected]097a0cbf2013-12-12 17:04:58306void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
307 // If the begin frame data was handled, then scroll and scale set was applied
308 // by the main thread, so the active tree needs to be updated as if these sent
309 // values were applied and committed.
310 if (did_handle) {
311 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
312 active_tree_->ResetContentsTexturesPurged();
313 }
314}
315
[email protected]c1bb5af2013-03-13 19:06:27316void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29317
[email protected]c1bb5af2013-03-13 19:06:27318void LayerTreeHostImpl::CommitComplete() {
319 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08320
[email protected]8e0176d2013-03-21 03:14:52321 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25322 // Impl-side painting needs an update immediately post-commit to have the
323 // opportunity to create tilings. Other paths can call UpdateDrawProperties
324 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50325 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27326 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04327 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26328 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08329 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38330 NotifyReadyToActivate();
331 else
332 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27333 } else {
334 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01335 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
336 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27337 }
[email protected]3ba4cae2013-01-16 03:58:38338
[email protected]c1bb5af2013-03-13 19:06:27339 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33340
341 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14342}
343
[email protected]6133cc232013-07-30 18:47:07344bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27345 // Note: If you are changing this function or any other function that might
346 // affect the result of CanDraw, make sure to call
347 // client_->OnCanDrawStateChanged in the proper places and update the
348 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14349
[email protected]6133cc232013-07-30 18:47:07350 if (!renderer_) {
351 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
352 TRACE_EVENT_SCOPE_THREAD);
353 return false;
354 }
355
356 // Must have an OutputSurface if |renderer_| is not NULL.
357 DCHECK(output_surface_);
358
359 // TODO(boliu): Make draws without root_layer work and move this below
360 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27361 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42362 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
363 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22364 return false;
[email protected]c1bb5af2013-03-13 19:06:27365 }
[email protected]6133cc232013-07-30 18:47:07366
367 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
368 return true;
369
[email protected]54af03522013-09-05 00:43:28370 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42371 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
372 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27373 return false;
374 }
375 if (active_tree_->ViewportSizeInvalid()) {
376 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42377 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
378 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27379 return false;
380 }
[email protected]c1bb5af2013-03-13 19:06:27381 if (active_tree_->ContentsTexturesPurged()) {
382 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42383 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
384 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27385 return false;
386 }
[email protected]127bdc1a2013-09-11 01:44:48387 if (EvictedUIResourcesExist()) {
388 TRACE_EVENT_INSTANT0(
389 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
390 TRACE_EVENT_SCOPE_THREAD);
391 return false;
392 }
[email protected]c1bb5af2013-03-13 19:06:27393 return true;
[email protected]2f1acc262012-11-16 21:42:22394}
395
[email protected]27152642014-03-11 20:42:00396void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time) {
[email protected]200a9c062013-05-20 04:34:37397 if (input_handler_client_)
398 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27399 AnimatePageScale(monotonic_time);
[email protected]27152642014-03-11 20:42:00400 AnimateLayers(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27401 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37402 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14403}
404
[email protected]c1bb5af2013-03-13 19:06:27405void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26406 if (!tile_manager_)
407 return;
[email protected]c48536a52013-09-14 00:02:08408 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26409 return;
[email protected]54af03522013-09-05 00:43:28410
[email protected]c48536a52013-09-14 00:02:08411 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19412 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27413
414 size_t memory_required_bytes;
415 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03416 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27417 size_t memory_used_bytes;
418 tile_manager_->GetMemoryStats(&memory_required_bytes,
419 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03420 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27421 &memory_used_bytes);
422 SendManagedMemoryStats(memory_required_bytes,
423 memory_nice_to_have_bytes,
424 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57425
426 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15427}
428
[email protected]243e4f12014-02-05 09:18:42429void LayerTreeHostImpl::StartPageScaleAnimation(
430 const gfx::Vector2d& target_offset,
431 bool anchor_point,
432 float page_scale,
433 base::TimeDelta duration) {
[email protected]adeda572014-01-31 00:49:47434 if (!InnerViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:27435 return;
436
[email protected]adeda572014-01-31 00:49:47437 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27438 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28439 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27440
[email protected]85d136f782013-04-26 22:04:40441 // Easing constants experimentally determined.
442 scoped_ptr<TimingFunction> timing_function =
443 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
444
[email protected]c1bb5af2013-03-13 19:06:27445 page_scale_animation_ =
446 PageScaleAnimation::Create(scroll_total,
447 active_tree_->total_page_scale_factor(),
448 viewport_size,
449 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40450 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27451
452 if (anchor_point) {
453 gfx::Vector2dF anchor(target_offset);
454 page_scale_animation_->ZoomWithAnchor(anchor,
455 page_scale,
456 duration.InSecondsF());
457 } else {
458 gfx::Vector2dF scaled_target_offset = target_offset;
459 page_scale_animation_->ZoomTo(scaled_target_offset,
460 page_scale,
461 duration.InSecondsF());
462 }
463
[email protected]4e4136d2013-11-29 02:22:44464 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27465 client_->SetNeedsCommitOnImplThread();
466 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15467}
468
[email protected]c1bb5af2013-03-13 19:06:27469void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44470 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15471}
472
[email protected]47a723f2014-03-05 12:42:49473bool LayerTreeHostImpl::HaveTouchEventHandlersAt(
474 const gfx::Point& viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07475 if (!settings_.touch_hit_testing)
476 return true;
[email protected]c1bb5af2013-03-13 19:06:27477 if (!EnsureRenderSurfaceLayerList())
478 return false;
[email protected]f57bbc02012-11-21 07:02:15479
[email protected]c1bb5af2013-03-13 19:06:27480 gfx::PointF device_viewport_point =
481 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15482
[email protected]c974d5d2013-10-24 01:02:48483 LayerImpl* layer_impl =
484 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
485 device_viewport_point,
486 active_tree_->RenderSurfaceLayerList());
487 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27488}
489
[email protected]6be422b2013-12-08 06:47:31490scoped_ptr<SwapPromiseMonitor>
491LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
492 ui::LatencyInfo* latency) {
493 return scoped_ptr<SwapPromiseMonitor>(
494 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08495}
496
[email protected]c1bb5af2013-03-13 19:06:27497void LayerTreeHostImpl::TrackDamageForAllSurfaces(
498 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28499 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27500 // For now, we use damage tracking to compute a global scissor. To do this, we
501 // must compute all damage tracking before drawing anything, so that we know
502 // the root damage rect. The root damage rect is then used to scissor each
503 // surface.
504
505 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06506 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27507 --surface_index) {
508 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
509 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
510 DCHECK(render_surface);
511 render_surface->damage_tracker()->UpdateDamageTrackingState(
512 render_surface->layer_list(),
513 render_surface_layer->id(),
514 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
515 render_surface->content_rect(),
516 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48517 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27518 }
519}
520
[email protected]34806722013-08-09 23:51:21521scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
522 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
523 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
524 value->SetBoolean("has_no_damage", has_no_damage);
525
526 // Quad data can be quite large, so only dump render passes if we select
527 // cc.debug.quads.
528 bool quads_enabled;
529 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
530 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
531 if (quads_enabled) {
532 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
533 for (size_t i = 0; i < render_passes.size(); ++i)
534 render_pass_list->Append(render_passes[i]->AsValue().release());
535 value->Set("render_passes", render_pass_list.release());
536 }
537 return value.PassAs<base::Value>();
538}
539
[email protected]c1bb5af2013-03-13 19:06:27540void LayerTreeHostImpl::FrameData::AppendRenderPass(
541 scoped_ptr<RenderPass> render_pass) {
542 render_passes_by_id[render_pass->id] = render_pass.get();
543 render_passes.push_back(render_pass.Pass());
544}
545
[email protected]ffbb2212013-06-02 23:47:59546static DrawMode GetDrawMode(OutputSurface* output_surface) {
547 if (output_surface->ForcedDrawToSoftwareDevice()) {
548 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09549 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59550 return DRAW_MODE_HARDWARE;
551 } else {
[email protected]a7f35682013-10-22 23:05:57552 DCHECK_EQ(!output_surface->software_device(),
553 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59554 return DRAW_MODE_SOFTWARE;
555 }
556}
557
[email protected]34ba1ffb2014-03-05 06:55:03558static void AppendQuadsForLayer(
559 RenderPass* target_render_pass,
560 LayerImpl* layer,
561 const OcclusionTracker<LayerImpl>& occlusion_tracker,
562 AppendQuadsData* append_quads_data) {
[email protected]c1bb5af2013-03-13 19:06:27563 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49564 QuadCuller quad_culler(&target_render_pass->quad_list,
565 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27566 layer,
567 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27568 for_surface);
569 layer->AppendQuads(&quad_culler, append_quads_data);
570}
571
572static void AppendQuadsForRenderSurfaceLayer(
573 RenderPass* target_render_pass,
574 LayerImpl* layer,
575 const RenderPass* contributing_render_pass,
[email protected]34ba1ffb2014-03-05 06:55:03576 const OcclusionTracker<LayerImpl>& occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27577 AppendQuadsData* append_quads_data) {
578 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49579 QuadCuller quad_culler(&target_render_pass->quad_list,
580 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27581 layer,
582 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27583 for_surface);
584
585 bool is_replica = false;
586 layer->render_surface()->AppendQuads(&quad_culler,
587 append_quads_data,
588 is_replica,
589 contributing_render_pass->id);
590
591 // Add replica after the surface so that it appears below the surface.
592 if (layer->has_replica()) {
593 is_replica = true;
594 layer->render_surface()->AppendQuads(&quad_culler,
595 append_quads_data,
596 is_replica,
597 contributing_render_pass->id);
598 }
599}
600
601static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08602 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20603 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06604 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27605 RenderPass* target_render_pass,
606 LayerImpl* root_layer,
607 SkColor screen_background_color,
[email protected]34ba1ffb2014-03-05 06:55:03608 const OcclusionTracker<LayerImpl>& occlusion_tracker) {
[email protected]c1bb5af2013-03-13 19:06:27609 if (!root_layer || !SkColorGetA(screen_background_color))
610 return;
611
612 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
613 if (fill_region.IsEmpty())
614 return;
615
[email protected]3744e27b2013-11-06 21:44:08616 // Divide the fill region into the part to be filled with the overhang
617 // resource and the part to be filled with the background color.
618 Region screen_background_color_region = fill_region;
619 Region overhang_region;
620 if (overhang_resource_id) {
621 overhang_region = fill_region;
622 overhang_region.Subtract(root_scroll_layer_rect);
623 screen_background_color_region.Intersect(root_scroll_layer_rect);
624 }
625
[email protected]c1bb5af2013-03-13 19:06:27626 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49627 QuadCuller quad_culler(&target_render_pass->quad_list,
628 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27629 root_layer,
630 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27631 for_surface);
632
633 // Manually create the quad state for the gutter quads, as the root layer
634 // doesn't have any bounds and so can't generate this itself.
635 // TODO(danakj): Make the gutter quads generated by the solid color layer
636 // (make it smarter about generating quads to fill unoccluded areas).
637
638 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
639 float opacity = 1.f;
640 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49641 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]7785e1002014-02-20 21:04:32642 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27643 root_target_rect.size(),
644 root_target_rect,
645 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01646 false,
[email protected]7bbeaf4e2013-11-26 10:27:22647 opacity,
648 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15649
[email protected]3744e27b2013-11-06 21:44:08650 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27651 fill_rects.has_rect();
652 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32653 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24654 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08655 // Skip the quad culler and just append the quads directly to avoid
656 // occlusion checks.
657 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]9bf06c72014-03-07 18:16:24658 quad->SetNew(shared_quad_state,
659 screen_space_rect,
660 visible_screen_space_rect,
661 screen_background_color,
662 false);
[email protected]0d8c7eaa2014-03-13 05:26:30663 quad_culler.MaybeAppend(quad.PassAs<DrawQuad>());
[email protected]3744e27b2013-11-06 21:44:08664 }
665 for (Region::Iterator fill_rects(overhang_region);
666 fill_rects.has_rect();
667 fill_rects.next()) {
668 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32669 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24670 gfx::Rect opaque_screen_space_rect = screen_space_rect;
671 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08672 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
673 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
674 tex_quad->SetNew(
675 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32676 screen_space_rect,
[email protected]9bf06c72014-03-07 18:16:24677 opaque_screen_space_rect,
678 visible_screen_space_rect,
[email protected]3744e27b2013-11-06 21:44:08679 overhang_resource_id,
680 false,
[email protected]7785e1002014-02-20 21:04:32681 gfx::PointF(
682 screen_space_rect.x() / overhang_resource_scaled_size.width(),
683 screen_space_rect.y() / overhang_resource_scaled_size.height()),
684 gfx::PointF(
685 screen_space_rect.right() / overhang_resource_scaled_size.width(),
686 screen_space_rect.bottom() /
687 overhang_resource_scaled_size.height()),
[email protected]3744e27b2013-11-06 21:44:08688 screen_background_color,
689 vertex_opacity,
690 false);
[email protected]0d8c7eaa2014-03-13 05:26:30691 quad_culler.MaybeAppend(tex_quad.PassAs<DrawQuad>());
[email protected]c1bb5af2013-03-13 19:06:27692 }
[email protected]467b3612012-08-28 07:41:16693}
694
[email protected]7ddfe7e82014-01-30 07:22:11695DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::CalculateRenderPasses(
696 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27697 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11698 DCHECK(CanDraw());
699 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32700
[email protected]c1bb5af2013-03-13 19:06:27701 TrackDamageForAllSurfaces(active_tree_->root_layer(),
702 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14703
[email protected]e0341352013-04-06 05:01:20704 // If the root render surface has no visible damage, then don't generate a
705 // frame at all.
706 RenderSurfaceImpl* root_surface =
707 active_tree_->root_layer()->render_surface();
708 bool root_surface_has_no_visible_damage =
709 !root_surface->damage_tracker()->current_damage_rect().Intersects(
710 root_surface->content_rect());
711 bool root_surface_has_contributing_layers =
712 !root_surface->layer_list().empty();
[email protected]5fda7f32014-03-12 20:44:29713 bool hud_wants_to_draw_ = active_tree_->hud_layer() &&
714 active_tree_->hud_layer()->IsAnimatingHUDContents();
[email protected]e0341352013-04-06 05:01:20715 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23716 root_surface_has_no_visible_damage &&
[email protected]5fda7f32014-03-12 20:44:29717 active_tree_->LayersWithCopyOutputRequest().empty() &&
718 !hud_wants_to_draw_) {
[email protected]e0341352013-04-06 05:01:20719 TRACE_EVENT0("cc",
720 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
721 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07722 DCHECK(!output_surface_->capabilities()
723 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11724 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20725 }
726
[email protected]c1bb5af2013-03-13 19:06:27727 TRACE_EVENT1("cc",
728 "LayerTreeHostImpl::CalculateRenderPasses",
729 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06730 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14731
[email protected]c1bb5af2013-03-13 19:06:27732 // Create the render passes in dependency order.
733 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06734 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27735 --surface_index) {
736 LayerImpl* render_surface_layer =
737 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45738 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
739
740 bool should_draw_into_render_pass =
741 render_surface_layer->parent() == NULL ||
742 render_surface->contributes_to_drawn_surface() ||
743 render_surface_layer->HasCopyRequest();
744 if (should_draw_into_render_pass)
745 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27746 }
[email protected]94f206c12012-08-25 00:09:14747
[email protected]f636ce82014-03-10 22:33:08748 // When we are displaying the HUD, change the root damage rect to cover the
749 // entire root surface. This will disable partial-swap/scissor optimizations
750 // that would prevent the HUD from updating, since the HUD does not cause
751 // damage itself, to prevent it from messing with damage visualizations. Since
752 // damage visualizations are done off the LayerImpls and RenderSurfaceImpls,
753 // changing the RenderPass does not affect them.
754 if (active_tree_->hud_layer()) {
755 RenderPass* root_pass = frame->render_passes.back();
756 root_pass->damage_rect = root_pass->output_rect;
757 }
758
[email protected]34ba1ffb2014-03-05 06:55:03759 OcclusionTracker<LayerImpl> occlusion_tracker(
[email protected]97c6a7342014-03-12 20:36:43760 active_tree_->root_layer()->render_surface()->content_rect());
[email protected]c1bb5af2013-03-13 19:06:27761 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52762 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14763
[email protected]846f455b2013-03-18 19:07:41764 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27765 occlusion_tracker.set_occluding_screen_space_rects_container(
766 &frame->occluding_screen_space_rects);
767 }
[email protected]846f455b2013-03-18 19:07:41768 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27769 occlusion_tracker.set_non_occluding_screen_space_rects_container(
770 &frame->non_occluding_screen_space_rects);
771 }
[email protected]94f206c12012-08-25 00:09:14772
[email protected]ba1b33e2014-02-28 16:44:51773 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27774 // occlusion and performing culling during the tree walk.
[email protected]ba1b33e2014-02-28 16:44:51775 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14776
[email protected]c1bb5af2013-03-13 19:06:27777 // Typically when we are missing a texture and use a checkerboard quad, we
778 // still draw the frame. However when the layer being checkerboarded is moving
779 // due to an impl-animation, we drop the frame to avoid flashing due to the
780 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11781 DrawSwapReadbackResult::DrawResult draw_result =
782 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25783 // When we have a copy request for a layer, we need to draw no matter
784 // what, as the layer may disappear after this frame.
785 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14786
[email protected]372bad5f2013-03-21 16:38:43787 int layers_drawn = 0;
788
[email protected]ffbb2212013-06-02 23:47:59789 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
790
[email protected]c1bb5af2013-03-13 19:06:27791 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09792 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27793 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09794 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27795 it != end;
796 ++it) {
797 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09798 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27799 RenderPass* target_render_pass =
800 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14801
[email protected]e47b0a0a2013-11-18 23:26:22802 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14803
[email protected]30fe19ff2013-07-04 00:54:45804 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30805
[email protected]18a70192013-04-26 16:18:25806 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36807 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25808 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11809 it->TakeCopyRequestsAndTransformToTarget(
810 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25811 }
[email protected]30fe19ff2013-07-04 00:54:45812 } else if (it.represents_contributing_render_surface() &&
813 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27814 RenderPass::Id contributing_render_pass_id =
815 it->render_surface()->RenderPassId();
816 RenderPass* contributing_render_pass =
817 frame->render_passes_by_id[contributing_render_pass_id];
818 AppendQuadsForRenderSurfaceLayer(target_render_pass,
819 *it,
820 contributing_render_pass,
821 occlusion_tracker,
822 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45823 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09824 !it->visible_content_rect().IsEmpty()) {
[email protected]7d6d6c92014-03-06 01:18:50825 bool occluded = occlusion_tracker.Occluded(it->render_target(),
826 it->visible_content_rect(),
827 it->draw_transform());
[email protected]fbc293322013-10-01 05:07:15828 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27829 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59830
[email protected]c1bb5af2013-03-13 19:06:27831 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57832
[email protected]c1bb5af2013-03-13 19:06:27833 if (it->HasContributingDelegatedRenderPasses()) {
834 RenderPass::Id contributing_render_pass_id =
835 it->FirstContributingRenderPassId();
836 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
837 frame->render_passes_by_id.end()) {
838 RenderPass* render_pass =
839 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14840
[email protected]c1bb5af2013-03-13 19:06:27841 AppendQuadsData append_quads_data(render_pass->id);
842 AppendQuadsForLayer(render_pass,
843 *it,
844 occlusion_tracker,
845 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57846
[email protected]c1bb5af2013-03-13 19:06:27847 contributing_render_pass_id =
848 it->NextContributingRenderPassId(contributing_render_pass_id);
849 }
[email protected]94f206c12012-08-25 00:09:14850 }
851
[email protected]c1bb5af2013-03-13 19:06:27852 AppendQuadsForLayer(target_render_pass,
853 *it,
854 occlusion_tracker,
855 &append_quads_data);
856 }
[email protected]89228202012-08-29 03:20:30857
[email protected]372bad5f2013-03-21 16:38:43858 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14859 }
860
[email protected]e1e768f2013-03-26 08:48:09861 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27862 bool layer_has_animating_transform =
863 it->screen_space_transform_is_animating() ||
864 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11865 if (layer_has_animating_transform) {
866 draw_result =
867 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
868 }
[email protected]c1bb5af2013-03-13 19:06:27869 }
870
[email protected]3d609bb2014-02-01 01:10:23871 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27872 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23873 if (active_tree()->RequiresHighResToDraw())
874 draw_result =
875 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
876 }
[email protected]c1bb5af2013-03-13 19:06:27877
878 occlusion_tracker.LeaveLayer(it);
879 }
880
[email protected]6133cc232013-07-30 18:47:07881 if (have_copy_request ||
882 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11883 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25884
[email protected]767f38d72014-03-18 21:26:41885#if DCHECK_IS_ON
[email protected]c1bb5af2013-03-13 19:06:27886 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
887 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
888 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
889 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
890 != frame->render_passes_by_id.end());
891 }
[email protected]94f206c12012-08-25 00:09:14892#endif
[email protected]c1bb5af2013-03-13 19:06:27893 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14894
[email protected]c1bb5af2013-03-13 19:06:27895 if (!active_tree_->has_transparent_background()) {
896 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08897 AppendQuadsToFillScreen(
898 ResourceIdForUIResource(overhang_ui_resource_id_),
899 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
900 active_tree_->RootScrollLayerDeviceViewportBounds(),
901 frame->render_passes.back(),
902 active_tree_->root_layer(),
903 active_tree_->background_color(),
904 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27905 }
[email protected]94f206c12012-08-25 00:09:14906
[email protected]c1bb5af2013-03-13 19:06:27907 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48908 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14909
[email protected]30fe19ff2013-07-04 00:54:45910 // Any copy requests left in the tree are not going to get serviced, and
911 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11912 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
913 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
914 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
915 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45916 }
[email protected]19334ac2013-12-12 23:59:11917 for (size_t i = 0; i < requests_to_abort.size(); ++i)
918 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45919
[email protected]e0341352013-04-06 05:01:20920 // If we're making a frame to draw, it better have at least one render pass.
921 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48922
923 // Should only have one render pass in resourceless software mode.
924 if (output_surface_->ForcedDrawToSoftwareDevice())
925 DCHECK_EQ(1u, frame->render_passes.size());
926
[email protected]7ddfe7e82014-01-30 07:22:11927 return draw_result;
[email protected]94f206c12012-08-25 00:09:14928}
929
[email protected]200a9c062013-05-20 04:34:37930void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
931 if (input_handler_client_)
932 input_handler_client_->MainThreadHasStoppedFlinging();
933}
934
[email protected]3d9f7432013-04-06 00:35:18935void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
936 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15937 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01938 if (should_background_tick)
939 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15940
[email protected]cd77bb62014-03-27 05:16:36941 bool enabled = should_background_tick && needs_animate_layers();
[email protected]3d9f7432013-04-06 00:35:18942
[email protected]c1bb5af2013-03-13 19:06:27943 // Lazily create the time_source adapter so that we can vary the interval for
944 // testing.
945 if (!time_source_client_adapter_) {
946 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
947 this,
[email protected]810d40b72013-06-20 18:26:15948 DelayBasedTimeSource::Create(
949 LowFrequencyAnimationInterval(),
950 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
951 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27952 }
[email protected]94f206c12012-08-25 00:09:14953
[email protected]c1bb5af2013-03-13 19:06:27954 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14955}
956
[email protected]b8384e22013-12-03 02:20:48957void LayerTreeHostImpl::DidAnimateScrollOffset() {
958 client_->SetNeedsCommitOnImplThread();
959 client_->RenewTreePriority();
960}
961
[email protected]0023fc72014-01-10 20:05:06962void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02963 viewport_damage_rect_.Union(damage_rect);
964}
965
[email protected]c1bb5af2013-03-13 19:06:27966static inline RenderPass* FindRenderPassById(
967 RenderPass::Id render_pass_id,
968 const LayerTreeHostImpl::FrameData& frame) {
969 RenderPassIdHashMap::const_iterator it =
970 frame.render_passes_by_id.find(render_pass_id);
971 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14972}
973
[email protected]c1bb5af2013-03-13 19:06:27974static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
975 LayerTreeHostImpl::FrameData* frame) {
976 RenderPass* remove_render_pass =
977 FindRenderPassById(remove_render_pass_id, *frame);
978 // The pass was already removed by another quad - probably the original, and
979 // we are the replica.
980 if (!remove_render_pass)
981 return;
982 RenderPassList& render_passes = frame->render_passes;
983 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
984 render_passes.end(),
985 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14986
[email protected]c1bb5af2013-03-13 19:06:27987 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14988
[email protected]c1bb5af2013-03-13 19:06:27989 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
990 frame->render_passes.erase(to_remove);
991 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14992
[email protected]c1bb5af2013-03-13 19:06:27993 // Now follow up for all RenderPass quads and remove their RenderPasses
994 // recursively.
995 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29996 QuadList::ConstBackToFrontIterator quad_list_iterator =
997 quad_list.BackToFrontBegin();
998 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27999 ++quad_list_iterator) {
1000 DrawQuad* current_quad = (*quad_list_iterator);
1001 if (current_quad->material != DrawQuad::RENDER_PASS)
1002 continue;
[email protected]94f206c12012-08-25 00:09:141003
[email protected]c1bb5af2013-03-13 19:06:271004 RenderPass::Id next_remove_render_pass_id =
1005 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1006 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1007 }
[email protected]94f206c12012-08-25 00:09:141008}
1009
[email protected]c1bb5af2013-03-13 19:06:271010bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1011 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1012 const RenderPass* render_pass =
1013 FindRenderPassById(quad.render_pass_id, frame);
1014 if (!render_pass)
1015 return false;
[email protected]94f206c12012-08-25 00:09:141016
[email protected]c1bb5af2013-03-13 19:06:271017 // If any quad or RenderPass draws into this RenderPass, then keep it.
1018 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291019 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1020 quad_list.BackToFrontBegin();
1021 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271022 ++quad_list_iterator) {
1023 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141024
[email protected]c1bb5af2013-03-13 19:06:271025 if (current_quad->material != DrawQuad::RENDER_PASS)
1026 return false;
[email protected]94f206c12012-08-25 00:09:141027
[email protected]c1bb5af2013-03-13 19:06:271028 const RenderPass* contributing_pass = FindRenderPassById(
1029 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1030 if (contributing_pass)
1031 return false;
1032 }
1033 return true;
[email protected]94f206c12012-08-25 00:09:141034}
1035
1036// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271037template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271038 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1039 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141040
1041// static
[email protected]c1bb5af2013-03-13 19:06:271042template <typename RenderPassCuller>
1043void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1044 FrameData* frame) {
1045 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1046 it != culler.RenderPassListEnd(frame->render_passes);
1047 it = culler.RenderPassListNext(it)) {
1048 const RenderPass* current_pass = frame->render_passes[it];
1049 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291050 QuadList::ConstBackToFrontIterator quad_list_iterator =
1051 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141052
[email protected]ed511b8d2013-03-25 03:29:291053 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271054 ++quad_list_iterator) {
1055 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141056
[email protected]c1bb5af2013-03-13 19:06:271057 if (current_quad->material != DrawQuad::RENDER_PASS)
1058 continue;
[email protected]94f206c12012-08-25 00:09:141059
[email protected]c1bb5af2013-03-13 19:06:271060 const RenderPassDrawQuad* render_pass_quad =
1061 RenderPassDrawQuad::MaterialCast(current_quad);
1062 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1063 continue;
[email protected]94f206c12012-08-25 00:09:141064
[email protected]c1bb5af2013-03-13 19:06:271065 // We are changing the vector in the middle of iteration. Because we
1066 // delete render passes that draw into the current pass, we are
1067 // guaranteed that any data from the iterator to the end will not
1068 // change. So, capture the iterator position from the end of the
1069 // list, and restore it after the change.
1070 size_t position_from_end = frame->render_passes.size() - it;
1071 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1072 it = frame->render_passes.size() - position_from_end;
1073 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141074 }
[email protected]c1bb5af2013-03-13 19:06:271075 }
[email protected]94f206c12012-08-25 00:09:141076}
1077
[email protected]7ddfe7e82014-01-30 07:22:111078DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::PrepareToDraw(
1079 FrameData* frame,
1080 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471081 TRACE_EVENT1("cc",
1082 "LayerTreeHostImpl::PrepareToDraw",
1083 "SourceFrameNumber",
1084 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141085
[email protected]425e5332013-09-21 04:10:031086 if (need_to_update_visible_tiles_before_draw_ &&
1087 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1088 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191089 }
[email protected]425e5332013-09-21 04:10:031090 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191091
[email protected]7d19dc342013-05-02 22:02:041092 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271093
[email protected]c1bb5af2013-03-13 19:06:271094 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1095 frame->render_passes.clear();
1096 frame->render_passes_by_id.clear();
1097 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201098 frame->contains_incomplete_tile = false;
1099 frame->has_no_damage = false;
1100
[email protected]0023fc72014-01-10 20:05:061101 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201102 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021103 device_viewport_damage_rect.Union(viewport_damage_rect_);
1104 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201105
1106 active_tree_->root_layer()->render_surface()->damage_tracker()->
1107 AddDamageNextUpdate(device_viewport_damage_rect);
1108 }
[email protected]94f206c12012-08-25 00:09:141109
[email protected]7ddfe7e82014-01-30 07:22:111110 DrawSwapReadbackResult::DrawResult draw_result = CalculateRenderPasses(frame);
1111 if (draw_result != DrawSwapReadbackResult::DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071112 DCHECK(!output_surface_->capabilities()
1113 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111114 return draw_result;
[email protected]6133cc232013-07-30 18:47:071115 }
[email protected]94f206c12012-08-25 00:09:141116
[email protected]7ddfe7e82014-01-30 07:22:111117 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1118 // this function is called again.
1119 return draw_result;
[email protected]94f206c12012-08-25 00:09:141120}
1121
[email protected]0309ba5e02013-06-26 04:11:081122void LayerTreeHostImpl::EvictTexturesForTesting() {
1123 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1124}
1125
[email protected]4f48f6e2013-08-27 06:33:381126void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1127 NOTREACHED();
1128}
1129
[email protected]425e5332013-09-21 04:10:031130void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1131 DidInitializeVisibleTile();
1132}
1133
[email protected]c1bb5af2013-03-13 19:06:271134void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1135 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121136
[email protected]c1bb5af2013-03-13 19:06:271137 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351138 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351139 ManagedMemoryPolicy::PriorityCutoffToValue(
1140 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351141 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271142 if (evicted_resources) {
1143 active_tree_->SetContentsTexturesPurged();
1144 if (pending_tree_)
1145 pending_tree_->SetContentsTexturesPurged();
1146 client_->SetNeedsCommitOnImplThread();
1147 client_->OnCanDrawStateChanged(CanDraw());
1148 client_->RenewTreePriority();
1149 }
1150 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431151
[email protected]8be1a9bf2013-05-01 03:45:191152 UpdateTileManagerMemoryPolicy(policy);
1153}
1154
1155void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1156 const ManagedMemoryPolicy& policy) {
1157 if (!tile_manager_)
1158 return;
1159
[email protected]8be1a9bf2013-05-01 03:45:191160 // TODO(reveman): We should avoid keeping around unused resources if
1161 // possible. crbug.com/224475
[email protected]25489c02014-02-13 23:18:411162 global_tile_state_.hard_memory_limit_in_bytes = 0;
1163 global_tile_state_.soft_memory_limit_in_bytes = 0;
1164 if (visible_ && policy.bytes_limit_when_visible > 0) {
1165 global_tile_state_.hard_memory_limit_in_bytes =
1166 policy.bytes_limit_when_visible;
1167 global_tile_state_.soft_memory_limit_in_bytes =
[email protected]831414e2014-02-22 02:27:371168 (static_cast<int64>(global_tile_state_.hard_memory_limit_in_bytes) *
1169 settings_.max_memory_for_prepaint_percentage) /
1170 100;
[email protected]25489c02014-02-13 23:18:411171 }
1172 // Unused limit is calculated from soft-limit, as hard-limit may
1173 // be very high and shouldn't typically be exceeded.
[email protected]1bcced22013-09-24 13:51:191174 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
[email protected]25489c02014-02-13 23:18:411175 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1176 settings_.max_unused_resource_memory_percentage) /
1177 100);
[email protected]1bcced22013-09-24 13:51:191178 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191179 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1180 visible_ ?
1181 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351182 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191183 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081184
[email protected]c48536a52013-09-14 00:02:081185 DidModifyTilePriorities();
1186}
1187
1188void LayerTreeHostImpl::DidModifyTilePriorities() {
1189 DCHECK(settings_.impl_side_painting);
1190 // Mark priorities as dirty and schedule a ManageTiles().
1191 tile_priorities_dirty_ = true;
1192 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141193}
1194
[email protected]86126792013-03-16 20:07:541195void LayerTreeHostImpl::DidInitializeVisibleTile() {
1196 // TODO(reveman): Determine tiles that changed and only damage
1197 // what's necessary.
1198 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031199 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541200 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471201}
1202
[email protected]94bf75c2013-06-12 13:20:041203void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381204 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041205}
1206
[email protected]3f2ff112013-08-03 02:41:071207void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1208 SetManagedMemoryPolicy(policy, zero_budget_);
1209}
1210
[email protected]5f07afc2013-07-19 05:17:181211void LayerTreeHostImpl::SetTreeActivationCallback(
1212 const base::Closure& callback) {
1213 DCHECK(proxy_->IsImplThread());
1214 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261215 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181216}
1217
[email protected]c1bb5af2013-03-13 19:06:271218void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071219 const ManagedMemoryPolicy& policy, bool zero_budget) {
1220 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271221 return;
[email protected]61de5812012-11-08 07:03:441222
[email protected]3f2ff112013-08-03 02:41:071223 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1224
1225 cached_managed_memory_policy_ = policy;
1226 zero_budget_ = zero_budget;
1227 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1228
1229 if (old_policy == actual_policy)
1230 return;
1231
1232 if (!proxy_->HasImplThread()) {
1233 // In single-thread mode, this can be called on the main thread by
1234 // GLRenderer::OnMemoryAllocationChanged.
1235 DebugScopedSetImplThread impl_thread(proxy_);
1236 EnforceManagedMemoryPolicy(actual_policy);
1237 } else {
1238 DCHECK(proxy_->IsImplThread());
1239 EnforceManagedMemoryPolicy(actual_policy);
1240 }
[email protected]50644642013-06-20 13:58:551241
[email protected]d7626ffd2013-03-29 00:17:421242 // If there is already enough memory to draw everything imaginable and the
1243 // new memory limit does not change this, then do not re-commit. Don't bother
1244 // skipping commits if this is not visible (commits don't happen when not
1245 // visible, there will almost always be a commit when this becomes visible).
1246 bool needs_commit = true;
1247 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071248 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1249 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1250 actual_policy.priority_cutoff_when_visible ==
1251 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421252 needs_commit = false;
1253 }
1254
[email protected]d7626ffd2013-03-29 00:17:421255 if (needs_commit)
1256 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141257}
1258
[email protected]f224cc92013-06-06 23:23:321259void LayerTreeHostImpl::SetExternalDrawConstraints(
1260 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061261 const gfx::Rect& viewport,
1262 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281263 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321264 external_transform_ = transform;
1265 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281266 external_clip_ = clip;
1267 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321268}
1269
[email protected]0023fc72014-01-10 20:05:061270void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311271 if (damage_rect.IsEmpty())
1272 return;
1273 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031274 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1275}
1276
[email protected]daea3d42013-10-23 17:04:501277void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1278 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451279}
1280
[email protected]4d7e46a2013-11-08 05:33:401281void LayerTreeHostImpl::DidSwapBuffers() {
1282 client_->DidSwapBuffersOnImplThread();
1283}
1284
[email protected]a7335e0b2013-09-18 09:34:511285void LayerTreeHostImpl::OnSwapBuffersComplete() {
1286 client_->OnSwapBuffersCompleteOnImplThread();
1287}
1288
1289void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271290 // TODO(piman): We may need to do some validation on this ack before
1291 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511292 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291293 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161294}
1295
[email protected]c1bb5af2013-03-13 19:06:271296void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1297 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291298}
1299
[email protected]c1bb5af2013-03-13 19:06:271300CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1301 CompositorFrameMetadata metadata;
1302 metadata.device_scale_factor = device_scale_factor_;
1303 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1304 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1305 metadata.root_layer_size = active_tree_->ScrollableSize();
1306 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1307 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1308 if (top_controls_manager_) {
1309 metadata.location_bar_offset =
1310 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1311 metadata.location_bar_content_translation =
1312 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011313 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271314 }
[email protected]bf189f62012-12-18 03:42:111315
[email protected]adeda572014-01-31 00:49:471316 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111317 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271318
[email protected]adeda572014-01-31 00:49:471319 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271320
1321 return metadata;
[email protected]bf189f62012-12-18 03:42:111322}
1323
[email protected]6f50b8fa2013-07-01 19:51:491324static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1325 layer->DidBeginTracing();
1326}
[email protected]37349bc2013-06-04 01:31:521327
[email protected]f0c2a242013-03-15 19:34:521328void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1329 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351330 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271331 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201332
[email protected]3519b872013-07-30 07:17:501333 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191334 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071335 DCHECK(!output_surface_->capabilities()
1336 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201337 return;
[email protected]3519b872013-07-30 07:17:501338 }
[email protected]e0341352013-04-06 05:01:201339
[email protected]c1bb5af2013-03-13 19:06:271340 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141341
[email protected]213fcfc2013-08-24 14:59:271342 fps_counter_->SaveTimeStamp(frame_begin_time,
1343 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351344 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091345 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351346 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431347
[email protected]c1bb5af2013-03-13 19:06:271348 if (tile_manager_) {
1349 memory_history_->SaveEntry(
1350 tile_manager_->memory_stats_from_last_assign());
1351 }
[email protected]1191d9d2013-02-02 06:00:331352
[email protected]846f455b2013-03-18 19:07:411353 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021354 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271355 active_tree_->root_layer(),
[email protected]f636ce82014-03-10 22:33:081356 active_tree_->hud_layer(),
[email protected]c1bb5af2013-03-13 19:06:271357 *frame->render_surface_layer_list,
1358 frame->occluding_screen_space_rects,
1359 frame->non_occluding_screen_space_rects,
1360 debug_state_);
1361 }
[email protected]94f206c12012-08-25 00:09:141362
[email protected]a848c102013-03-26 08:59:091363 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1364 const RenderingStats& stats =
1365 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181366 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091367 }
1368
[email protected]37349bc2013-06-04 01:31:521369 bool is_new_trace;
1370 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1371 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491372 if (pending_tree_) {
1373 LayerTreeHostCommon::CallFunctionForSubtree(
1374 pending_tree_->root_layer(),
1375 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1376 }
1377 LayerTreeHostCommon::CallFunctionForSubtree(
1378 active_tree_->root_layer(),
1379 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521380 }
1381
[email protected]9197dbcb2013-05-15 20:28:511382 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211383 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1384 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1385 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081386
[email protected]c1bb5af2013-03-13 19:06:271387 // Because the contents of the HUD depend on everything else in the frame, the
1388 // contents of its texture are updated as the last thing before the frame is
1389 // drawn.
[email protected]671f6672013-10-01 07:31:071390 if (active_tree_->hud_layer()) {
1391 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531392 active_tree_->hud_layer()->UpdateHudTexture(
1393 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071394 }
[email protected]94f206c12012-08-25 00:09:141395
[email protected]b09c1942013-05-10 00:06:371396 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]251699b2013-10-09 00:21:261397 bool disable_picture_quad_image_filtering =
1398 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321399
[email protected]b09c1942013-05-10 00:06:371400 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321401 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301402 temp_software_renderer->DrawFrame(&frame->render_passes,
1403 NULL,
1404 device_scale_factor_,
1405 DeviceViewport(),
1406 DeviceClip(),
[email protected]59fee302013-12-10 17:53:301407 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371408 } else {
[email protected]b5174d712013-08-28 08:10:431409 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321410 offscreen_context_provider_.get(),
1411 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301412 DeviceViewport(),
1413 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261414 false);
[email protected]b09c1942013-05-10 00:06:371415 }
[email protected]c1bb5af2013-03-13 19:06:271416 // The render passes should be consumed by the renderer.
1417 DCHECK(frame->render_passes.empty());
1418 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141419
[email protected]c1bb5af2013-03-13 19:06:271420 // The next frame should start by assuming nothing has changed, and changes
1421 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321422 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1423 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541424 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271425 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1426 DidDrawDamagedArea();
1427 }
1428 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351429
[email protected]d73094482014-02-13 21:28:281430 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331431 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1432 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181433 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141434}
1435
[email protected]c1bb5af2013-03-13 19:06:271436void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1437 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1438 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521439
[email protected]c1bb5af2013-03-13 19:06:271440 // Once all layers have been drawn, pending texture uploads should no
1441 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151442 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141443}
1444
[email protected]c1bb5af2013-03-13 19:06:271445void LayerTreeHostImpl::FinishAllRendering() {
1446 if (renderer_)
1447 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141448}
1449
[email protected]c1bb5af2013-03-13 19:06:271450bool LayerTreeHostImpl::IsContextLost() {
1451 DCHECK(proxy_->IsImplThread());
1452 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141453}
1454
[email protected]7a8bcd262014-01-15 12:54:581455const RendererCapabilitiesImpl&
1456LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271457 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141458}
1459
[email protected]e0341352013-04-06 05:01:201460bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231461 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191462 if (frame.has_no_damage) {
1463 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201464 return false;
[email protected]b69c1db2013-11-27 00:05:191465 }
[email protected]4a28a612013-11-27 02:06:331466 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551467 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331468 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161469 return true;
[email protected]94f206c12012-08-25 00:09:141470}
1471
[email protected]daea3d42013-10-23 17:04:501472void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451473 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501474 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451475}
1476
[email protected]fef74fd2014-02-27 06:28:171477gfx::SizeF LayerTreeHostImpl::ComputeInnerViewportContainerSize() const {
1478 gfx::SizeF dip_size =
1479 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
1480
1481 float top_offset =
1482 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
1483
1484 return gfx::SizeF(dip_size.width(),
1485 dip_size.height() - top_offset - overdraw_bottom_height_);
1486}
1487
1488void LayerTreeHostImpl::UpdateInnerViewportContainerSize() {
1489 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
1490 if (!container_layer)
1491 return;
1492
[email protected]05dfe4f2014-02-27 19:52:501493 // We pass the value returned from UnscaledScrollableViewportSize() here as
1494 // it accounts for scrollbar dimensions when
1495 // container_layer->masks_to_bounds() is set.
1496 container_layer->SetTemporaryImplBounds(UnscaledScrollableViewportSize());
[email protected]fef74fd2014-02-27 06:28:171497}
1498
[email protected]54af03522013-09-05 00:43:281499gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341500 // Use the root container layer bounds if it clips to them, otherwise, the
1501 // true viewport size should be used.
[email protected]fef74fd2014-02-27 06:28:171502 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341503 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441504 DCHECK(!top_controls_manager_);
1505 DCHECK_EQ(0, overdraw_bottom_height_);
1506 return container_layer->bounds();
1507 }
1508
[email protected]fef74fd2014-02-27 06:28:171509 return ComputeInnerViewportContainerSize();
1510}
[email protected]ffb2720f2013-03-15 19:18:371511
[email protected]fef74fd2014-02-27 06:28:171512float LayerTreeHostImpl::VerticalAdjust() const {
1513 if (!active_tree_->InnerViewportContainerLayer())
1514 return 0;
1515
1516 return active_tree_->InnerViewportContainerLayer()->BoundsDelta().y();
[email protected]ffb2720f2013-03-15 19:18:371517}
1518
[email protected]c1bb5af2013-03-13 19:06:271519void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191520 if (resource_provider_)
1521 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011522 // TODO(jamesr): The renderer_ check is needed to make some of the
1523 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1524 // important) in production. We should adjust the test to not need this.
1525 if (renderer_)
1526 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]767f38d72014-03-18 21:26:411527#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361528 did_lose_called_ = true;
1529#endif
[email protected]94f206c12012-08-25 00:09:141530}
1531
[email protected]c1bb5af2013-03-13 19:06:271532void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061533 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271534 DCHECK(renderer_);
1535 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141536}
1537
[email protected]59adb112013-04-09 04:48:441538bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471539 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011540}
1541
[email protected]c1bb5af2013-03-13 19:06:271542LayerImpl* LayerTreeHostImpl::RootLayer() const {
1543 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081544}
1545
[email protected]adeda572014-01-31 00:49:471546LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1547 return active_tree_->InnerViewportScrollLayer();
1548}
1549
1550LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1551 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081552}
1553
[email protected]c1bb5af2013-03-13 19:06:271554LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1555 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081556}
1557
[email protected]251699b2013-10-09 00:21:261558bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1559 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471560 (InnerViewportScrollLayer() &&
1561 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1562 (OuterViewportScrollLayer() &&
1563 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261564}
1565
[email protected]94f206c12012-08-25 00:09:141566// Content layers can be either directly scrollable or contained in an outer
1567// scrolling layer which applies the scroll transform. Given a content layer,
1568// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271569static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1570 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021571 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271572
1573 if (layer_impl->scrollable())
1574 return layer_impl;
1575
1576 if (layer_impl->DrawsContent() &&
1577 layer_impl->parent() &&
1578 layer_impl->parent()->scrollable())
1579 return layer_impl->parent();
1580
[email protected]bf1cfd9a2013-09-26 05:43:021581 return NULL;
[email protected]94f206c12012-08-25 00:09:141582}
1583
[email protected]c1bb5af2013-03-13 19:06:271584void LayerTreeHostImpl::CreatePendingTree() {
1585 CHECK(!pending_tree_);
1586 if (recycle_tree_)
1587 recycle_tree_.swap(pending_tree_);
1588 else
1589 pending_tree_ = LayerTreeImpl::create(this);
1590 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111591 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271592}
1593
[email protected]8bb6cfa2013-07-23 00:11:191594void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191595 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1596 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191597 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481598}
1599
[email protected]c1bb5af2013-03-13 19:06:271600void LayerTreeHostImpl::ActivatePendingTree() {
1601 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111602 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351603
[email protected]4f48f6e2013-08-27 06:33:381604 need_to_update_visible_tiles_before_draw_ = true;
1605
[email protected]1960a712013-04-30 17:06:471606 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271607 active_tree_->PushPersistedState(pending_tree_.get());
1608 if (pending_tree_->needs_full_tree_sync()) {
1609 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331610 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271611 active_tree_->DetachLayerTree(),
1612 active_tree_.get()));
1613 }
[email protected]b5651c22013-03-14 15:06:331614 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271615 active_tree_->root_layer());
1616 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511617
[email protected]c9280762013-08-01 06:28:571618 // Process any requests in the UI resource queue. The request queue is given
1619 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1620 // the swap.
1621 pending_tree_->ProcessUIResourceRequestQueue();
1622
[email protected]c1bb5af2013-03-13 19:06:271623 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511624
[email protected]c1bb5af2013-03-13 19:06:271625 // Now that we've synced everything from the pending tree to the active
1626 // tree, rename the pending tree the recycle tree so we can reuse it on the
1627 // next sync.
1628 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511629
[email protected]7d1b07e2013-10-01 17:31:301630 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471631 active_tree_->SetRootLayerScrollOffsetDelegate(
1632 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221633
[email protected]c1bb5af2013-03-13 19:06:271634 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441635 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271636 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241637
[email protected]a848c102013-03-26 08:59:091638 if (debug_state_.continuous_painting) {
1639 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431640 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091641 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1642 stats.main_stats.record_time +
1643 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271644 }
[email protected]2a61ad52013-05-13 14:01:291645
[email protected]fef74fd2014-02-27 06:28:171646 UpdateInnerViewportContainerSize();
[email protected]2a61ad52013-05-13 14:01:291647 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261648 if (!tree_activation_callback_.is_null())
1649 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011650
1651 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1652 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281653 devtools_instrumentation::DidActivateLayerTree(
1654 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271655}
1656
[email protected]c1bb5af2013-03-13 19:06:271657void LayerTreeHostImpl::SetVisible(bool visible) {
1658 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141659
[email protected]c1bb5af2013-03-13 19:06:271660 if (visible_ == visible)
1661 return;
1662 visible_ = visible;
1663 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121664 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141665
[email protected]3d609bb2014-02-01 01:10:231666 if (!visible_) {
1667 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481668 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231669 }
[email protected]127bdc1a2013-09-11 01:44:481670
[email protected]0fa42e02013-08-20 01:16:051671 // Evict tiles immediately if invisible since this tab may never get another
1672 // draw or timer tick.
1673 if (!visible_)
1674 ManageTiles();
1675
[email protected]c1bb5af2013-03-13 19:06:271676 if (!renderer_)
1677 return;
[email protected]94f206c12012-08-25 00:09:141678
[email protected]c1bb5af2013-03-13 19:06:271679 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141680}
1681
[email protected]6be422b2013-12-08 06:47:311682void LayerTreeHostImpl::SetNeedsRedraw() {
1683 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1684 client_->SetNeedsRedrawOnImplThread();
1685}
1686
[email protected]206a3922013-05-17 06:34:121687ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071688 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011689 // TODO(ernstm): The second condition disables pre-painting for all layers
1690 // when GPU rasterization is enabled. Once we selectively enable GPU
1691 // rasterization per layer, we also need to disable pre-painting selectively:
1692 // crbug.com/335387
1693 if (debug_state_.rasterize_only_visible_content ||
[email protected]a23530d2014-03-11 06:04:141694 settings_.rasterization_site == LayerTreeSettings::GpuRasterization) {
[email protected]3f2ff112013-08-03 02:41:071695 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291696 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071697 }
[email protected]206a3922013-05-17 06:34:121698
[email protected]3f2ff112013-08-03 02:41:071699 if (zero_budget_) {
1700 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071701 }
1702
[email protected]206a3922013-05-17 06:34:121703 return actual;
1704}
1705
[email protected]3f2ff112013-08-03 02:41:071706size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1707 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1708}
1709
[email protected]990e050a2013-09-23 18:50:211710int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1711 return ManagedMemoryPolicy::PriorityCutoffToValue(
1712 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1713}
1714
[email protected]50644642013-06-20 13:58:551715void LayerTreeHostImpl::ReleaseTreeResources() {
1716 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551717 SendReleaseResourcesRecursive(active_tree_->root_layer());
1718 if (pending_tree_ && pending_tree_->root_layer())
1719 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1720 if (recycle_tree_ && recycle_tree_->root_layer())
1721 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571722
[email protected]127bdc1a2013-09-11 01:44:481723 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551724}
1725
1726void LayerTreeHostImpl::CreateAndSetRenderer(
1727 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201728 ResourceProvider* resource_provider,
1729 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551730 DCHECK(!renderer_);
1731 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321732 renderer_ = DelegatingRenderer::Create(
1733 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091734 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551735 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321736 &settings_,
[email protected]50644642013-06-20 13:58:551737 output_surface,
1738 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111739 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241740 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551741 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321742 renderer_ = SoftwareRenderer::Create(
1743 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551744 }
1745
[email protected]84975832013-06-29 00:24:111746 if (renderer_) {
[email protected]50644642013-06-20 13:58:551747 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111748 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561749
1750 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1751 // initialized to get max texture size. Also, after releasing resources,
1752 // trees need another update to generate new ones.
1753 active_tree_->set_needs_update_draw_properties();
1754 if (pending_tree_)
1755 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591756 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111757 }
[email protected]50644642013-06-20 13:58:551758}
1759
[email protected]50af3c22013-07-13 03:50:201760void LayerTreeHostImpl::CreateAndSetTileManager(
1761 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011762 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371763 bool using_map_image,
1764 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201765 DCHECK(settings_.impl_side_painting);
1766 DCHECK(resource_provider);
[email protected]98805e62014-03-05 01:33:591767 DCHECK(proxy_->ImplThreadTaskRunner());
[email protected]b6eb8e332013-09-10 00:51:011768 tile_manager_ =
1769 TileManager::Create(this,
[email protected]98805e62014-03-05 01:33:591770 proxy_->ImplThreadTaskRunner(),
[email protected]b6eb8e332013-09-10 00:51:011771 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281772 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011773 rendering_stats_instrumentation_,
1774 using_map_image,
[email protected]4e472a42014-02-11 01:56:371775 allow_rasterize_on_demand,
[email protected]b4664142013-11-08 00:50:311776 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121777 GetMaxRasterTasksUsageBytes(context_provider),
1778 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011779
[email protected]50af3c22013-07-13 03:50:201780 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191781 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201782}
1783
[email protected]50644642013-06-20 13:58:551784void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071785 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551786}
1787
[email protected]c1bb5af2013-03-13 19:06:271788bool LayerTreeHostImpl::InitializeRenderer(
1789 scoped_ptr<OutputSurface> output_surface) {
[email protected]767f38d72014-03-18 21:26:411790#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361791 DCHECK(!renderer_ || did_lose_called_);
1792#endif
1793
[email protected]c1bb5af2013-03-13 19:06:271794 // Since we will create a new resource provider, we cannot continue to use
1795 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1796 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551797 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401798
[email protected]c1bb5af2013-03-13 19:06:271799 // Note: order is important here.
1800 renderer_.reset();
1801 tile_manager_.reset();
1802 resource_provider_.reset();
1803 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141804
[email protected]c1bb5af2013-03-13 19:06:271805 if (!output_surface->BindToClient(this))
1806 return false;
[email protected]be3181652012-09-25 13:02:131807
[email protected]a7f35682013-10-22 23:05:571808 scoped_ptr<ResourceProvider> resource_provider =
1809 ResourceProvider::Create(output_surface.get(),
1810 shared_bitmap_manager_,
1811 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341812 settings_.use_rgba_4444_textures,
1813 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551814 if (!resource_provider)
1815 return false;
[email protected]2b154b22013-06-07 09:03:271816
[email protected]50644642013-06-20 13:58:551817 if (output_surface->capabilities().deferred_gl_initialization)
1818 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131819
[email protected]50af3c22013-07-13 03:50:201820 bool skip_gl_renderer = false;
1821 CreateAndSetRenderer(
1822 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131823
[email protected]50644642013-06-20 13:58:551824 if (!renderer_)
1825 return false;
[email protected]6123930c2013-05-23 06:48:371826
[email protected]50644642013-06-20 13:58:551827 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371828 CreateAndSetTileManager(
1829 resource_provider.get(),
1830 output_surface->context_provider().get(),
1831 GetRendererCapabilities().using_map_image,
1832 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241833 }
1834
[email protected]daea3d42013-10-23 17:04:501835 // Setup BeginImplFrameEmulation if it's not supported natively
1836 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351837 const base::TimeDelta display_refresh_interval =
1838 base::TimeDelta::FromMicroseconds(
1839 base::Time::kMicrosecondsPerSecond /
1840 settings_.refresh_rate);
1841
[email protected]daea3d42013-10-23 17:04:501842 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151843 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351844 settings_.throttle_frame_production,
1845 display_refresh_interval);
1846 }
1847
1848 int max_frames_pending =
1849 output_surface->capabilities().max_frames_pending;
1850 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351851 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351852 output_surface->SetMaxFramesPending(max_frames_pending);
1853
[email protected]50644642013-06-20 13:58:551854 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271855 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141856
[email protected]c1bb5af2013-03-13 19:06:271857 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211858
[email protected]c1bb5af2013-03-13 19:06:271859 return true;
[email protected]94f206c12012-08-25 00:09:141860}
1861
[email protected]2b154b22013-06-07 09:03:271862bool LayerTreeHostImpl::DeferredInitialize(
1863 scoped_refptr<ContextProvider> offscreen_context_provider) {
1864 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551865 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091866 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271867
[email protected]50644642013-06-20 13:58:551868 ReleaseTreeResources();
1869 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271870
[email protected]d9ca99e2013-08-28 21:49:481871 bool resource_provider_success = resource_provider_->InitializeGL();
1872
1873 bool success = resource_provider_success;
1874 if (success) {
1875 bool skip_gl_renderer = false;
1876 CreateAndSetRenderer(
1877 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1878 if (!renderer_)
1879 success = false;
1880 }
1881
1882 if (success) {
1883 if (offscreen_context_provider.get() &&
1884 !offscreen_context_provider->BindToCurrentThread())
1885 success = false;
1886 }
1887
[email protected]50644642013-06-20 13:58:551888 if (success) {
1889 EnforceZeroBudget(false);
1890 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481891 } else {
1892 if (offscreen_context_provider.get()) {
1893 if (offscreen_context_provider->BindToCurrentThread())
1894 offscreen_context_provider->VerifyContexts();
1895 offscreen_context_provider = NULL;
1896 }
1897
1898 client_->DidLoseOutputSurfaceOnImplThread();
1899
1900 if (resource_provider_success) {
1901 // If this fails the context provider will be dropped from the output
1902 // surface and destroyed. But the GLRenderer expects the output surface
1903 // to stick around - and hold onto the context3d - as long as it is alive.
1904 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1905 renderer_.reset();
1906
1907 // The resource provider can't stay in GL mode or it tries to clean up GL
1908 // stuff, but the context provider is going away on the output surface
1909 // which contradicts being in GL mode.
1910 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1911 resource_provider_->InitializeSoftware();
1912 }
[email protected]50644642013-06-20 13:58:551913 }
[email protected]b5174d712013-08-28 08:10:431914
1915 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271916 return success;
1917}
1918
[email protected]50af3c22013-07-13 03:50:201919void LayerTreeHostImpl::ReleaseGL() {
1920 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1921 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091922 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201923
1924 ReleaseTreeResources();
1925 renderer_.reset();
1926 tile_manager_.reset();
1927 resource_provider_->InitializeSoftware();
1928
1929 bool skip_gl_renderer = true;
1930 CreateAndSetRenderer(
1931 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1932 DCHECK(renderer_);
1933
1934 EnforceZeroBudget(true);
1935 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011936 NULL,
[email protected]4e472a42014-02-11 01:56:371937 GetRendererCapabilities().using_map_image,
1938 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201939 DCHECK(tile_manager_);
1940
[email protected]b5174d712013-08-28 08:10:431941 SetOffscreenContextProvider(NULL);
1942
[email protected]50af3c22013-07-13 03:50:201943 client_->SetNeedsCommitOnImplThread();
1944}
1945
[email protected]64348ea2014-01-29 22:58:261946void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:401947 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271948 return;
[email protected]94f206c12012-08-25 00:09:141949
[email protected]54af03522013-09-05 00:43:281950 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271951 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271952
[email protected]c1bb5af2013-03-13 19:06:271953 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141954
[email protected]fef74fd2014-02-27 06:28:171955 UpdateInnerViewportContainerSize();
[email protected]c1bb5af2013-03-13 19:06:271956 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441957 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141958}
1959
[email protected]d9083762013-03-24 01:36:401960void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1961 if (overdraw_bottom_height == overdraw_bottom_height_)
1962 return;
1963 overdraw_bottom_height_ = overdraw_bottom_height;
1964
[email protected]fef74fd2014-02-27 06:28:171965 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441966 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401967}
1968
[email protected]9f4f6a32013-09-04 21:35:121969void LayerTreeHostImpl::SetOverhangUIResource(
1970 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:261971 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:121972 overhang_ui_resource_id_ = overhang_ui_resource_id;
1973 overhang_ui_resource_size_ = overhang_ui_resource_size;
1974}
1975
[email protected]c1bb5af2013-03-13 19:06:271976void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1977 if (device_scale_factor == device_scale_factor_)
1978 return;
1979 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271980
[email protected]fef74fd2014-02-27 06:28:171981 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441982 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141983}
1984
[email protected]54af03522013-09-05 00:43:281985gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1986 return DeviceViewport().size();
1987}
1988
[email protected]f224cc92013-06-06 23:23:321989gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1990 if (external_viewport_.IsEmpty())
1991 return gfx::Rect(device_viewport_size_);
1992
1993 return external_viewport_;
1994}
1995
[email protected]54af03522013-09-05 00:43:281996gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1997 if (external_clip_.IsEmpty())
1998 return DeviceViewport();
1999
2000 return external_clip_;
2001}
2002
2003const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:322004 return external_transform_;
2005}
2006
[email protected]59adb112013-04-09 04:48:442007void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]fef74fd2014-02-27 06:28:172008 UpdateInnerViewportContainerSize();
[email protected]4e4136d2013-11-29 02:22:442009 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:442010 active_tree_->set_needs_update_draw_properties();
2011 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142012}
2013
[email protected]c1bb5af2013-03-13 19:06:272014bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:042015 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:342016 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:142017}
2018
[email protected]200a9c062013-05-20 04:34:372019void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2020 DCHECK(input_handler_client_ == NULL);
2021 input_handler_client_ = client;
2022}
2023
[email protected]420fdf6e2013-08-26 20:36:382024static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2025 if (LayerImpl* scroll_parent = layer->scroll_parent())
2026 return scroll_parent;
2027 return layer->parent();
2028}
2029
[email protected]f620b0e72013-10-01 21:38:242030LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472031 const gfx::PointF& device_viewport_point,
2032 InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:432033 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:022034 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142035
[email protected]c1bb5af2013-03-13 19:06:272036 // Walk up the hierarchy and look for a scrollable layer.
2037 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382038 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272039 // The content layer can also block attempts to scroll outside the main
2040 // thread.
2041 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2042 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022043 *scroll_on_main_thread = true;
2044 return NULL;
[email protected]94f206c12012-08-25 00:09:142045 }
2046
[email protected]c1bb5af2013-03-13 19:06:272047 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2048 if (!scroll_layer_impl)
2049 continue;
[email protected]94f206c12012-08-25 00:09:142050
[email protected]c1bb5af2013-03-13 19:06:272051 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272052 // If any layer wants to divert the scroll event to the main thread, abort.
2053 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022054 *scroll_on_main_thread = true;
2055 return NULL;
[email protected]94f206c12012-08-25 00:09:142056 }
2057
[email protected]c1bb5af2013-03-13 19:06:272058 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2059 potentially_scrolling_layer_impl = scroll_layer_impl;
2060 }
2061
[email protected]bf1cfd9a2013-09-26 05:43:022062 return potentially_scrolling_layer_impl;
2063}
2064
2065InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
[email protected]47a723f2014-03-05 12:42:492066 const gfx::Point& viewport_point,
2067 InputHandler::ScrollInputType type) {
[email protected]bf1cfd9a2013-09-26 05:43:022068 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2069
2070 if (top_controls_manager_)
2071 top_controls_manager_->ScrollBegin();
2072
2073 DCHECK(!CurrentlyScrollingLayer());
2074 ClearCurrentlyScrollingLayer();
2075
2076 if (!EnsureRenderSurfaceLayerList())
2077 return ScrollIgnored;
2078
[email protected]f620b0e72013-10-01 21:38:242079 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2080 device_scale_factor_);
2081 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2082 device_viewport_point,
2083 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022084 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242085 LayerImpl* potentially_scrolling_layer_impl =
2086 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2087 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022088
2089 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022090 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2091 return ScrollOnMainThread;
2092 }
2093
[email protected]adeda572014-01-31 00:49:472094 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532095 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472096 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2097 ? OuterViewportScrollLayer()
2098 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102099
[email protected]c1bb5af2013-03-13 19:06:272100 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202101 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272102 potentially_scrolling_layer_impl);
2103 should_bubble_scrolls_ = (type != NonBubblingGesture);
2104 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272105 client_->RenewTreePriority();
2106 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2107 return ScrollStarted;
2108 }
2109 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142110}
2111
[email protected]c1bb5af2013-03-13 19:06:272112gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2113 LayerImpl* layer_impl,
2114 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472115 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122116 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272117 // Layers with non-invertible screen space transforms should not have passed
2118 // the scroll hit test in the first place.
2119 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2120 gfx::Transform inverse_screen_space_transform(
2121 gfx::Transform::kSkipInitialization);
2122 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2123 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352124 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2125 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272126 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142127
[email protected]c1bb5af2013-03-13 19:06:272128 gfx::PointF screen_space_point =
2129 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142130
[email protected]c1bb5af2013-03-13 19:06:272131 gfx::Vector2dF screen_space_delta = viewport_delta;
2132 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2133
2134 // First project the scroll start and end points to local layer space to find
2135 // the scroll delta in layer coordinates.
2136 bool start_clipped, end_clipped;
2137 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2138 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212139 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272140 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212141 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272142 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212143 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272144 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212145 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272146
2147 // In general scroll point coordinates should not get clipped.
2148 DCHECK(!start_clipped);
2149 DCHECK(!end_clipped);
2150 if (start_clipped || end_clipped)
2151 return gfx::Vector2dF();
2152
2153 // local_start_point and local_end_point are in content space but we want to
2154 // move them to layer space for scrolling.
2155 float width_scale = 1.f / layer_impl->contents_scale_x();
2156 float height_scale = 1.f / layer_impl->contents_scale_y();
2157 local_start_point.Scale(width_scale, height_scale);
2158 local_end_point.Scale(width_scale, height_scale);
2159
2160 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472161 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272162 layer_impl->ScrollBy(local_end_point - local_start_point);
2163
2164 // Get the end point in the layer's content space so we can apply its
2165 // ScreenSpaceTransform.
2166 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472167 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272168 previous_delta;
2169 gfx::PointF actual_local_content_end_point =
2170 gfx::ScalePoint(actual_local_end_point,
2171 1.f / width_scale,
2172 1.f / height_scale);
2173
2174 // Calculate the applied scroll delta in viewport space coordinates.
2175 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212176 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272177 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212178 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272179 DCHECK(!end_clipped);
2180 if (end_clipped)
2181 return gfx::Vector2dF();
2182 gfx::PointF actual_viewport_end_point =
2183 gfx::ScalePoint(actual_screen_space_end_point,
2184 1.f / scale_from_viewport_to_screen_space);
2185 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142186}
2187
[email protected]c1bb5af2013-03-13 19:06:272188static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122189 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472190 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272191 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472192 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272193}
2194
[email protected]47a723f2014-03-05 12:42:492195bool LayerTreeHostImpl::ScrollBy(const gfx::Point& viewport_point,
[email protected]3244c9132014-01-23 10:39:122196 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272197 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2198 if (!CurrentlyScrollingLayer())
2199 return false;
2200
2201 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532202 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292203 bool did_scroll_x = false;
2204 bool did_scroll_y = false;
[email protected]f42cffb2014-03-08 18:03:252205 bool did_scroll_top_controls = false;
[email protected]adeda572014-01-31 00:49:472206 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2207 // here?
2208 bool consume_by_top_controls =
2209 top_controls_manager_ &&
2210 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2211 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2212 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2213 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062214
[email protected]c1bb5af2013-03-13 19:06:272215 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2216 layer_impl;
2217 layer_impl = layer_impl->parent()) {
2218 if (!layer_impl->scrollable())
2219 continue;
2220
[email protected]adeda572014-01-31 00:49:472221 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532222 // Only allow bubble scrolling when the scroll is in the direction to make
2223 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472224 gfx::Vector2dF applied_delta;
2225 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532226 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472227 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2228 applied_delta = pending_delta - excess_delta;
2229 pending_delta = excess_delta;
2230 // Force updating of vertical adjust values if needed.
[email protected]f42cffb2014-03-08 18:03:252231 if (applied_delta.y() != 0) {
2232 did_scroll_top_controls = true;
[email protected]adeda572014-01-31 00:49:472233 layer_impl->ScrollbarParametersDidChange();
[email protected]f42cffb2014-03-08 18:03:252234 }
[email protected]a2b5ded2013-05-20 21:32:532235 }
2236 // Track root layer deltas for reporting overscroll.
2237 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422238 }
2239
[email protected]c1bb5af2013-03-13 19:06:272240 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272241 // Gesture events need to be transformed from viewport coordinates to local
2242 // layer coordinates so that the scrolling contents exactly follow the
2243 // user's finger. In contrast, wheel events represent a fixed amount of
2244 // scrolling so we can just apply them directly.
2245 if (!wheel_scrolling_) {
2246 float scale_from_viewport_to_screen_space = device_scale_factor_;
2247 applied_delta =
2248 ScrollLayerWithViewportSpaceDelta(layer_impl,
2249 scale_from_viewport_to_screen_space,
2250 viewport_point, pending_delta);
2251 } else {
2252 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142253 }
[email protected]94f206c12012-08-25 00:09:142254
[email protected]c1bb5af2013-03-13 19:06:272255 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292256 float move_threshold = 0.1f;
2257 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2258 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2259 did_scroll_x |= did_move_layer_x;
2260 did_scroll_y |= did_move_layer_y;
2261 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142262 // Scrolls should always bubble between the outer and inner viewports
2263 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2264 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272265 continue;
[email protected]db102612013-12-20 22:04:122266 else
2267 break;
[email protected]94f206c12012-08-25 00:09:142268 }
[email protected]a2b5ded2013-05-20 21:32:532269
[email protected]485a42dc2014-03-26 22:44:342270 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532271 unused_root_delta.Subtract(applied_delta);
[email protected]485a42dc2014-03-26 22:44:342272 const float kOverscrollEpsilon = 0.01f;
2273 if (std::abs(unused_root_delta.x()) < kOverscrollEpsilon)
2274 unused_root_delta.set_x(0.0f);
2275 if (std::abs(unused_root_delta.y()) < kOverscrollEpsilon)
2276 unused_root_delta.set_y(0.0f);
2277 }
[email protected]a2b5ded2013-05-20 21:32:532278
[email protected]c1bb5af2013-03-13 19:06:272279 did_lock_scrolling_layer_ = true;
2280 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202281 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272282 break;
[email protected]94f206c12012-08-25 00:09:142283 }
[email protected]94f206c12012-08-25 00:09:142284
[email protected]c1bb5af2013-03-13 19:06:272285 // If the applied delta is within 45 degrees of the input delta, bail out to
2286 // make it easier to scroll just one layer in one direction without
2287 // affecting any of its parents.
2288 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212289 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272290 applied_delta, pending_delta) < angle_threshold) {
2291 pending_delta = gfx::Vector2d();
2292 break;
[email protected]4a23c374c2012-12-08 08:38:552293 }
[email protected]c1bb5af2013-03-13 19:06:272294
2295 // Allow further movement only on an axis perpendicular to the direction in
2296 // which the layer moved.
2297 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212298 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272299
[email protected]a2b5ded2013-05-20 21:32:532300 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272301 break;
2302 }
2303
[email protected]f42cffb2014-03-08 18:03:252304 bool did_scroll_content = did_scroll_x || did_scroll_y;
2305 if (did_scroll_content) {
[email protected]c1bb5af2013-03-13 19:06:272306 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442307 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272308 client_->RenewTreePriority();
2309 }
[email protected]a2b5ded2013-05-20 21:32:532310
[email protected]2bd503f2013-07-23 05:35:292311 // Scrolling along an axis resets accumulated root overscroll for that axis.
2312 if (did_scroll_x)
2313 accumulated_root_overscroll_.set_x(0);
2314 if (did_scroll_y)
2315 accumulated_root_overscroll_.set_y(0);
2316
[email protected]a2b5ded2013-05-20 21:32:532317 accumulated_root_overscroll_ += unused_root_delta;
[email protected]485a42dc2014-03-26 22:44:342318 bool did_overscroll = !unused_root_delta.IsZero();
[email protected]a2b5ded2013-05-20 21:32:532319 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112320 DidOverscrollParams params;
2321 params.accumulated_overscroll = accumulated_root_overscroll_;
2322 params.latest_overscroll_delta = unused_root_delta;
2323 params.current_fling_velocity = current_fling_velocity_;
2324 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532325 }
2326
[email protected]f42cffb2014-03-08 18:03:252327 return did_scroll_content || did_scroll_top_controls;
[email protected]4a23c374c2012-12-08 08:38:552328}
2329
[email protected]be782f52013-03-23 21:36:142330// This implements scrolling by page as described here:
2331// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2332// for events with WHEEL_PAGESCROLL set.
[email protected]47a723f2014-03-05 12:42:492333bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
[email protected]c28df4c12013-05-22 17:36:492334 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142335 DCHECK(wheel_scrolling_);
2336
2337 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2338 layer_impl;
2339 layer_impl = layer_impl->parent()) {
2340 if (!layer_impl->scrollable())
2341 continue;
2342
[email protected]adeda572014-01-31 00:49:472343 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142344 continue;
2345
[email protected]adeda572014-01-31 00:49:472346 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142347
2348 // These magical values match WebKit and are designed to scroll nearly the
2349 // entire visible content height but leave a bit of overlap.
2350 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492351 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142352 page = -page;
2353
2354 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2355
2356 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2357
2358 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142359 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442360 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142361 client_->RenewTreePriority();
2362 return true;
2363 }
2364
2365 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2366 }
2367
2368 return false;
2369}
2370
[email protected]1960a712013-04-30 17:06:472371void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2372 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2373 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2374 active_tree_->SetRootLayerScrollOffsetDelegate(
2375 root_layer_scroll_offset_delegate_);
2376}
2377
2378void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2379 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2380 client_->SetNeedsCommitOnImplThread();
2381}
2382
[email protected]c1bb5af2013-03-13 19:06:272383void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2384 active_tree_->ClearCurrentlyScrollingLayer();
2385 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532386 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202387 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142388}
2389
[email protected]c1bb5af2013-03-13 19:06:272390void LayerTreeHostImpl::ScrollEnd() {
2391 if (top_controls_manager_)
2392 top_controls_manager_->ScrollEnd();
2393 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052394 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142395}
2396
[email protected]5ff3c9782013-04-29 17:35:122397InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462398 if (!active_tree_->CurrentlyScrollingLayer())
2399 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212400
[email protected]75147f52013-08-02 19:11:462401 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472402 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2403 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462404 ClearCurrentlyScrollingLayer();
2405 return ScrollIgnored;
2406 }
2407
[email protected]db102612013-12-20 22:04:122408 if (!wheel_scrolling_) {
2409 // Allow the fling to lock to the first layer that moves after the initial
2410 // fling |ScrollBy()| event.
2411 did_lock_scrolling_layer_ = false;
2412 should_bubble_scrolls_ = false;
2413 }
[email protected]df0c42342013-10-08 20:52:122414
[email protected]75147f52013-08-02 19:11:462415 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212416}
2417
[email protected]3244c9132014-01-23 10:39:122418void LayerTreeHostImpl::NotifyCurrentFlingVelocity(
2419 const gfx::Vector2dF& velocity) {
[email protected]6e921bd2013-04-29 21:10:202420 current_fling_velocity_ = velocity;
2421}
2422
[email protected]bf1cfd9a2013-09-26 05:43:022423float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472424 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022425 LayerImpl* layer_impl) {
2426 if (!layer_impl)
2427 return std::numeric_limits<float>::max();
2428
2429 gfx::Rect layer_impl_bounds(
2430 layer_impl->content_bounds());
2431
2432 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2433 layer_impl->screen_space_transform(),
2434 layer_impl_bounds);
2435
2436 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2437 device_viewport_point);
2438}
2439
[email protected]47a723f2014-03-05 12:42:492440void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) {
[email protected]bf1cfd9a2013-09-26 05:43:022441 if (!EnsureRenderSurfaceLayerList())
2442 return;
2443
[email protected]f620b0e72013-10-01 21:38:242444 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2445 device_scale_factor_);
2446
2447 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2448 device_viewport_point,
2449 active_tree_->RenderSurfaceLayerList());
2450 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2451 return;
2452
2453 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2454 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2455 scroll_layer_id_when_mouse_over_scrollbar_);
2456
[email protected]5cd9ac12014-02-05 21:48:532457 // The check for a null scroll_layer_impl below was added to see if it will
2458 // eliminate the crashes described in http://crbug.com/326635.
2459 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242460 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532461 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2462 : NULL;
[email protected]f620b0e72013-10-01 21:38:242463 if (animation_controller) {
[email protected]1dc06162014-03-26 22:54:452464 animation_controller->DidMouseMoveOffScrollbar(CurrentFrameTimeTicks());
[email protected]f620b0e72013-10-01 21:38:242465 StartScrollbarAnimation();
2466 }
2467 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2468 }
2469
[email protected]bf1cfd9a2013-09-26 05:43:022470 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242471 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2472 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022473 &scroll_on_main_thread);
2474 if (scroll_on_main_thread || !scroll_layer_impl)
2475 return;
2476
2477 ScrollbarAnimationController* animation_controller =
2478 scroll_layer_impl->scrollbar_animation_controller();
2479 if (!animation_controller)
2480 return;
2481
[email protected]adeda572014-01-31 00:49:472482 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2483 float distance_to_scrollbar = std::numeric_limits<float>::max();
2484 for (LayerImpl::ScrollbarSet::iterator it =
2485 scroll_layer_impl->scrollbars()->begin();
2486 it != scroll_layer_impl->scrollbars()->end();
2487 ++it)
2488 distance_to_scrollbar =
2489 std::min(distance_to_scrollbar,
2490 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022491
2492 bool should_animate = animation_controller->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452493 CurrentFrameTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202494 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022495 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022496}
2497
[email protected]f620b0e72013-10-01 21:38:242498bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472499 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242500 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2501 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2502 layer_impl = active_tree_->LayerById(scroll_layer_id);
2503 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2504 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202505 bool should_animate =
2506 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452507 CurrentFrameTimeTicks(), 0);
[email protected]534236b2013-10-25 21:19:202508 if (should_animate)
2509 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242510 } else {
2511 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2512 }
2513
2514 return true;
2515 }
2516
2517 return false;
2518}
2519
[email protected]c1bb5af2013-03-13 19:06:272520void LayerTreeHostImpl::PinchGestureBegin() {
2521 pinch_gesture_active_ = true;
2522 previous_pinch_anchor_ = gfx::Point();
2523 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132524 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472525 if (active_tree_->OuterViewportScrollLayer()) {
2526 active_tree_->SetCurrentlyScrollingLayer(
2527 active_tree_->OuterViewportScrollLayer());
2528 } else {
2529 active_tree_->SetCurrentlyScrollingLayer(
2530 active_tree_->InnerViewportScrollLayer());
2531 }
[email protected]a9bda5e2013-10-25 18:43:372532 if (top_controls_manager_)
2533 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142534}
2535
[email protected]c1bb5af2013-03-13 19:06:272536void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
[email protected]47a723f2014-03-05 12:42:492537 const gfx::Point& anchor) {
[email protected]b93480862014-02-20 13:07:032538 if (!InnerViewportScrollLayer())
2539 return;
[email protected]d3afa112012-12-08 06:24:282540
[email protected]b93480862014-02-20 13:07:032541 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282542
[email protected]c1bb5af2013-03-13 19:06:272543 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2544 // position over the course of the magnify.
2545 float page_scale_delta = active_tree_->page_scale_delta();
2546 gfx::PointF previous_scale_anchor =
2547 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2548 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2549 page_scale_delta = active_tree_->page_scale_delta();
2550 gfx::PointF new_scale_anchor =
2551 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2552 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2553
2554 previous_pinch_anchor_ = anchor;
2555
2556 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472557 // If clamping the inner viewport scroll offset causes a change, it should
2558 // be accounted for from the intended move.
2559 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272560
[email protected]adeda572014-01-31 00:49:472561 // We manually manage the bubbling behaviour here as it is different to that
2562 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2563 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2564 // 2) we don't want the directional limitations on the unused parts that
2565 // ScrollBy() implements, and
2566 // 3) pinching should not engage the top controls manager.
2567 gfx::Vector2dF unused = OuterViewportScrollLayer()
2568 ? OuterViewportScrollLayer()->ScrollBy(move)
2569 : move;
2570
2571 if (!unused.IsZero()) {
2572 InnerViewportScrollLayer()->ScrollBy(unused);
2573 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2574 }
[email protected]c1bb5af2013-03-13 19:06:272575
[email protected]c1bb5af2013-03-13 19:06:272576 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442577 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272578 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282579}
2580
[email protected]c1bb5af2013-03-13 19:06:272581void LayerTreeHostImpl::PinchGestureEnd() {
2582 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132583 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2584 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2585 ClearCurrentlyScrollingLayer();
2586 }
[email protected]a9bda5e2013-10-25 18:43:372587 if (top_controls_manager_)
2588 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272589 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142590}
2591
[email protected]c1bb5af2013-03-13 19:06:272592static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2593 LayerImpl* layer_impl) {
2594 if (!layer_impl)
2595 return;
[email protected]94f206c12012-08-25 00:09:142596
[email protected]c1bb5af2013-03-13 19:06:272597 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472598 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272599 if (!scroll_delta.IsZero()) {
2600 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392601 scroll.layer_id = layer_impl->id();
2602 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272603 scroll_info->scrolls.push_back(scroll);
2604 layer_impl->SetSentScrollDelta(scroll_delta);
2605 }
[email protected]94f206c12012-08-25 00:09:142606
[email protected]c1bb5af2013-03-13 19:06:272607 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2608 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142609}
2610
[email protected]c1bb5af2013-03-13 19:06:272611scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2612 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302613
[email protected]c1bb5af2013-03-13 19:06:272614 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392615 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2616 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302617
[email protected]c1bb5af2013-03-13 19:06:272618 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302619}
2620
[email protected]c1bb5af2013-03-13 19:06:272621void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282622 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102623}
2624
[email protected]adeda572014-01-31 00:49:472625void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2626 DCHECK(InnerViewportScrollLayer());
2627 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2628 ? OuterViewportScrollLayer()
2629 : InnerViewportScrollLayer();
2630
2631 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2632
2633 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2634 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2635}
2636
[email protected]27152642014-03-11 20:42:002637void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks monotonic_time) {
[email protected]adeda572014-01-31 00:49:472638 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272639 return;
2640
[email protected]27152642014-03-11 20:42:002641 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2642 // time.
2643 double monotonic_time_for_cc_animations =
2644 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472645 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272646
[email protected]34e1b512013-09-26 17:32:332647 if (!page_scale_animation_->IsAnimationStarted())
[email protected]27152642014-03-11 20:42:002648 page_scale_animation_->StartAnimation(monotonic_time_for_cc_animations);
[email protected]34e1b512013-09-26 17:32:332649
[email protected]27152642014-03-11 20:42:002650 active_tree_->SetPageScaleDelta(page_scale_animation_->PageScaleFactorAtTime(
2651 monotonic_time_for_cc_animations) /
2652 active_tree_->page_scale_factor());
2653 gfx::Vector2dF next_scroll = page_scale_animation_->ScrollOffsetAtTime(
2654 monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272655
[email protected]adeda572014-01-31 00:49:472656 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442657 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272658
[email protected]27152642014-03-11 20:42:002659 if (page_scale_animation_->IsAnimationCompleteAtTime(
2660 monotonic_time_for_cc_animations)) {
[email protected]c1bb5af2013-03-13 19:06:272661 page_scale_animation_.reset();
2662 client_->SetNeedsCommitOnImplThread();
2663 client_->RenewTreePriority();
2664 }
[email protected]829ad972013-01-28 23:36:102665}
2666
[email protected]ffb2720f2013-03-15 19:18:372667void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082668 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372669 return;
2670 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472671 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172672 return;
[email protected]5f7d9112014-01-17 21:31:422673 if (scroll.IsZero()) {
2674 // This may happen on the first animation step. Force redraw otherwise
2675 // the animation would stop because of no new frames.
2676 SetNeedsRedraw();
2677 } else {
[email protected]adeda572014-01-31 00:49:472678 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422679 scroll, 1.f / active_tree_->total_page_scale_factor()));
2680 }
[email protected]ffb2720f2013-03-15 19:18:372681}
2682
[email protected]27152642014-03-11 20:42:002683void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time) {
[email protected]8e0176d2013-03-21 03:14:522684 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362685 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272686 !active_tree_->root_layer())
2687 return;
2688
2689 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2690
[email protected]27152642014-03-11 20:42:002691 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2692 // time.
2693 double monotonic_time_for_cc_animations =
2694 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]c1bb5af2013-03-13 19:06:272695 AnimationRegistrar::AnimationControllerMap copy =
2696 animation_registrar_->active_animation_controllers();
2697 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2698 iter != copy.end();
2699 ++iter)
[email protected]27152642014-03-11 20:42:002700 (*iter).second->Animate(monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272701
[email protected]4e4136d2013-11-29 02:22:442702 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082703}
2704
[email protected]3d9f7432013-04-06 00:35:182705void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522706 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362707 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272708 !active_tree_->root_layer())
2709 return;
2710
2711 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2712 scoped_ptr<AnimationEventsVector> events =
2713 make_scoped_ptr(new AnimationEventsVector);
2714 AnimationRegistrar::AnimationControllerMap copy =
2715 animation_registrar_->active_animation_controllers();
2716 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2717 iter != copy.end();
2718 ++iter)
[email protected]3d9f7432013-04-06 00:35:182719 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272720
2721 if (!events->empty()) {
[email protected]85b57502014-03-11 15:37:482722 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass());
[email protected]c1bb5af2013-03-13 19:06:272723 }
[email protected]131a0c22013-02-12 18:31:082724}
2725
[email protected]c1bb5af2013-03-13 19:06:272726base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2727 return base::TimeDelta::FromSeconds(1);
2728}
2729
[email protected]50644642013-06-20 13:58:552730void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352731 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502732 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352733 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552734 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352735 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552736 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352737 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552738 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352739}
2740
[email protected]b5174d712013-08-28 08:10:432741void LayerTreeHostImpl::SetOffscreenContextProvider(
2742 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2743 if (!offscreen_context_provider.get()) {
2744 offscreen_context_provider_ = NULL;
2745 return;
2746 }
2747
2748 if (!offscreen_context_provider->BindToCurrentThread()) {
2749 offscreen_context_provider_ = NULL;
2750 return;
2751 }
2752
2753 offscreen_context_provider_ = offscreen_context_provider;
2754}
2755
[email protected]c1bb5af2013-03-13 19:06:272756std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2757 std::string str;
2758 if (active_tree_->root_layer()) {
2759 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2760 base::JSONWriter::WriteWithOptions(
2761 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2762 }
2763 return str;
2764}
2765
[email protected]c1bb5af2013-03-13 19:06:272766int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362767 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272768}
2769
[email protected]c1bb5af2013-03-13 19:06:272770void LayerTreeHostImpl::SendManagedMemoryStats(
2771 size_t memory_visible_bytes,
2772 size_t memory_visible_and_nearby_bytes,
2773 size_t memory_use_bytes) {
2774 if (!renderer_)
2775 return;
2776
2777 // Round the numbers being sent up to the next 8MB, to throttle the rate
2778 // at which we spam the GPU process.
2779 static const size_t rounding_step = 8 * 1024 * 1024;
2780 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2781 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2782 rounding_step);
2783 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2784 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2785 last_sent_memory_visible_and_nearby_bytes_ ==
2786 memory_visible_and_nearby_bytes &&
2787 last_sent_memory_use_bytes_ == memory_use_bytes) {
2788 return;
2789 }
2790 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2791 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2792 last_sent_memory_use_bytes_ = memory_use_bytes;
2793
2794 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2795 last_sent_memory_visible_and_nearby_bytes_,
2796 last_sent_memory_use_bytes_);
2797}
2798
2799void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2800 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2801}
2802
2803void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2804 base::TimeTicks time) {
2805 if (!layer)
2806 return;
2807
2808 ScrollbarAnimationController* scrollbar_controller =
2809 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352810 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202811 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422812 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2813 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442814 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202815 }
[email protected]c1bb5af2013-03-13 19:06:272816
2817 for (size_t i = 0; i < layer->children().size(); ++i)
2818 AnimateScrollbarsRecursive(layer->children()[i], time);
2819}
2820
[email protected]21c9dee72013-06-15 01:20:052821void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202822 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]1dc06162014-03-26 22:54:452823 StartScrollbarAnimationRecursive(RootLayer(), CurrentFrameTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202824}
2825
2826void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2827 base::TimeTicks time) {
2828 if (!layer)
2829 return;
2830
2831 ScrollbarAnimationController* scrollbar_controller =
2832 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352833 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2834 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202835 if (delay > base::TimeDelta())
2836 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352837 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442838 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202839 }
2840
2841 for (size_t i = 0; i < layer->children().size(); ++i)
2842 StartScrollbarAnimationRecursive(layer->children()[i], time);
2843}
2844
[email protected]c1bb5af2013-03-13 19:06:272845void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2846 if (!tile_manager_)
2847 return;
2848
[email protected]1bcced22013-09-24 13:51:192849 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272850 return;
[email protected]1bcced22013-09-24 13:51:192851 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082852 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272853}
2854
[email protected]1dc06162014-03-26 22:54:452855void LayerTreeHostImpl::UpdateCurrentFrameTime() {
2856 DCHECK(current_frame_timeticks_.is_null());
2857 current_frame_timeticks_ = gfx::FrameTime::Now();
2858}
2859
[email protected]8347d692013-05-17 23:22:382860void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552861 current_frame_timeticks_ = base::TimeTicks();
[email protected]c1bb5af2013-03-13 19:06:272862}
2863
[email protected]fb7425a2013-04-22 16:28:552864base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
[email protected]1dc06162014-03-26 22:54:452865 // Try to use the current frame time to keep animations non-jittery. But if
2866 // we're not in a frame (because this is during an input event or a delayed
2867 // task), fall back to physical time. This should still be monotonic.
2868 if (!current_frame_timeticks_.is_null())
2869 return current_frame_timeticks_;
[email protected]de2cf8c2013-10-25 19:46:462870 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052871}
2872
[email protected]34806722013-08-09 23:51:212873scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2874 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272875 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222876 if (this->pending_tree_)
2877 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272878 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212879 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272880 if (tile_manager_)
2881 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2882 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222883 if (pending_tree_)
2884 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212885 if (frame)
2886 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222887 return state.PassAs<base::Value>();
2888}
2889
2890scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222891 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2892 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472893 if (tile_manager_)
2894 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272895 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082896}
2897
[email protected]6e7fdeb2013-07-09 14:28:382898void LayerTreeHostImpl::SetDebugState(
2899 const LayerTreeDebugState& new_debug_state) {
2900 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2901 return;
2902 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272903 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242904
[email protected]6e7fdeb2013-07-09 14:28:382905 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122906 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382907 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022908}
2909
[email protected]741fba422013-09-20 03:34:142910void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2911 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572912 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572913
[email protected]efa48412013-09-05 15:30:162914 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142915 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162916 case UIResourceBitmap::CLAMP_TO_EDGE:
2917 wrap_mode = GL_CLAMP_TO_EDGE;
2918 break;
2919 case UIResourceBitmap::REPEAT:
2920 wrap_mode = GL_REPEAT;
2921 break;
2922 }
2923
[email protected]c9280762013-08-01 06:28:572924 // Allow for multiple creation requests with the same UIResourceId. The
2925 // previous resource is simply deleted.
2926 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2927 if (id)
2928 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122929
2930 ResourceFormat format = resource_provider_->best_texture_format();
2931 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2932 format = ETC1;
[email protected]c9280762013-08-01 06:28:572933 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142934 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162935 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142936 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122937 format);
[email protected]c9280762013-08-01 06:28:572938
[email protected]efbdb3a2013-10-04 00:35:132939 UIResourceData data;
2940 data.resource_id = id;
2941 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512942 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132943
2944 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032945
2946 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572947 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032948 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142949 gfx::Rect(bitmap.GetSize()),
2950 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572951 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482952 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572953}
2954
2955void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2956 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2957 if (id) {
2958 resource_provider_->DeleteResource(id);
2959 ui_resource_map_.erase(uid);
2960 }
[email protected]127bdc1a2013-09-11 01:44:482961 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572962}
2963
[email protected]127bdc1a2013-09-11 01:44:482964void LayerTreeHostImpl::EvictAllUIResources() {
2965 if (ui_resource_map_.empty())
2966 return;
2967
[email protected]5f4dc4f2013-09-05 14:58:212968 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2969 iter != ui_resource_map_.end();
2970 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482971 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132972 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212973 }
2974 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482975
2976 client_->SetNeedsCommitOnImplThread();
2977 client_->OnCanDrawStateChanged(CanDraw());
2978 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212979}
2980
[email protected]c9280762013-08-01 06:28:572981ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2982 UIResourceId uid) const {
2983 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2984 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132985 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572986 return 0;
2987}
2988
[email protected]709c9542013-10-26 01:43:512989bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2990 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2991 DCHECK(iter != ui_resource_map_.end());
2992 return iter->second.opaque;
2993}
2994
[email protected]127bdc1a2013-09-11 01:44:482995bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2996 return !evicted_ui_resources_.empty();
2997}
2998
2999void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
3000 std::set<UIResourceId>::iterator found_in_evicted =
3001 evicted_ui_resources_.find(uid);
3002 if (found_in_evicted == evicted_ui_resources_.end())
3003 return;
3004 evicted_ui_resources_.erase(found_in_evicted);
3005 if (evicted_ui_resources_.empty())
3006 client_->OnCanDrawStateChanged(CanDraw());
3007}
3008
[email protected]5e5648a2013-11-18 00:39:333009void LayerTreeHostImpl::ScheduleMicroBenchmark(
3010 scoped_ptr<MicroBenchmarkImpl> benchmark) {
3011 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
3012}
3013
[email protected]6be422b2013-12-08 06:47:313014void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3015 swap_promise_monitor_.insert(monitor);
3016}
3017
3018void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3019 swap_promise_monitor_.erase(monitor);
3020}
3021
3022void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3023 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3024 for (; it != swap_promise_monitor_.end(); it++)
3025 (*it)->OnSetNeedsRedrawOnImpl();
3026}
3027
[email protected]d3143c732012-10-05 19:17:593028} // namespace cc