blob: e2be690f76e688ac5939dd2dc390ee1e41bcb4e0 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
[email protected]b6eb8e332013-09-10 00:51:018#include <limits>
[email protected]ac7c7f52012-11-08 06:26:509
[email protected]ad5d1422012-10-19 13:40:2910#include "base/basictypes.h"
[email protected]34806722013-08-09 23:51:2111#include "base/containers/hash_tables.h"
[email protected]4a23c374c2012-12-08 08:38:5512#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1413#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3414#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4815#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0916#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4017#include "cc/animation/timing_function.h"
[email protected]6be422b2013-12-08 06:47:3118#include "cc/base/latency_info_swap_promise_monitor.h"
[email protected]681ccff2013-03-18 06:13:5219#include "cc/base/math_util.h"
20#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3321#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2722#include "cc/debug/debug_rect_history.h"
[email protected]dbe759a2013-12-02 19:23:0223#include "cc/debug/devtools_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2724#include "cc/debug/frame_rate_counter.h"
25#include "cc/debug/overdraw_metrics.h"
26#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4327#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2228#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2129#include "cc/input/page_scale_animation.h"
30#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/append_quads_data.h"
32#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2833#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5234#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1735#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2836#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0237#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3038#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4539#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3040#include "cc/output/delegating_renderer.h"
41#include "cc/output/gl_renderer.h"
42#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5643#include "cc/quads/render_pass_draw_quad.h"
44#include "cc/quads/shared_quad_state.h"
45#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1246#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4047#include "cc/resources/memory_history.h"
48#include "cc/resources/picture_layer_tiling.h"
49#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1150#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5751#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3152#include "cc/scheduler/delay_based_time_source.h"
[email protected]556fd292013-03-18 08:03:0453#include "cc/trees/damage_tracker.h"
54#include "cc/trees/layer_tree_host.h"
55#include "cc/trees/layer_tree_host_common.h"
56#include "cc/trees/layer_tree_impl.h"
[email protected]34ba1ffb2014-03-05 06:55:0357#include "cc/trees/occlusion_tracker.h"
[email protected]556fd292013-03-18 08:03:0458#include "cc/trees/quad_culler.h"
59#include "cc/trees/single_thread_proxy.h"
60#include "cc/trees/tree_synchronizer.h"
[email protected]8d7aa012013-11-23 21:19:1261#include "gpu/GLES2/gl2extchromium.h"
[email protected]de2cf8c2013-10-25 19:46:4662#include "ui/gfx/frame_time.h"
[email protected]d455d552012-11-02 00:19:0663#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1364#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1465
[email protected]94f206c12012-08-25 00:09:1466namespace {
67
[email protected]c1bb5af2013-03-13 19:06:2768void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
69 if (visible) {
70 TRACE_EVENT_ASYNC_BEGIN1("webkit",
71 "LayerTreeHostImpl::SetVisible",
72 id,
73 "LayerTreeHostImpl",
74 id);
75 return;
76 }
[email protected]94f206c12012-08-25 00:09:1477
[email protected]c1bb5af2013-03-13 19:06:2778 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1479}
80
[email protected]b6eb8e332013-09-10 00:51:0181size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
[email protected]b4664142013-11-08 00:50:3182 // Software compositing should not use this value in production. Just use a
83 // default value when testing uploads with the software compositor.
84 if (!context_provider)
[email protected]b6eb8e332013-09-10 00:51:0185 return std::numeric_limits<size_t>::max();
[email protected]b4664142013-11-08 00:50:3186
87 // We want to make sure the default transfer buffer size is equal to the
88 // amount of data that can be uploaded by the compositor to avoid stalling
89 // the pipeline.
90 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
91 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
92 // Assuming a two frame deep pipeline between CPU and GPU and we are
93 // drawing 60 frames per second which would require us to draw one
94 // frame in 16 milliseconds.
95 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
96 return std::min(
97 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
98 kMaxTransferBufferUsageBytes);
99}
100
101size_t GetMaxRasterTasksUsageBytes(cc::ContextProvider* context_provider) {
102 // Transfer-buffer/raster-tasks limits are different but related. We make
103 // equal here, as this is ideal when using transfer buffers. When not using
104 // transfer buffers we should still limit raster to something similar, to
105 // preserve caching behavior (and limit memory waste when priorities change).
106 return GetMaxTransferBufferUsageBytes(context_provider);
[email protected]b6eb8e332013-09-10 00:51:01107}
108
[email protected]00d92d32014-01-23 09:46:50109unsigned GetMapImageTextureTarget(cc::ContextProvider* context_provider) {
[email protected]8d7aa012013-11-23 21:19:12110 if (!context_provider)
111 return GL_TEXTURE_2D;
112
113 // TODO(reveman): Determine if GL_TEXTURE_EXTERNAL_OES works well on
114 // Android before we enable this. crbug.com/322780
115#if !defined(OS_ANDROID)
[email protected]900db4412014-01-12 02:19:01116 if (context_provider->ContextCapabilities().gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12117 return GL_TEXTURE_EXTERNAL_OES;
[email protected]900db4412014-01-12 02:19:01118 if (context_provider->ContextCapabilities().gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12119 return GL_TEXTURE_RECTANGLE_ARB;
120#endif
121
122 return GL_TEXTURE_2D;
123}
124
[email protected]c1bb5af2013-03-13 19:06:27125} // namespace
[email protected]94f206c12012-08-25 00:09:14126
[email protected]9c88e562012-09-14 22:21:30127namespace cc {
[email protected]94f206c12012-08-25 00:09:14128
[email protected]96baf3e2012-10-22 23:09:55129class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27130 public:
131 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
132 LayerTreeHostImpl* layer_tree_host_impl,
133 scoped_refptr<DelayBasedTimeSource> time_source) {
134 return make_scoped_ptr(
135 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
136 time_source));
137 }
138 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26139 time_source_->SetClient(NULL);
140 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27141 }
142
[email protected]6d0e69d2013-03-20 14:53:26143 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27144 // In single threaded mode we attempt to simulate changing the current
145 // thread by maintaining a fake thread id. When we switch from one
146 // thread to another, we construct DebugScopedSetXXXThread objects that
147 // update the thread id. This lets DCHECKS that ensure we're on the
148 // right thread to work correctly in single threaded mode. The problem
149 // here is that the timer tasks are run via the message loop, and when
150 // they run, we've had no chance to construct a DebugScopedSetXXXThread
151 // object. The result is that we report that we're running on the main
152 // thread. In multi-threaded mode, this timer is run on the compositor
153 // thread, so to keep this consistent in single-threaded mode, we'll
154 // construct a DebugScopedSetImplThread object. There is no need to do
155 // this in multi-threaded mode since the real thread id's will be
156 // correct. In fact, setting fake thread id's interferes with the real
157 // thread id's and causes breakage.
158 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
159 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
160 set_impl_thread.reset(
161 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14162 }
163
[email protected]94bf75c2013-06-12 13:20:04164 // TODO(enne): This should probably happen post-animate.
165 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38166 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
167 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04168 }
169
[email protected]fb7425a2013-04-22 16:28:55170 layer_tree_host_impl_->Animate(
171 layer_tree_host_impl_->CurrentFrameTimeTicks(),
172 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18173 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
174 bool start_ready_animations = true;
175 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38176 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27177 }
[email protected]373974232013-01-10 22:20:50178
[email protected]c1bb5af2013-03-13 19:06:27179 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26180 if (active != time_source_->Active())
181 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27182 }
[email protected]94f206c12012-08-25 00:09:14183
[email protected]d9fce6722013-08-30 01:10:01184 bool Active() const { return time_source_->Active(); }
185
[email protected]c1bb5af2013-03-13 19:06:27186 private:
187 LayerTreeHostImplTimeSourceAdapter(
188 LayerTreeHostImpl* layer_tree_host_impl,
189 scoped_refptr<DelayBasedTimeSource> time_source)
190 : layer_tree_host_impl_(layer_tree_host_impl),
191 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26192 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27193 }
[email protected]94f206c12012-08-25 00:09:14194
[email protected]c1bb5af2013-03-13 19:06:27195 LayerTreeHostImpl* layer_tree_host_impl_;
196 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14197
[email protected]c1bb5af2013-03-13 19:06:27198 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14199};
200
[email protected]96baf3e2012-10-22 23:09:55201LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20202 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27203
204LayerTreeHostImpl::FrameData::~FrameData() {}
205
206scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
207 const LayerTreeSettings& settings,
208 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43209 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57210 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02211 SharedBitmapManager* manager,
212 int id) {
[email protected]a7f35682013-10-22 23:05:57213 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02214 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10215}
216
[email protected]372bad5f2013-03-21 16:38:43217LayerTreeHostImpl::LayerTreeHostImpl(
218 const LayerTreeSettings& settings,
219 LayerTreeHostImplClient* client,
220 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57221 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02222 SharedBitmapManager* manager,
223 int id)
[email protected]c1bb5af2013-03-13 19:06:27224 : client_(client),
225 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37226 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27227 did_lock_scrolling_layer_(false),
228 should_bubble_scrolls_(false),
229 wheel_scrolling_(false),
[email protected]f620b0e72013-10-01 21:38:24230 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08231 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47232 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27233 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27234 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07235 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34236 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29237 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07238 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27239 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13240 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36241 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29242 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27243 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02244 debug_rect_history_(DebugRectHistory::Create()),
[email protected]ea468c6c2013-09-10 08:25:11245 texture_mailbox_deleter_(new TextureMailboxDeleter),
[email protected]d7626ffd2013-03-29 00:17:42246 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27247 last_sent_memory_visible_bytes_(0),
248 last_sent_memory_visible_and_nearby_bytes_(0),
249 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55250 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32251 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12252 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32253 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28254 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17255 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43256 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19257 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33258 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57259 need_to_update_visible_tiles_before_draw_(false),
[email protected]ce2e8112013-11-28 07:44:36260#ifndef NDEBUG
261 did_lose_called_(false),
262#endif
[email protected]dbe759a2013-12-02 19:23:02263 shared_bitmap_manager_(manager),
264 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27265 DCHECK(proxy_->IsImplThread());
266 DidVisibilityChange(this, visible_);
267
[email protected]8e0176d2013-03-21 03:14:52268 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27269
[email protected]8e0176d2013-03-21 03:14:52270 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27271 top_controls_manager_ =
272 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52273 settings.top_controls_height,
274 settings.top_controls_show_threshold,
275 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27276 }
277
[email protected]8e0176d2013-03-21 03:14:52278 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27279
280 // LTHI always has an active tree.
281 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51282 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
283 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10284}
285
[email protected]c1bb5af2013-03-13 19:06:27286LayerTreeHostImpl::~LayerTreeHostImpl() {
287 DCHECK(proxy_->IsImplThread());
288 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51289 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
290 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27291
[email protected]200a9c062013-05-20 04:34:37292 if (input_handler_client_) {
293 input_handler_client_->WillShutdown();
294 input_handler_client_ = NULL;
295 }
296
[email protected]75deb742013-06-24 20:19:18297 // The layer trees must be destroyed before the layer tree host. We've
298 // made a contract with our animation controllers that the registrar
299 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40300 if (recycle_tree_)
301 recycle_tree_->Shutdown();
302 if (pending_tree_)
303 pending_tree_->Shutdown();
304 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18305 recycle_tree_.reset();
306 pending_tree_.reset();
307 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14308}
309
[email protected]097a0cbf2013-12-12 17:04:58310void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
311 // If the begin frame data was handled, then scroll and scale set was applied
312 // by the main thread, so the active tree needs to be updated as if these sent
313 // values were applied and committed.
314 if (did_handle) {
315 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
316 active_tree_->ResetContentsTexturesPurged();
317 }
318}
319
[email protected]c1bb5af2013-03-13 19:06:27320void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29321
[email protected]c1bb5af2013-03-13 19:06:27322void LayerTreeHostImpl::CommitComplete() {
323 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08324
[email protected]8e0176d2013-03-21 03:14:52325 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25326 // Impl-side painting needs an update immediately post-commit to have the
327 // opportunity to create tilings. Other paths can call UpdateDrawProperties
328 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50329 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27330 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04331 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26332 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08333 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38334 NotifyReadyToActivate();
335 else
336 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27337 } else {
338 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01339 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
340 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27341 }
[email protected]3ba4cae2013-01-16 03:58:38342
[email protected]c1bb5af2013-03-13 19:06:27343 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33344
345 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14346}
347
[email protected]6133cc232013-07-30 18:47:07348bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27349 // Note: If you are changing this function or any other function that might
350 // affect the result of CanDraw, make sure to call
351 // client_->OnCanDrawStateChanged in the proper places and update the
352 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14353
[email protected]6133cc232013-07-30 18:47:07354 if (!renderer_) {
355 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
356 TRACE_EVENT_SCOPE_THREAD);
357 return false;
358 }
359
360 // Must have an OutputSurface if |renderer_| is not NULL.
361 DCHECK(output_surface_);
362
363 // TODO(boliu): Make draws without root_layer work and move this below
364 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27365 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42366 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
367 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22368 return false;
[email protected]c1bb5af2013-03-13 19:06:27369 }
[email protected]6133cc232013-07-30 18:47:07370
371 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
372 return true;
373
[email protected]54af03522013-09-05 00:43:28374 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42375 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
376 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27377 return false;
378 }
379 if (active_tree_->ViewportSizeInvalid()) {
380 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42381 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
382 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27383 return false;
384 }
[email protected]c1bb5af2013-03-13 19:06:27385 if (active_tree_->ContentsTexturesPurged()) {
386 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42387 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
388 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27389 return false;
390 }
[email protected]127bdc1a2013-09-11 01:44:48391 if (EvictedUIResourcesExist()) {
392 TRACE_EVENT_INSTANT0(
393 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
394 TRACE_EVENT_SCOPE_THREAD);
395 return false;
396 }
[email protected]c1bb5af2013-03-13 19:06:27397 return true;
[email protected]2f1acc262012-11-16 21:42:22398}
399
[email protected]c1bb5af2013-03-13 19:06:27400void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
401 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37402 if (input_handler_client_)
403 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27404 AnimatePageScale(monotonic_time);
405 AnimateLayers(monotonic_time, wall_clock_time);
406 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37407 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14408}
409
[email protected]c1bb5af2013-03-13 19:06:27410void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26411 if (!tile_manager_)
412 return;
[email protected]c48536a52013-09-14 00:02:08413 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26414 return;
[email protected]54af03522013-09-05 00:43:28415
[email protected]c48536a52013-09-14 00:02:08416 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19417 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27418
419 size_t memory_required_bytes;
420 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03421 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27422 size_t memory_used_bytes;
423 tile_manager_->GetMemoryStats(&memory_required_bytes,
424 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03425 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27426 &memory_used_bytes);
427 SendManagedMemoryStats(memory_required_bytes,
428 memory_nice_to_have_bytes,
429 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57430
431 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15432}
433
[email protected]243e4f12014-02-05 09:18:42434void LayerTreeHostImpl::StartPageScaleAnimation(
435 const gfx::Vector2d& target_offset,
436 bool anchor_point,
437 float page_scale,
438 base::TimeDelta duration) {
[email protected]adeda572014-01-31 00:49:47439 if (!InnerViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:27440 return;
441
[email protected]adeda572014-01-31 00:49:47442 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27443 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28444 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27445
[email protected]85d136f782013-04-26 22:04:40446 // Easing constants experimentally determined.
447 scoped_ptr<TimingFunction> timing_function =
448 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
449
[email protected]c1bb5af2013-03-13 19:06:27450 page_scale_animation_ =
451 PageScaleAnimation::Create(scroll_total,
452 active_tree_->total_page_scale_factor(),
453 viewport_size,
454 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40455 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27456
457 if (anchor_point) {
458 gfx::Vector2dF anchor(target_offset);
459 page_scale_animation_->ZoomWithAnchor(anchor,
460 page_scale,
461 duration.InSecondsF());
462 } else {
463 gfx::Vector2dF scaled_target_offset = target_offset;
464 page_scale_animation_->ZoomTo(scaled_target_offset,
465 page_scale,
466 duration.InSecondsF());
467 }
468
[email protected]4e4136d2013-11-29 02:22:44469 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27470 client_->SetNeedsCommitOnImplThread();
471 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15472}
473
[email protected]c1bb5af2013-03-13 19:06:27474void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44475 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15476}
477
[email protected]47a723f2014-03-05 12:42:49478bool LayerTreeHostImpl::HaveTouchEventHandlersAt(
479 const gfx::Point& viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07480 if (!settings_.touch_hit_testing)
481 return true;
[email protected]c1bb5af2013-03-13 19:06:27482 if (!EnsureRenderSurfaceLayerList())
483 return false;
[email protected]f57bbc02012-11-21 07:02:15484
[email protected]c1bb5af2013-03-13 19:06:27485 gfx::PointF device_viewport_point =
486 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15487
[email protected]c974d5d2013-10-24 01:02:48488 LayerImpl* layer_impl =
489 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
490 device_viewport_point,
491 active_tree_->RenderSurfaceLayerList());
492 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27493}
494
[email protected]6be422b2013-12-08 06:47:31495scoped_ptr<SwapPromiseMonitor>
496LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
497 ui::LatencyInfo* latency) {
498 return scoped_ptr<SwapPromiseMonitor>(
499 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08500}
501
[email protected]c1bb5af2013-03-13 19:06:27502void LayerTreeHostImpl::TrackDamageForAllSurfaces(
503 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28504 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27505 // For now, we use damage tracking to compute a global scissor. To do this, we
506 // must compute all damage tracking before drawing anything, so that we know
507 // the root damage rect. The root damage rect is then used to scissor each
508 // surface.
509
510 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06511 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27512 --surface_index) {
513 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
514 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
515 DCHECK(render_surface);
516 render_surface->damage_tracker()->UpdateDamageTrackingState(
517 render_surface->layer_list(),
518 render_surface_layer->id(),
519 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
520 render_surface->content_rect(),
521 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48522 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27523 }
524}
525
[email protected]34806722013-08-09 23:51:21526scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
527 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
528 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
529 value->SetBoolean("has_no_damage", has_no_damage);
530
531 // Quad data can be quite large, so only dump render passes if we select
532 // cc.debug.quads.
533 bool quads_enabled;
534 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
535 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
536 if (quads_enabled) {
537 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
538 for (size_t i = 0; i < render_passes.size(); ++i)
539 render_pass_list->Append(render_passes[i]->AsValue().release());
540 value->Set("render_passes", render_pass_list.release());
541 }
542 return value.PassAs<base::Value>();
543}
544
[email protected]c1bb5af2013-03-13 19:06:27545void LayerTreeHostImpl::FrameData::AppendRenderPass(
546 scoped_ptr<RenderPass> render_pass) {
547 render_passes_by_id[render_pass->id] = render_pass.get();
548 render_passes.push_back(render_pass.Pass());
549}
550
[email protected]ffbb2212013-06-02 23:47:59551static DrawMode GetDrawMode(OutputSurface* output_surface) {
552 if (output_surface->ForcedDrawToSoftwareDevice()) {
553 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09554 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59555 return DRAW_MODE_HARDWARE;
556 } else {
[email protected]a7f35682013-10-22 23:05:57557 DCHECK_EQ(!output_surface->software_device(),
558 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59559 return DRAW_MODE_SOFTWARE;
560 }
561}
562
[email protected]34ba1ffb2014-03-05 06:55:03563static void AppendQuadsForLayer(
564 RenderPass* target_render_pass,
565 LayerImpl* layer,
566 const OcclusionTracker<LayerImpl>& occlusion_tracker,
567 AppendQuadsData* append_quads_data) {
[email protected]c1bb5af2013-03-13 19:06:27568 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49569 QuadCuller quad_culler(&target_render_pass->quad_list,
570 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27571 layer,
572 occlusion_tracker,
573 layer->ShowDebugBorders(),
574 for_surface);
575 layer->AppendQuads(&quad_culler, append_quads_data);
576}
577
578static void AppendQuadsForRenderSurfaceLayer(
579 RenderPass* target_render_pass,
580 LayerImpl* layer,
581 const RenderPass* contributing_render_pass,
[email protected]34ba1ffb2014-03-05 06:55:03582 const OcclusionTracker<LayerImpl>& occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27583 AppendQuadsData* append_quads_data) {
584 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49585 QuadCuller quad_culler(&target_render_pass->quad_list,
586 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27587 layer,
588 occlusion_tracker,
589 layer->ShowDebugBorders(),
590 for_surface);
591
592 bool is_replica = false;
593 layer->render_surface()->AppendQuads(&quad_culler,
594 append_quads_data,
595 is_replica,
596 contributing_render_pass->id);
597
598 // Add replica after the surface so that it appears below the surface.
599 if (layer->has_replica()) {
600 is_replica = true;
601 layer->render_surface()->AppendQuads(&quad_culler,
602 append_quads_data,
603 is_replica,
604 contributing_render_pass->id);
605 }
606}
607
608static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08609 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20610 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06611 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27612 RenderPass* target_render_pass,
613 LayerImpl* root_layer,
614 SkColor screen_background_color,
[email protected]34ba1ffb2014-03-05 06:55:03615 const OcclusionTracker<LayerImpl>& occlusion_tracker) {
[email protected]c1bb5af2013-03-13 19:06:27616 if (!root_layer || !SkColorGetA(screen_background_color))
617 return;
618
619 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
620 if (fill_region.IsEmpty())
621 return;
622
[email protected]3744e27b2013-11-06 21:44:08623 // Divide the fill region into the part to be filled with the overhang
624 // resource and the part to be filled with the background color.
625 Region screen_background_color_region = fill_region;
626 Region overhang_region;
627 if (overhang_resource_id) {
628 overhang_region = fill_region;
629 overhang_region.Subtract(root_scroll_layer_rect);
630 screen_background_color_region.Intersect(root_scroll_layer_rect);
631 }
632
[email protected]c1bb5af2013-03-13 19:06:27633 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49634 QuadCuller quad_culler(&target_render_pass->quad_list,
635 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27636 root_layer,
637 occlusion_tracker,
638 root_layer->ShowDebugBorders(),
639 for_surface);
640
641 // Manually create the quad state for the gutter quads, as the root layer
642 // doesn't have any bounds and so can't generate this itself.
643 // TODO(danakj): Make the gutter quads generated by the solid color layer
644 // (make it smarter about generating quads to fill unoccluded areas).
645
646 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
647 float opacity = 1.f;
648 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49649 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]7785e1002014-02-20 21:04:32650 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27651 root_target_rect.size(),
652 root_target_rect,
653 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01654 false,
[email protected]7bbeaf4e2013-11-26 10:27:22655 opacity,
656 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15657
[email protected]c1bb5af2013-03-13 19:06:27658 AppendQuadsData append_quads_data;
[email protected]3744e27b2013-11-06 21:44:08659 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27660 fill_rects.has_rect();
661 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32662 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24663 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08664 // Skip the quad culler and just append the quads directly to avoid
665 // occlusion checks.
666 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]9bf06c72014-03-07 18:16:24667 quad->SetNew(shared_quad_state,
668 screen_space_rect,
669 visible_screen_space_rect,
670 screen_background_color,
671 false);
[email protected]3744e27b2013-11-06 21:44:08672 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
673 }
674 for (Region::Iterator fill_rects(overhang_region);
675 fill_rects.has_rect();
676 fill_rects.next()) {
677 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32678 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24679 gfx::Rect opaque_screen_space_rect = screen_space_rect;
680 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08681 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
682 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
683 tex_quad->SetNew(
684 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32685 screen_space_rect,
[email protected]9bf06c72014-03-07 18:16:24686 opaque_screen_space_rect,
687 visible_screen_space_rect,
[email protected]3744e27b2013-11-06 21:44:08688 overhang_resource_id,
689 false,
[email protected]7785e1002014-02-20 21:04:32690 gfx::PointF(
691 screen_space_rect.x() / overhang_resource_scaled_size.width(),
692 screen_space_rect.y() / overhang_resource_scaled_size.height()),
693 gfx::PointF(
694 screen_space_rect.right() / overhang_resource_scaled_size.width(),
695 screen_space_rect.bottom() /
696 overhang_resource_scaled_size.height()),
[email protected]3744e27b2013-11-06 21:44:08697 screen_background_color,
698 vertex_opacity,
699 false);
700 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27701 }
[email protected]467b3612012-08-28 07:41:16702}
703
[email protected]7ddfe7e82014-01-30 07:22:11704DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::CalculateRenderPasses(
705 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27706 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11707 DCHECK(CanDraw());
708 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32709
[email protected]c1bb5af2013-03-13 19:06:27710 TrackDamageForAllSurfaces(active_tree_->root_layer(),
711 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14712
[email protected]e0341352013-04-06 05:01:20713 // If the root render surface has no visible damage, then don't generate a
714 // frame at all.
715 RenderSurfaceImpl* root_surface =
716 active_tree_->root_layer()->render_surface();
717 bool root_surface_has_no_visible_damage =
718 !root_surface->damage_tracker()->current_damage_rect().Intersects(
719 root_surface->content_rect());
720 bool root_surface_has_contributing_layers =
721 !root_surface->layer_list().empty();
722 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23723 root_surface_has_no_visible_damage &&
724 active_tree_->LayersWithCopyOutputRequest().empty()) {
[email protected]e0341352013-04-06 05:01:20725 TRACE_EVENT0("cc",
726 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
727 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07728 DCHECK(!output_surface_->capabilities()
729 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11730 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20731 }
732
[email protected]c1bb5af2013-03-13 19:06:27733 TRACE_EVENT1("cc",
734 "LayerTreeHostImpl::CalculateRenderPasses",
735 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06736 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14737
[email protected]c1bb5af2013-03-13 19:06:27738 // Create the render passes in dependency order.
739 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06740 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27741 --surface_index) {
742 LayerImpl* render_surface_layer =
743 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45744 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
745
746 bool should_draw_into_render_pass =
747 render_surface_layer->parent() == NULL ||
748 render_surface->contributes_to_drawn_surface() ||
749 render_surface_layer->HasCopyRequest();
750 if (should_draw_into_render_pass)
751 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27752 }
[email protected]94f206c12012-08-25 00:09:14753
[email protected]f636ce82014-03-10 22:33:08754 // When we are displaying the HUD, change the root damage rect to cover the
755 // entire root surface. This will disable partial-swap/scissor optimizations
756 // that would prevent the HUD from updating, since the HUD does not cause
757 // damage itself, to prevent it from messing with damage visualizations. Since
758 // damage visualizations are done off the LayerImpls and RenderSurfaceImpls,
759 // changing the RenderPass does not affect them.
760 if (active_tree_->hud_layer()) {
761 RenderPass* root_pass = frame->render_passes.back();
762 root_pass->damage_rect = root_pass->output_rect;
763 }
764
[email protected]c1bb5af2013-03-13 19:06:27765 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52766 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27767 base::debug::TraceLog::GetInstance() &&
768 base::debug::TraceLog::GetInstance()->IsEnabled();
[email protected]34ba1ffb2014-03-05 06:55:03769 OcclusionTracker<LayerImpl> occlusion_tracker(
[email protected]c1bb5af2013-03-13 19:06:27770 active_tree_->root_layer()->render_surface()->content_rect(),
771 record_metrics_for_frame);
772 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52773 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14774
[email protected]846f455b2013-03-18 19:07:41775 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27776 occlusion_tracker.set_occluding_screen_space_rects_container(
777 &frame->occluding_screen_space_rects);
778 }
[email protected]846f455b2013-03-18 19:07:41779 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27780 occlusion_tracker.set_non_occluding_screen_space_rects_container(
781 &frame->non_occluding_screen_space_rects);
782 }
[email protected]94f206c12012-08-25 00:09:14783
[email protected]ba1b33e2014-02-28 16:44:51784 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27785 // occlusion and performing culling during the tree walk.
[email protected]ba1b33e2014-02-28 16:44:51786 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14787
[email protected]c1bb5af2013-03-13 19:06:27788 // Typically when we are missing a texture and use a checkerboard quad, we
789 // still draw the frame. However when the layer being checkerboarded is moving
790 // due to an impl-animation, we drop the frame to avoid flashing due to the
791 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11792 DrawSwapReadbackResult::DrawResult draw_result =
793 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25794 // When we have a copy request for a layer, we need to draw no matter
795 // what, as the layer may disappear after this frame.
796 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14797
[email protected]372bad5f2013-03-21 16:38:43798 int layers_drawn = 0;
799
[email protected]ffbb2212013-06-02 23:47:59800 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
801
[email protected]c1bb5af2013-03-13 19:06:27802 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09803 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27804 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09805 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27806 it != end;
807 ++it) {
808 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09809 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27810 RenderPass* target_render_pass =
811 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14812
[email protected]e47b0a0a2013-11-18 23:26:22813 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14814
[email protected]30fe19ff2013-07-04 00:54:45815 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30816
[email protected]18a70192013-04-26 16:18:25817 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36818 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25819 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11820 it->TakeCopyRequestsAndTransformToTarget(
821 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25822 }
[email protected]30fe19ff2013-07-04 00:54:45823 } else if (it.represents_contributing_render_surface() &&
824 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27825 RenderPass::Id contributing_render_pass_id =
826 it->render_surface()->RenderPassId();
827 RenderPass* contributing_render_pass =
828 frame->render_passes_by_id[contributing_render_pass_id];
829 AppendQuadsForRenderSurfaceLayer(target_render_pass,
830 *it,
831 contributing_render_pass,
832 occlusion_tracker,
833 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45834 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09835 !it->visible_content_rect().IsEmpty()) {
[email protected]7d6d6c92014-03-06 01:18:50836 bool occluded = occlusion_tracker.Occluded(it->render_target(),
837 it->visible_content_rect(),
838 it->draw_transform());
[email protected]fbc293322013-10-01 05:07:15839 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27840 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59841
[email protected]c1bb5af2013-03-13 19:06:27842 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57843
[email protected]c1bb5af2013-03-13 19:06:27844 if (it->HasContributingDelegatedRenderPasses()) {
845 RenderPass::Id contributing_render_pass_id =
846 it->FirstContributingRenderPassId();
847 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
848 frame->render_passes_by_id.end()) {
849 RenderPass* render_pass =
850 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14851
[email protected]c1bb5af2013-03-13 19:06:27852 AppendQuadsData append_quads_data(render_pass->id);
853 AppendQuadsForLayer(render_pass,
854 *it,
855 occlusion_tracker,
856 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57857
[email protected]c1bb5af2013-03-13 19:06:27858 contributing_render_pass_id =
859 it->NextContributingRenderPassId(contributing_render_pass_id);
860 }
[email protected]94f206c12012-08-25 00:09:14861 }
862
[email protected]c1bb5af2013-03-13 19:06:27863 AppendQuadsForLayer(target_render_pass,
864 *it,
865 occlusion_tracker,
866 &append_quads_data);
867 }
[email protected]89228202012-08-29 03:20:30868
[email protected]372bad5f2013-03-21 16:38:43869 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14870 }
871
[email protected]e1e768f2013-03-26 08:48:09872 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27873 bool layer_has_animating_transform =
874 it->screen_space_transform_is_animating() ||
875 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11876 if (layer_has_animating_transform) {
877 draw_result =
878 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
879 }
[email protected]c1bb5af2013-03-13 19:06:27880 }
881
[email protected]3d609bb2014-02-01 01:10:23882 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27883 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23884 if (active_tree()->RequiresHighResToDraw())
885 draw_result =
886 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
887 }
[email protected]c1bb5af2013-03-13 19:06:27888
889 occlusion_tracker.LeaveLayer(it);
890 }
891
[email protected]6133cc232013-07-30 18:47:07892 if (have_copy_request ||
893 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11894 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25895
[email protected]1d993172012-10-18 18:15:04896#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27897 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
898 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
899 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
900 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
901 != frame->render_passes_by_id.end());
902 }
[email protected]94f206c12012-08-25 00:09:14903#endif
[email protected]c1bb5af2013-03-13 19:06:27904 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14905
[email protected]c1bb5af2013-03-13 19:06:27906 if (!active_tree_->has_transparent_background()) {
907 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08908 AppendQuadsToFillScreen(
909 ResourceIdForUIResource(overhang_ui_resource_id_),
910 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
911 active_tree_->RootScrollLayerDeviceViewportBounds(),
912 frame->render_passes.back(),
913 active_tree_->root_layer(),
914 active_tree_->background_color(),
915 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27916 }
[email protected]94f206c12012-08-25 00:09:14917
[email protected]7ddfe7e82014-01-30 07:22:11918 if (draw_result == DrawSwapReadbackResult::DRAW_SUCCESS)
[email protected]c1bb5af2013-03-13 19:06:27919 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25920 else
921 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14922
[email protected]c1bb5af2013-03-13 19:06:27923 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48924 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14925
[email protected]30fe19ff2013-07-04 00:54:45926 // Any copy requests left in the tree are not going to get serviced, and
927 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11928 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
929 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
930 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
931 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45932 }
[email protected]19334ac2013-12-12 23:59:11933 for (size_t i = 0; i < requests_to_abort.size(); ++i)
934 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45935
[email protected]e0341352013-04-06 05:01:20936 // If we're making a frame to draw, it better have at least one render pass.
937 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48938
939 // Should only have one render pass in resourceless software mode.
940 if (output_surface_->ForcedDrawToSoftwareDevice())
941 DCHECK_EQ(1u, frame->render_passes.size());
942
[email protected]7ddfe7e82014-01-30 07:22:11943 return draw_result;
[email protected]94f206c12012-08-25 00:09:14944}
945
[email protected]200a9c062013-05-20 04:34:37946void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
947 if (input_handler_client_)
948 input_handler_client_->MainThreadHasStoppedFlinging();
949}
950
[email protected]3d9f7432013-04-06 00:35:18951void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
952 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15953 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01954 if (should_background_tick)
955 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15956
[email protected]3d9f7432013-04-06 00:35:18957 bool enabled = should_background_tick &&
958 !animation_registrar_->active_animation_controllers().empty();
959
[email protected]c1bb5af2013-03-13 19:06:27960 // Lazily create the time_source adapter so that we can vary the interval for
961 // testing.
962 if (!time_source_client_adapter_) {
963 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
964 this,
[email protected]810d40b72013-06-20 18:26:15965 DelayBasedTimeSource::Create(
966 LowFrequencyAnimationInterval(),
967 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
968 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27969 }
[email protected]94f206c12012-08-25 00:09:14970
[email protected]c1bb5af2013-03-13 19:06:27971 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14972}
973
[email protected]b8384e22013-12-03 02:20:48974void LayerTreeHostImpl::DidAnimateScrollOffset() {
975 client_->SetNeedsCommitOnImplThread();
976 client_->RenewTreePriority();
977}
978
[email protected]0023fc72014-01-10 20:05:06979void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02980 viewport_damage_rect_.Union(damage_rect);
981}
982
[email protected]c1bb5af2013-03-13 19:06:27983static inline RenderPass* FindRenderPassById(
984 RenderPass::Id render_pass_id,
985 const LayerTreeHostImpl::FrameData& frame) {
986 RenderPassIdHashMap::const_iterator it =
987 frame.render_passes_by_id.find(render_pass_id);
988 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14989}
990
[email protected]c1bb5af2013-03-13 19:06:27991static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
992 LayerTreeHostImpl::FrameData* frame) {
993 RenderPass* remove_render_pass =
994 FindRenderPassById(remove_render_pass_id, *frame);
995 // The pass was already removed by another quad - probably the original, and
996 // we are the replica.
997 if (!remove_render_pass)
998 return;
999 RenderPassList& render_passes = frame->render_passes;
1000 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
1001 render_passes.end(),
1002 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:141003
[email protected]c1bb5af2013-03-13 19:06:271004 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:141005
[email protected]c1bb5af2013-03-13 19:06:271006 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
1007 frame->render_passes.erase(to_remove);
1008 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:141009
[email protected]c1bb5af2013-03-13 19:06:271010 // Now follow up for all RenderPass quads and remove their RenderPasses
1011 // recursively.
1012 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291013 QuadList::ConstBackToFrontIterator quad_list_iterator =
1014 quad_list.BackToFrontBegin();
1015 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271016 ++quad_list_iterator) {
1017 DrawQuad* current_quad = (*quad_list_iterator);
1018 if (current_quad->material != DrawQuad::RENDER_PASS)
1019 continue;
[email protected]94f206c12012-08-25 00:09:141020
[email protected]c1bb5af2013-03-13 19:06:271021 RenderPass::Id next_remove_render_pass_id =
1022 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1023 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1024 }
[email protected]94f206c12012-08-25 00:09:141025}
1026
[email protected]c1bb5af2013-03-13 19:06:271027bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1028 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1029 const RenderPass* render_pass =
1030 FindRenderPassById(quad.render_pass_id, frame);
1031 if (!render_pass)
1032 return false;
[email protected]94f206c12012-08-25 00:09:141033
[email protected]c1bb5af2013-03-13 19:06:271034 // If any quad or RenderPass draws into this RenderPass, then keep it.
1035 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291036 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1037 quad_list.BackToFrontBegin();
1038 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271039 ++quad_list_iterator) {
1040 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141041
[email protected]c1bb5af2013-03-13 19:06:271042 if (current_quad->material != DrawQuad::RENDER_PASS)
1043 return false;
[email protected]94f206c12012-08-25 00:09:141044
[email protected]c1bb5af2013-03-13 19:06:271045 const RenderPass* contributing_pass = FindRenderPassById(
1046 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1047 if (contributing_pass)
1048 return false;
1049 }
1050 return true;
[email protected]94f206c12012-08-25 00:09:141051}
1052
1053// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271054template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271055 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1056 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141057
1058// static
[email protected]c1bb5af2013-03-13 19:06:271059template <typename RenderPassCuller>
1060void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1061 FrameData* frame) {
1062 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1063 it != culler.RenderPassListEnd(frame->render_passes);
1064 it = culler.RenderPassListNext(it)) {
1065 const RenderPass* current_pass = frame->render_passes[it];
1066 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291067 QuadList::ConstBackToFrontIterator quad_list_iterator =
1068 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141069
[email protected]ed511b8d2013-03-25 03:29:291070 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271071 ++quad_list_iterator) {
1072 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141073
[email protected]c1bb5af2013-03-13 19:06:271074 if (current_quad->material != DrawQuad::RENDER_PASS)
1075 continue;
[email protected]94f206c12012-08-25 00:09:141076
[email protected]c1bb5af2013-03-13 19:06:271077 const RenderPassDrawQuad* render_pass_quad =
1078 RenderPassDrawQuad::MaterialCast(current_quad);
1079 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1080 continue;
[email protected]94f206c12012-08-25 00:09:141081
[email protected]c1bb5af2013-03-13 19:06:271082 // We are changing the vector in the middle of iteration. Because we
1083 // delete render passes that draw into the current pass, we are
1084 // guaranteed that any data from the iterator to the end will not
1085 // change. So, capture the iterator position from the end of the
1086 // list, and restore it after the change.
1087 size_t position_from_end = frame->render_passes.size() - it;
1088 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1089 it = frame->render_passes.size() - position_from_end;
1090 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141091 }
[email protected]c1bb5af2013-03-13 19:06:271092 }
[email protected]94f206c12012-08-25 00:09:141093}
1094
[email protected]7ddfe7e82014-01-30 07:22:111095DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::PrepareToDraw(
1096 FrameData* frame,
1097 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471098 TRACE_EVENT1("cc",
1099 "LayerTreeHostImpl::PrepareToDraw",
1100 "SourceFrameNumber",
1101 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141102
[email protected]425e5332013-09-21 04:10:031103 if (need_to_update_visible_tiles_before_draw_ &&
1104 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1105 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191106 }
[email protected]425e5332013-09-21 04:10:031107 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191108
[email protected]7d19dc342013-05-02 22:02:041109 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271110
[email protected]c1bb5af2013-03-13 19:06:271111 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1112 frame->render_passes.clear();
1113 frame->render_passes_by_id.clear();
1114 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201115 frame->contains_incomplete_tile = false;
1116 frame->has_no_damage = false;
1117
[email protected]0023fc72014-01-10 20:05:061118 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201119 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021120 device_viewport_damage_rect.Union(viewport_damage_rect_);
1121 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201122
1123 active_tree_->root_layer()->render_surface()->damage_tracker()->
1124 AddDamageNextUpdate(device_viewport_damage_rect);
1125 }
[email protected]94f206c12012-08-25 00:09:141126
[email protected]7ddfe7e82014-01-30 07:22:111127 DrawSwapReadbackResult::DrawResult draw_result = CalculateRenderPasses(frame);
1128 if (draw_result != DrawSwapReadbackResult::DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071129 DCHECK(!output_surface_->capabilities()
1130 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111131 return draw_result;
[email protected]6133cc232013-07-30 18:47:071132 }
[email protected]94f206c12012-08-25 00:09:141133
[email protected]7ddfe7e82014-01-30 07:22:111134 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1135 // this function is called again.
1136 return draw_result;
[email protected]94f206c12012-08-25 00:09:141137}
1138
[email protected]0309ba5e02013-06-26 04:11:081139void LayerTreeHostImpl::EvictTexturesForTesting() {
1140 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1141}
1142
[email protected]4f48f6e2013-08-27 06:33:381143void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1144 NOTREACHED();
1145}
1146
[email protected]425e5332013-09-21 04:10:031147void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1148 DidInitializeVisibleTile();
1149}
1150
[email protected]c1bb5af2013-03-13 19:06:271151void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1152 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121153
[email protected]c1bb5af2013-03-13 19:06:271154 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351155 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351156 ManagedMemoryPolicy::PriorityCutoffToValue(
1157 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351158 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271159 if (evicted_resources) {
1160 active_tree_->SetContentsTexturesPurged();
1161 if (pending_tree_)
1162 pending_tree_->SetContentsTexturesPurged();
1163 client_->SetNeedsCommitOnImplThread();
1164 client_->OnCanDrawStateChanged(CanDraw());
1165 client_->RenewTreePriority();
1166 }
1167 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431168
[email protected]8be1a9bf2013-05-01 03:45:191169 UpdateTileManagerMemoryPolicy(policy);
1170}
1171
1172void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1173 const ManagedMemoryPolicy& policy) {
1174 if (!tile_manager_)
1175 return;
1176
[email protected]8be1a9bf2013-05-01 03:45:191177 // TODO(reveman): We should avoid keeping around unused resources if
1178 // possible. crbug.com/224475
[email protected]25489c02014-02-13 23:18:411179 global_tile_state_.hard_memory_limit_in_bytes = 0;
1180 global_tile_state_.soft_memory_limit_in_bytes = 0;
1181 if (visible_ && policy.bytes_limit_when_visible > 0) {
1182 global_tile_state_.hard_memory_limit_in_bytes =
1183 policy.bytes_limit_when_visible;
1184 global_tile_state_.soft_memory_limit_in_bytes =
[email protected]831414e2014-02-22 02:27:371185 (static_cast<int64>(global_tile_state_.hard_memory_limit_in_bytes) *
1186 settings_.max_memory_for_prepaint_percentage) /
1187 100;
[email protected]25489c02014-02-13 23:18:411188 }
1189 // Unused limit is calculated from soft-limit, as hard-limit may
1190 // be very high and shouldn't typically be exceeded.
[email protected]1bcced22013-09-24 13:51:191191 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
[email protected]25489c02014-02-13 23:18:411192 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1193 settings_.max_unused_resource_memory_percentage) /
1194 100);
[email protected]1bcced22013-09-24 13:51:191195 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191196 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1197 visible_ ?
1198 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351199 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191200 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081201
[email protected]c48536a52013-09-14 00:02:081202 DidModifyTilePriorities();
1203}
1204
1205void LayerTreeHostImpl::DidModifyTilePriorities() {
1206 DCHECK(settings_.impl_side_painting);
1207 // Mark priorities as dirty and schedule a ManageTiles().
1208 tile_priorities_dirty_ = true;
1209 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141210}
1211
[email protected]86126792013-03-16 20:07:541212void LayerTreeHostImpl::DidInitializeVisibleTile() {
1213 // TODO(reveman): Determine tiles that changed and only damage
1214 // what's necessary.
1215 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031216 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541217 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471218}
1219
[email protected]94bf75c2013-06-12 13:20:041220void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381221 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041222}
1223
[email protected]3f2ff112013-08-03 02:41:071224void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1225 SetManagedMemoryPolicy(policy, zero_budget_);
1226}
1227
[email protected]5f07afc2013-07-19 05:17:181228void LayerTreeHostImpl::SetTreeActivationCallback(
1229 const base::Closure& callback) {
1230 DCHECK(proxy_->IsImplThread());
1231 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261232 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181233}
1234
[email protected]c1bb5af2013-03-13 19:06:271235void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071236 const ManagedMemoryPolicy& policy, bool zero_budget) {
1237 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271238 return;
[email protected]61de5812012-11-08 07:03:441239
[email protected]3f2ff112013-08-03 02:41:071240 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1241
1242 cached_managed_memory_policy_ = policy;
1243 zero_budget_ = zero_budget;
1244 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1245
1246 if (old_policy == actual_policy)
1247 return;
1248
1249 if (!proxy_->HasImplThread()) {
1250 // In single-thread mode, this can be called on the main thread by
1251 // GLRenderer::OnMemoryAllocationChanged.
1252 DebugScopedSetImplThread impl_thread(proxy_);
1253 EnforceManagedMemoryPolicy(actual_policy);
1254 } else {
1255 DCHECK(proxy_->IsImplThread());
1256 EnforceManagedMemoryPolicy(actual_policy);
1257 }
[email protected]50644642013-06-20 13:58:551258
[email protected]d7626ffd2013-03-29 00:17:421259 // If there is already enough memory to draw everything imaginable and the
1260 // new memory limit does not change this, then do not re-commit. Don't bother
1261 // skipping commits if this is not visible (commits don't happen when not
1262 // visible, there will almost always be a commit when this becomes visible).
1263 bool needs_commit = true;
1264 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071265 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1266 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1267 actual_policy.priority_cutoff_when_visible ==
1268 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421269 needs_commit = false;
1270 }
1271
[email protected]d7626ffd2013-03-29 00:17:421272 if (needs_commit)
1273 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141274}
1275
[email protected]f224cc92013-06-06 23:23:321276void LayerTreeHostImpl::SetExternalDrawConstraints(
1277 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061278 const gfx::Rect& viewport,
1279 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281280 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321281 external_transform_ = transform;
1282 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281283 external_clip_ = clip;
1284 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321285}
1286
[email protected]0023fc72014-01-10 20:05:061287void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311288 if (damage_rect.IsEmpty())
1289 return;
1290 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031291 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1292}
1293
[email protected]daea3d42013-10-23 17:04:501294void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1295 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451296}
1297
[email protected]4d7e46a2013-11-08 05:33:401298void LayerTreeHostImpl::DidSwapBuffers() {
1299 client_->DidSwapBuffersOnImplThread();
1300}
1301
[email protected]a7335e0b2013-09-18 09:34:511302void LayerTreeHostImpl::OnSwapBuffersComplete() {
1303 client_->OnSwapBuffersCompleteOnImplThread();
1304}
1305
1306void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271307 // TODO(piman): We may need to do some validation on this ack before
1308 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511309 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291310 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161311}
1312
[email protected]c1bb5af2013-03-13 19:06:271313void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1314 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291315}
1316
[email protected]c1bb5af2013-03-13 19:06:271317CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1318 CompositorFrameMetadata metadata;
1319 metadata.device_scale_factor = device_scale_factor_;
1320 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1321 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1322 metadata.root_layer_size = active_tree_->ScrollableSize();
1323 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1324 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1325 if (top_controls_manager_) {
1326 metadata.location_bar_offset =
1327 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1328 metadata.location_bar_content_translation =
1329 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011330 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271331 }
[email protected]bf189f62012-12-18 03:42:111332
[email protected]adeda572014-01-31 00:49:471333 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111334 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271335
[email protected]adeda572014-01-31 00:49:471336 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271337
1338 return metadata;
[email protected]bf189f62012-12-18 03:42:111339}
1340
[email protected]6f50b8fa2013-07-01 19:51:491341static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1342 layer->DidBeginTracing();
1343}
[email protected]37349bc2013-06-04 01:31:521344
[email protected]f0c2a242013-03-15 19:34:521345void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1346 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351347 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271348 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201349
[email protected]3519b872013-07-30 07:17:501350 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191351 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071352 DCHECK(!output_surface_->capabilities()
1353 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201354 return;
[email protected]3519b872013-07-30 07:17:501355 }
[email protected]e0341352013-04-06 05:01:201356
[email protected]c1bb5af2013-03-13 19:06:271357 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141358
[email protected]213fcfc2013-08-24 14:59:271359 fps_counter_->SaveTimeStamp(frame_begin_time,
1360 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351361 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091362 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351363 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431364
[email protected]c1bb5af2013-03-13 19:06:271365 if (tile_manager_) {
1366 memory_history_->SaveEntry(
1367 tile_manager_->memory_stats_from_last_assign());
1368 }
[email protected]1191d9d2013-02-02 06:00:331369
[email protected]846f455b2013-03-18 19:07:411370 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021371 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271372 active_tree_->root_layer(),
[email protected]f636ce82014-03-10 22:33:081373 active_tree_->hud_layer(),
[email protected]c1bb5af2013-03-13 19:06:271374 *frame->render_surface_layer_list,
1375 frame->occluding_screen_space_rects,
1376 frame->non_occluding_screen_space_rects,
1377 debug_state_);
1378 }
[email protected]94f206c12012-08-25 00:09:141379
[email protected]a848c102013-03-26 08:59:091380 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1381 const RenderingStats& stats =
1382 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181383 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091384 }
1385
[email protected]37349bc2013-06-04 01:31:521386 bool is_new_trace;
1387 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1388 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491389 if (pending_tree_) {
1390 LayerTreeHostCommon::CallFunctionForSubtree(
1391 pending_tree_->root_layer(),
1392 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1393 }
1394 LayerTreeHostCommon::CallFunctionForSubtree(
1395 active_tree_->root_layer(),
1396 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521397 }
1398
[email protected]9197dbcb2013-05-15 20:28:511399 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211400 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1401 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1402 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081403
[email protected]c1bb5af2013-03-13 19:06:271404 // Because the contents of the HUD depend on everything else in the frame, the
1405 // contents of its texture are updated as the last thing before the frame is
1406 // drawn.
[email protected]671f6672013-10-01 07:31:071407 if (active_tree_->hud_layer()) {
1408 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531409 active_tree_->hud_layer()->UpdateHudTexture(
1410 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071411 }
[email protected]94f206c12012-08-25 00:09:141412
[email protected]b09c1942013-05-10 00:06:371413 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]251699b2013-10-09 00:21:261414 bool disable_picture_quad_image_filtering =
1415 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321416
[email protected]b09c1942013-05-10 00:06:371417 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321418 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301419 temp_software_renderer->DrawFrame(&frame->render_passes,
1420 NULL,
1421 device_scale_factor_,
1422 DeviceViewport(),
1423 DeviceClip(),
[email protected]59fee302013-12-10 17:53:301424 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371425 } else {
[email protected]b5174d712013-08-28 08:10:431426 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321427 offscreen_context_provider_.get(),
1428 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301429 DeviceViewport(),
1430 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261431 false);
[email protected]b09c1942013-05-10 00:06:371432 }
[email protected]c1bb5af2013-03-13 19:06:271433 // The render passes should be consumed by the renderer.
1434 DCHECK(frame->render_passes.empty());
1435 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141436
[email protected]c1bb5af2013-03-13 19:06:271437 // The next frame should start by assuming nothing has changed, and changes
1438 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321439 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1440 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541441 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271442 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1443 DidDrawDamagedArea();
1444 }
1445 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351446
[email protected]d73094482014-02-13 21:28:281447 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331448 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1449 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181450 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141451}
1452
[email protected]c1bb5af2013-03-13 19:06:271453void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1454 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1455 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521456
[email protected]c1bb5af2013-03-13 19:06:271457 // Once all layers have been drawn, pending texture uploads should no
1458 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151459 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141460}
1461
[email protected]c1bb5af2013-03-13 19:06:271462void LayerTreeHostImpl::FinishAllRendering() {
1463 if (renderer_)
1464 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141465}
1466
[email protected]c1bb5af2013-03-13 19:06:271467bool LayerTreeHostImpl::IsContextLost() {
1468 DCHECK(proxy_->IsImplThread());
1469 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141470}
1471
[email protected]7a8bcd262014-01-15 12:54:581472const RendererCapabilitiesImpl&
1473LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271474 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141475}
1476
[email protected]e0341352013-04-06 05:01:201477bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231478 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191479 if (frame.has_no_damage) {
1480 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201481 return false;
[email protected]b69c1db2013-11-27 00:05:191482 }
[email protected]4a28a612013-11-27 02:06:331483 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551484 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331485 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161486 return true;
[email protected]94f206c12012-08-25 00:09:141487}
1488
[email protected]daea3d42013-10-23 17:04:501489void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451490 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501491 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451492}
1493
[email protected]fef74fd2014-02-27 06:28:171494gfx::SizeF LayerTreeHostImpl::ComputeInnerViewportContainerSize() const {
1495 gfx::SizeF dip_size =
1496 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
1497
1498 float top_offset =
1499 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
1500
1501 return gfx::SizeF(dip_size.width(),
1502 dip_size.height() - top_offset - overdraw_bottom_height_);
1503}
1504
1505void LayerTreeHostImpl::UpdateInnerViewportContainerSize() {
1506 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
1507 if (!container_layer)
1508 return;
1509
[email protected]05dfe4f2014-02-27 19:52:501510 // We pass the value returned from UnscaledScrollableViewportSize() here as
1511 // it accounts for scrollbar dimensions when
1512 // container_layer->masks_to_bounds() is set.
1513 container_layer->SetTemporaryImplBounds(UnscaledScrollableViewportSize());
[email protected]fef74fd2014-02-27 06:28:171514}
1515
[email protected]54af03522013-09-05 00:43:281516gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341517 // Use the root container layer bounds if it clips to them, otherwise, the
1518 // true viewport size should be used.
[email protected]fef74fd2014-02-27 06:28:171519 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341520 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441521 DCHECK(!top_controls_manager_);
1522 DCHECK_EQ(0, overdraw_bottom_height_);
1523 return container_layer->bounds();
1524 }
1525
[email protected]fef74fd2014-02-27 06:28:171526 return ComputeInnerViewportContainerSize();
1527}
[email protected]ffb2720f2013-03-15 19:18:371528
[email protected]fef74fd2014-02-27 06:28:171529float LayerTreeHostImpl::VerticalAdjust() const {
1530 if (!active_tree_->InnerViewportContainerLayer())
1531 return 0;
1532
1533 return active_tree_->InnerViewportContainerLayer()->BoundsDelta().y();
[email protected]ffb2720f2013-03-15 19:18:371534}
1535
[email protected]c1bb5af2013-03-13 19:06:271536void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191537 if (resource_provider_)
1538 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011539 // TODO(jamesr): The renderer_ check is needed to make some of the
1540 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1541 // important) in production. We should adjust the test to not need this.
1542 if (renderer_)
1543 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]ce2e8112013-11-28 07:44:361544#ifndef NDEBUG
1545 did_lose_called_ = true;
1546#endif
[email protected]94f206c12012-08-25 00:09:141547}
1548
[email protected]c1bb5af2013-03-13 19:06:271549void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061550 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271551 DCHECK(renderer_);
1552 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141553}
1554
[email protected]59adb112013-04-09 04:48:441555bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471556 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011557}
1558
[email protected]c1bb5af2013-03-13 19:06:271559LayerImpl* LayerTreeHostImpl::RootLayer() const {
1560 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081561}
1562
[email protected]adeda572014-01-31 00:49:471563LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1564 return active_tree_->InnerViewportScrollLayer();
1565}
1566
1567LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1568 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081569}
1570
[email protected]c1bb5af2013-03-13 19:06:271571LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1572 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081573}
1574
[email protected]251699b2013-10-09 00:21:261575bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1576 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471577 (InnerViewportScrollLayer() &&
1578 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1579 (OuterViewportScrollLayer() &&
1580 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261581}
1582
[email protected]94f206c12012-08-25 00:09:141583// Content layers can be either directly scrollable or contained in an outer
1584// scrolling layer which applies the scroll transform. Given a content layer,
1585// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271586static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1587 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021588 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271589
1590 if (layer_impl->scrollable())
1591 return layer_impl;
1592
1593 if (layer_impl->DrawsContent() &&
1594 layer_impl->parent() &&
1595 layer_impl->parent()->scrollable())
1596 return layer_impl->parent();
1597
[email protected]bf1cfd9a2013-09-26 05:43:021598 return NULL;
[email protected]94f206c12012-08-25 00:09:141599}
1600
[email protected]c1bb5af2013-03-13 19:06:271601void LayerTreeHostImpl::CreatePendingTree() {
1602 CHECK(!pending_tree_);
1603 if (recycle_tree_)
1604 recycle_tree_.swap(pending_tree_);
1605 else
1606 pending_tree_ = LayerTreeImpl::create(this);
1607 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111608 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271609}
1610
[email protected]8bb6cfa2013-07-23 00:11:191611void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191612 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1613 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191614 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481615}
1616
[email protected]c1bb5af2013-03-13 19:06:271617void LayerTreeHostImpl::ActivatePendingTree() {
1618 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111619 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351620
[email protected]4f48f6e2013-08-27 06:33:381621 need_to_update_visible_tiles_before_draw_ = true;
1622
[email protected]1960a712013-04-30 17:06:471623 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271624 active_tree_->PushPersistedState(pending_tree_.get());
1625 if (pending_tree_->needs_full_tree_sync()) {
1626 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331627 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271628 active_tree_->DetachLayerTree(),
1629 active_tree_.get()));
1630 }
[email protected]b5651c22013-03-14 15:06:331631 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271632 active_tree_->root_layer());
1633 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511634
[email protected]c9280762013-08-01 06:28:571635 // Process any requests in the UI resource queue. The request queue is given
1636 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1637 // the swap.
1638 pending_tree_->ProcessUIResourceRequestQueue();
1639
[email protected]c1bb5af2013-03-13 19:06:271640 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511641
[email protected]c1bb5af2013-03-13 19:06:271642 // Now that we've synced everything from the pending tree to the active
1643 // tree, rename the pending tree the recycle tree so we can reuse it on the
1644 // next sync.
1645 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511646
[email protected]7d1b07e2013-10-01 17:31:301647 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471648 active_tree_->SetRootLayerScrollOffsetDelegate(
1649 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221650
[email protected]c1bb5af2013-03-13 19:06:271651 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441652 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271653 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241654
[email protected]a848c102013-03-26 08:59:091655 if (debug_state_.continuous_painting) {
1656 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431657 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091658 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1659 stats.main_stats.record_time +
1660 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271661 }
[email protected]2a61ad52013-05-13 14:01:291662
[email protected]fef74fd2014-02-27 06:28:171663 UpdateInnerViewportContainerSize();
[email protected]2a61ad52013-05-13 14:01:291664 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261665 if (!tree_activation_callback_.is_null())
1666 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011667
1668 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1669 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281670 devtools_instrumentation::DidActivateLayerTree(
1671 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271672}
1673
[email protected]c1bb5af2013-03-13 19:06:271674void LayerTreeHostImpl::SetVisible(bool visible) {
1675 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141676
[email protected]c1bb5af2013-03-13 19:06:271677 if (visible_ == visible)
1678 return;
1679 visible_ = visible;
1680 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121681 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141682
[email protected]3d609bb2014-02-01 01:10:231683 if (!visible_) {
1684 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481685 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231686 }
[email protected]127bdc1a2013-09-11 01:44:481687
[email protected]0fa42e02013-08-20 01:16:051688 // Evict tiles immediately if invisible since this tab may never get another
1689 // draw or timer tick.
1690 if (!visible_)
1691 ManageTiles();
1692
[email protected]c1bb5af2013-03-13 19:06:271693 if (!renderer_)
1694 return;
[email protected]94f206c12012-08-25 00:09:141695
[email protected]c1bb5af2013-03-13 19:06:271696 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141697}
1698
[email protected]6be422b2013-12-08 06:47:311699void LayerTreeHostImpl::SetNeedsRedraw() {
1700 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1701 client_->SetNeedsRedrawOnImplThread();
1702}
1703
[email protected]206a3922013-05-17 06:34:121704ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071705 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011706 // TODO(ernstm): The second condition disables pre-painting for all layers
1707 // when GPU rasterization is enabled. Once we selectively enable GPU
1708 // rasterization per layer, we also need to disable pre-painting selectively:
1709 // crbug.com/335387
1710 if (debug_state_.rasterize_only_visible_content ||
[email protected]a23530d2014-03-11 06:04:141711 settings_.rasterization_site == LayerTreeSettings::GpuRasterization) {
[email protected]3f2ff112013-08-03 02:41:071712 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291713 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071714 }
[email protected]206a3922013-05-17 06:34:121715
[email protected]3f2ff112013-08-03 02:41:071716 if (zero_budget_) {
1717 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071718 }
1719
[email protected]206a3922013-05-17 06:34:121720 return actual;
1721}
1722
[email protected]3f2ff112013-08-03 02:41:071723size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1724 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1725}
1726
[email protected]990e050a2013-09-23 18:50:211727int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1728 return ManagedMemoryPolicy::PriorityCutoffToValue(
1729 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1730}
1731
[email protected]50644642013-06-20 13:58:551732void LayerTreeHostImpl::ReleaseTreeResources() {
1733 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551734 SendReleaseResourcesRecursive(active_tree_->root_layer());
1735 if (pending_tree_ && pending_tree_->root_layer())
1736 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1737 if (recycle_tree_ && recycle_tree_->root_layer())
1738 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571739
[email protected]127bdc1a2013-09-11 01:44:481740 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551741}
1742
1743void LayerTreeHostImpl::CreateAndSetRenderer(
1744 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201745 ResourceProvider* resource_provider,
1746 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551747 DCHECK(!renderer_);
1748 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321749 renderer_ = DelegatingRenderer::Create(
1750 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091751 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551752 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321753 &settings_,
[email protected]50644642013-06-20 13:58:551754 output_surface,
1755 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111756 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241757 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551758 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321759 renderer_ = SoftwareRenderer::Create(
1760 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551761 }
1762
[email protected]84975832013-06-29 00:24:111763 if (renderer_) {
[email protected]50644642013-06-20 13:58:551764 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111765 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561766
1767 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1768 // initialized to get max texture size. Also, after releasing resources,
1769 // trees need another update to generate new ones.
1770 active_tree_->set_needs_update_draw_properties();
1771 if (pending_tree_)
1772 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591773 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111774 }
[email protected]50644642013-06-20 13:58:551775}
1776
[email protected]50af3c22013-07-13 03:50:201777void LayerTreeHostImpl::CreateAndSetTileManager(
1778 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011779 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371780 bool using_map_image,
1781 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201782 DCHECK(settings_.impl_side_painting);
1783 DCHECK(resource_provider);
[email protected]98805e62014-03-05 01:33:591784 DCHECK(proxy_->ImplThreadTaskRunner());
[email protected]b6eb8e332013-09-10 00:51:011785 tile_manager_ =
1786 TileManager::Create(this,
[email protected]98805e62014-03-05 01:33:591787 proxy_->ImplThreadTaskRunner(),
[email protected]b6eb8e332013-09-10 00:51:011788 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281789 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011790 rendering_stats_instrumentation_,
1791 using_map_image,
[email protected]4e472a42014-02-11 01:56:371792 allow_rasterize_on_demand,
[email protected]b4664142013-11-08 00:50:311793 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121794 GetMaxRasterTasksUsageBytes(context_provider),
1795 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011796
[email protected]50af3c22013-07-13 03:50:201797 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191798 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201799}
1800
[email protected]50644642013-06-20 13:58:551801void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071802 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551803}
1804
[email protected]c1bb5af2013-03-13 19:06:271805bool LayerTreeHostImpl::InitializeRenderer(
1806 scoped_ptr<OutputSurface> output_surface) {
[email protected]ce2e8112013-11-28 07:44:361807#ifndef NDEBUG
1808 DCHECK(!renderer_ || did_lose_called_);
1809#endif
1810
[email protected]c1bb5af2013-03-13 19:06:271811 // Since we will create a new resource provider, we cannot continue to use
1812 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1813 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551814 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401815
[email protected]c1bb5af2013-03-13 19:06:271816 // Note: order is important here.
1817 renderer_.reset();
1818 tile_manager_.reset();
1819 resource_provider_.reset();
1820 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141821
[email protected]c1bb5af2013-03-13 19:06:271822 if (!output_surface->BindToClient(this))
1823 return false;
[email protected]be3181652012-09-25 13:02:131824
[email protected]a7f35682013-10-22 23:05:571825 scoped_ptr<ResourceProvider> resource_provider =
1826 ResourceProvider::Create(output_surface.get(),
1827 shared_bitmap_manager_,
1828 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341829 settings_.use_rgba_4444_textures,
1830 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551831 if (!resource_provider)
1832 return false;
[email protected]2b154b22013-06-07 09:03:271833
[email protected]50644642013-06-20 13:58:551834 if (output_surface->capabilities().deferred_gl_initialization)
1835 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131836
[email protected]50af3c22013-07-13 03:50:201837 bool skip_gl_renderer = false;
1838 CreateAndSetRenderer(
1839 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131840
[email protected]50644642013-06-20 13:58:551841 if (!renderer_)
1842 return false;
[email protected]6123930c2013-05-23 06:48:371843
[email protected]50644642013-06-20 13:58:551844 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371845 CreateAndSetTileManager(
1846 resource_provider.get(),
1847 output_surface->context_provider().get(),
1848 GetRendererCapabilities().using_map_image,
1849 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241850 }
1851
[email protected]daea3d42013-10-23 17:04:501852 // Setup BeginImplFrameEmulation if it's not supported natively
1853 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351854 const base::TimeDelta display_refresh_interval =
1855 base::TimeDelta::FromMicroseconds(
1856 base::Time::kMicrosecondsPerSecond /
1857 settings_.refresh_rate);
1858
[email protected]daea3d42013-10-23 17:04:501859 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151860 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351861 settings_.throttle_frame_production,
1862 display_refresh_interval);
1863 }
1864
1865 int max_frames_pending =
1866 output_surface->capabilities().max_frames_pending;
1867 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351868 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351869 output_surface->SetMaxFramesPending(max_frames_pending);
1870
[email protected]50644642013-06-20 13:58:551871 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271872 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141873
[email protected]c1bb5af2013-03-13 19:06:271874 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211875
[email protected]c1bb5af2013-03-13 19:06:271876 return true;
[email protected]94f206c12012-08-25 00:09:141877}
1878
[email protected]2b154b22013-06-07 09:03:271879bool LayerTreeHostImpl::DeferredInitialize(
1880 scoped_refptr<ContextProvider> offscreen_context_provider) {
1881 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551882 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091883 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271884
[email protected]50644642013-06-20 13:58:551885 ReleaseTreeResources();
1886 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271887
[email protected]d9ca99e2013-08-28 21:49:481888 bool resource_provider_success = resource_provider_->InitializeGL();
1889
1890 bool success = resource_provider_success;
1891 if (success) {
1892 bool skip_gl_renderer = false;
1893 CreateAndSetRenderer(
1894 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1895 if (!renderer_)
1896 success = false;
1897 }
1898
1899 if (success) {
1900 if (offscreen_context_provider.get() &&
1901 !offscreen_context_provider->BindToCurrentThread())
1902 success = false;
1903 }
1904
[email protected]50644642013-06-20 13:58:551905 if (success) {
1906 EnforceZeroBudget(false);
1907 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481908 } else {
1909 if (offscreen_context_provider.get()) {
1910 if (offscreen_context_provider->BindToCurrentThread())
1911 offscreen_context_provider->VerifyContexts();
1912 offscreen_context_provider = NULL;
1913 }
1914
1915 client_->DidLoseOutputSurfaceOnImplThread();
1916
1917 if (resource_provider_success) {
1918 // If this fails the context provider will be dropped from the output
1919 // surface and destroyed. But the GLRenderer expects the output surface
1920 // to stick around - and hold onto the context3d - as long as it is alive.
1921 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1922 renderer_.reset();
1923
1924 // The resource provider can't stay in GL mode or it tries to clean up GL
1925 // stuff, but the context provider is going away on the output surface
1926 // which contradicts being in GL mode.
1927 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1928 resource_provider_->InitializeSoftware();
1929 }
[email protected]50644642013-06-20 13:58:551930 }
[email protected]b5174d712013-08-28 08:10:431931
1932 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271933 return success;
1934}
1935
[email protected]50af3c22013-07-13 03:50:201936void LayerTreeHostImpl::ReleaseGL() {
1937 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1938 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091939 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201940
1941 ReleaseTreeResources();
1942 renderer_.reset();
1943 tile_manager_.reset();
1944 resource_provider_->InitializeSoftware();
1945
1946 bool skip_gl_renderer = true;
1947 CreateAndSetRenderer(
1948 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1949 DCHECK(renderer_);
1950
1951 EnforceZeroBudget(true);
1952 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011953 NULL,
[email protected]4e472a42014-02-11 01:56:371954 GetRendererCapabilities().using_map_image,
1955 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201956 DCHECK(tile_manager_);
1957
[email protected]b5174d712013-08-28 08:10:431958 SetOffscreenContextProvider(NULL);
1959
[email protected]50af3c22013-07-13 03:50:201960 client_->SetNeedsCommitOnImplThread();
1961}
1962
[email protected]64348ea2014-01-29 22:58:261963void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:401964 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271965 return;
[email protected]94f206c12012-08-25 00:09:141966
[email protected]54af03522013-09-05 00:43:281967 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271968 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271969
[email protected]c1bb5af2013-03-13 19:06:271970 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141971
[email protected]fef74fd2014-02-27 06:28:171972 UpdateInnerViewportContainerSize();
[email protected]c1bb5af2013-03-13 19:06:271973 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441974 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141975}
1976
[email protected]d9083762013-03-24 01:36:401977void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1978 if (overdraw_bottom_height == overdraw_bottom_height_)
1979 return;
1980 overdraw_bottom_height_ = overdraw_bottom_height;
1981
[email protected]fef74fd2014-02-27 06:28:171982 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441983 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401984}
1985
[email protected]9f4f6a32013-09-04 21:35:121986void LayerTreeHostImpl::SetOverhangUIResource(
1987 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:261988 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:121989 overhang_ui_resource_id_ = overhang_ui_resource_id;
1990 overhang_ui_resource_size_ = overhang_ui_resource_size;
1991}
1992
[email protected]c1bb5af2013-03-13 19:06:271993void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1994 if (device_scale_factor == device_scale_factor_)
1995 return;
1996 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271997
[email protected]fef74fd2014-02-27 06:28:171998 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:441999 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142000}
2001
[email protected]54af03522013-09-05 00:43:282002gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
2003 return DeviceViewport().size();
2004}
2005
[email protected]f224cc92013-06-06 23:23:322006gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
2007 if (external_viewport_.IsEmpty())
2008 return gfx::Rect(device_viewport_size_);
2009
2010 return external_viewport_;
2011}
2012
[email protected]54af03522013-09-05 00:43:282013gfx::Rect LayerTreeHostImpl::DeviceClip() const {
2014 if (external_clip_.IsEmpty())
2015 return DeviceViewport();
2016
2017 return external_clip_;
2018}
2019
2020const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:322021 return external_transform_;
2022}
2023
[email protected]59adb112013-04-09 04:48:442024void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]fef74fd2014-02-27 06:28:172025 UpdateInnerViewportContainerSize();
[email protected]4e4136d2013-11-29 02:22:442026 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:442027 active_tree_->set_needs_update_draw_properties();
2028 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142029}
2030
[email protected]c1bb5af2013-03-13 19:06:272031bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:042032 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:342033 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:142034}
2035
[email protected]200a9c062013-05-20 04:34:372036void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2037 DCHECK(input_handler_client_ == NULL);
2038 input_handler_client_ = client;
2039}
2040
[email protected]420fdf6e2013-08-26 20:36:382041static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2042 if (LayerImpl* scroll_parent = layer->scroll_parent())
2043 return scroll_parent;
2044 return layer->parent();
2045}
2046
[email protected]f620b0e72013-10-01 21:38:242047LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472048 const gfx::PointF& device_viewport_point,
2049 InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:432050 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:022051 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142052
[email protected]c1bb5af2013-03-13 19:06:272053 // Walk up the hierarchy and look for a scrollable layer.
2054 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382055 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272056 // The content layer can also block attempts to scroll outside the main
2057 // thread.
2058 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2059 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022060 *scroll_on_main_thread = true;
2061 return NULL;
[email protected]94f206c12012-08-25 00:09:142062 }
2063
[email protected]c1bb5af2013-03-13 19:06:272064 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2065 if (!scroll_layer_impl)
2066 continue;
[email protected]94f206c12012-08-25 00:09:142067
[email protected]c1bb5af2013-03-13 19:06:272068 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272069 // If any layer wants to divert the scroll event to the main thread, abort.
2070 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022071 *scroll_on_main_thread = true;
2072 return NULL;
[email protected]94f206c12012-08-25 00:09:142073 }
2074
[email protected]c1bb5af2013-03-13 19:06:272075 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2076 potentially_scrolling_layer_impl = scroll_layer_impl;
2077 }
2078
[email protected]bf1cfd9a2013-09-26 05:43:022079 return potentially_scrolling_layer_impl;
2080}
2081
2082InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
[email protected]47a723f2014-03-05 12:42:492083 const gfx::Point& viewport_point,
2084 InputHandler::ScrollInputType type) {
[email protected]bf1cfd9a2013-09-26 05:43:022085 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2086
2087 if (top_controls_manager_)
2088 top_controls_manager_->ScrollBegin();
2089
2090 DCHECK(!CurrentlyScrollingLayer());
2091 ClearCurrentlyScrollingLayer();
2092
2093 if (!EnsureRenderSurfaceLayerList())
2094 return ScrollIgnored;
2095
[email protected]f620b0e72013-10-01 21:38:242096 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2097 device_scale_factor_);
2098 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2099 device_viewport_point,
2100 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022101 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242102 LayerImpl* potentially_scrolling_layer_impl =
2103 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2104 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022105
2106 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022107 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2108 return ScrollOnMainThread;
2109 }
2110
[email protected]adeda572014-01-31 00:49:472111 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532112 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472113 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2114 ? OuterViewportScrollLayer()
2115 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102116
[email protected]c1bb5af2013-03-13 19:06:272117 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202118 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272119 potentially_scrolling_layer_impl);
2120 should_bubble_scrolls_ = (type != NonBubblingGesture);
2121 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272122 client_->RenewTreePriority();
2123 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2124 return ScrollStarted;
2125 }
2126 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142127}
2128
[email protected]c1bb5af2013-03-13 19:06:272129gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2130 LayerImpl* layer_impl,
2131 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472132 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122133 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272134 // Layers with non-invertible screen space transforms should not have passed
2135 // the scroll hit test in the first place.
2136 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2137 gfx::Transform inverse_screen_space_transform(
2138 gfx::Transform::kSkipInitialization);
2139 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2140 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352141 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2142 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272143 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142144
[email protected]c1bb5af2013-03-13 19:06:272145 gfx::PointF screen_space_point =
2146 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142147
[email protected]c1bb5af2013-03-13 19:06:272148 gfx::Vector2dF screen_space_delta = viewport_delta;
2149 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2150
2151 // First project the scroll start and end points to local layer space to find
2152 // the scroll delta in layer coordinates.
2153 bool start_clipped, end_clipped;
2154 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2155 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212156 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272157 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212158 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272159 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212160 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272161 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212162 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272163
2164 // In general scroll point coordinates should not get clipped.
2165 DCHECK(!start_clipped);
2166 DCHECK(!end_clipped);
2167 if (start_clipped || end_clipped)
2168 return gfx::Vector2dF();
2169
2170 // local_start_point and local_end_point are in content space but we want to
2171 // move them to layer space for scrolling.
2172 float width_scale = 1.f / layer_impl->contents_scale_x();
2173 float height_scale = 1.f / layer_impl->contents_scale_y();
2174 local_start_point.Scale(width_scale, height_scale);
2175 local_end_point.Scale(width_scale, height_scale);
2176
2177 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472178 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272179 layer_impl->ScrollBy(local_end_point - local_start_point);
2180
2181 // Get the end point in the layer's content space so we can apply its
2182 // ScreenSpaceTransform.
2183 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472184 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272185 previous_delta;
2186 gfx::PointF actual_local_content_end_point =
2187 gfx::ScalePoint(actual_local_end_point,
2188 1.f / width_scale,
2189 1.f / height_scale);
2190
2191 // Calculate the applied scroll delta in viewport space coordinates.
2192 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212193 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272194 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212195 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272196 DCHECK(!end_clipped);
2197 if (end_clipped)
2198 return gfx::Vector2dF();
2199 gfx::PointF actual_viewport_end_point =
2200 gfx::ScalePoint(actual_screen_space_end_point,
2201 1.f / scale_from_viewport_to_screen_space);
2202 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142203}
2204
[email protected]c1bb5af2013-03-13 19:06:272205static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122206 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472207 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272208 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472209 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272210}
2211
[email protected]47a723f2014-03-05 12:42:492212bool LayerTreeHostImpl::ScrollBy(const gfx::Point& viewport_point,
[email protected]3244c9132014-01-23 10:39:122213 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272214 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2215 if (!CurrentlyScrollingLayer())
2216 return false;
2217
2218 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532219 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292220 bool did_scroll_x = false;
2221 bool did_scroll_y = false;
[email protected]f42cffb2014-03-08 18:03:252222 bool did_scroll_top_controls = false;
[email protected]adeda572014-01-31 00:49:472223 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2224 // here?
2225 bool consume_by_top_controls =
2226 top_controls_manager_ &&
2227 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2228 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2229 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2230 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062231
[email protected]c1bb5af2013-03-13 19:06:272232 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2233 layer_impl;
2234 layer_impl = layer_impl->parent()) {
2235 if (!layer_impl->scrollable())
2236 continue;
2237
[email protected]adeda572014-01-31 00:49:472238 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532239 // Only allow bubble scrolling when the scroll is in the direction to make
2240 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472241 gfx::Vector2dF applied_delta;
2242 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532243 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472244 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2245 applied_delta = pending_delta - excess_delta;
2246 pending_delta = excess_delta;
2247 // Force updating of vertical adjust values if needed.
[email protected]f42cffb2014-03-08 18:03:252248 if (applied_delta.y() != 0) {
2249 did_scroll_top_controls = true;
[email protected]adeda572014-01-31 00:49:472250 layer_impl->ScrollbarParametersDidChange();
[email protected]f42cffb2014-03-08 18:03:252251 }
[email protected]a2b5ded2013-05-20 21:32:532252 }
2253 // Track root layer deltas for reporting overscroll.
2254 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422255 }
2256
[email protected]c1bb5af2013-03-13 19:06:272257 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272258 // Gesture events need to be transformed from viewport coordinates to local
2259 // layer coordinates so that the scrolling contents exactly follow the
2260 // user's finger. In contrast, wheel events represent a fixed amount of
2261 // scrolling so we can just apply them directly.
2262 if (!wheel_scrolling_) {
2263 float scale_from_viewport_to_screen_space = device_scale_factor_;
2264 applied_delta =
2265 ScrollLayerWithViewportSpaceDelta(layer_impl,
2266 scale_from_viewport_to_screen_space,
2267 viewport_point, pending_delta);
2268 } else {
2269 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142270 }
[email protected]94f206c12012-08-25 00:09:142271
[email protected]c1bb5af2013-03-13 19:06:272272 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292273 float move_threshold = 0.1f;
2274 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2275 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2276 did_scroll_x |= did_move_layer_x;
2277 did_scroll_y |= did_move_layer_y;
2278 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142279 // Scrolls should always bubble between the outer and inner viewports
2280 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2281 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272282 continue;
[email protected]db102612013-12-20 22:04:122283 else
2284 break;
[email protected]94f206c12012-08-25 00:09:142285 }
[email protected]a2b5ded2013-05-20 21:32:532286
[email protected]adeda572014-01-31 00:49:472287 if (layer_impl == InnerViewportScrollLayer())
[email protected]a2b5ded2013-05-20 21:32:532288 unused_root_delta.Subtract(applied_delta);
2289
[email protected]c1bb5af2013-03-13 19:06:272290 did_lock_scrolling_layer_ = true;
2291 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202292 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272293 break;
[email protected]94f206c12012-08-25 00:09:142294 }
[email protected]94f206c12012-08-25 00:09:142295
[email protected]c1bb5af2013-03-13 19:06:272296 // If the applied delta is within 45 degrees of the input delta, bail out to
2297 // make it easier to scroll just one layer in one direction without
2298 // affecting any of its parents.
2299 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212300 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272301 applied_delta, pending_delta) < angle_threshold) {
2302 pending_delta = gfx::Vector2d();
2303 break;
[email protected]4a23c374c2012-12-08 08:38:552304 }
[email protected]c1bb5af2013-03-13 19:06:272305
2306 // Allow further movement only on an axis perpendicular to the direction in
2307 // which the layer moved.
2308 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212309 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272310
[email protected]a2b5ded2013-05-20 21:32:532311 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272312 break;
2313 }
2314
[email protected]f42cffb2014-03-08 18:03:252315 bool did_scroll_content = did_scroll_x || did_scroll_y;
2316 if (did_scroll_content) {
[email protected]c1bb5af2013-03-13 19:06:272317 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442318 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272319 client_->RenewTreePriority();
2320 }
[email protected]a2b5ded2013-05-20 21:32:532321
[email protected]2bd503f2013-07-23 05:35:292322 // Scrolling along an axis resets accumulated root overscroll for that axis.
2323 if (did_scroll_x)
2324 accumulated_root_overscroll_.set_x(0);
2325 if (did_scroll_y)
2326 accumulated_root_overscroll_.set_y(0);
2327
[email protected]a2b5ded2013-05-20 21:32:532328 accumulated_root_overscroll_ += unused_root_delta;
2329 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2330 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112331 DidOverscrollParams params;
2332 params.accumulated_overscroll = accumulated_root_overscroll_;
2333 params.latest_overscroll_delta = unused_root_delta;
2334 params.current_fling_velocity = current_fling_velocity_;
2335 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532336 }
2337
[email protected]f42cffb2014-03-08 18:03:252338 return did_scroll_content || did_scroll_top_controls;
[email protected]4a23c374c2012-12-08 08:38:552339}
2340
[email protected]be782f52013-03-23 21:36:142341// This implements scrolling by page as described here:
2342// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2343// for events with WHEEL_PAGESCROLL set.
[email protected]47a723f2014-03-05 12:42:492344bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
[email protected]c28df4c12013-05-22 17:36:492345 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142346 DCHECK(wheel_scrolling_);
2347
2348 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2349 layer_impl;
2350 layer_impl = layer_impl->parent()) {
2351 if (!layer_impl->scrollable())
2352 continue;
2353
[email protected]adeda572014-01-31 00:49:472354 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142355 continue;
2356
[email protected]adeda572014-01-31 00:49:472357 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142358
2359 // These magical values match WebKit and are designed to scroll nearly the
2360 // entire visible content height but leave a bit of overlap.
2361 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492362 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142363 page = -page;
2364
2365 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2366
2367 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2368
2369 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142370 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442371 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142372 client_->RenewTreePriority();
2373 return true;
2374 }
2375
2376 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2377 }
2378
2379 return false;
2380}
2381
[email protected]1960a712013-04-30 17:06:472382void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2383 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2384 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2385 active_tree_->SetRootLayerScrollOffsetDelegate(
2386 root_layer_scroll_offset_delegate_);
2387}
2388
2389void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2390 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2391 client_->SetNeedsCommitOnImplThread();
2392}
2393
[email protected]c1bb5af2013-03-13 19:06:272394void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2395 active_tree_->ClearCurrentlyScrollingLayer();
2396 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532397 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202398 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142399}
2400
[email protected]c1bb5af2013-03-13 19:06:272401void LayerTreeHostImpl::ScrollEnd() {
2402 if (top_controls_manager_)
2403 top_controls_manager_->ScrollEnd();
2404 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052405 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142406}
2407
[email protected]5ff3c9782013-04-29 17:35:122408InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462409 if (!active_tree_->CurrentlyScrollingLayer())
2410 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212411
[email protected]75147f52013-08-02 19:11:462412 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472413 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2414 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462415 ClearCurrentlyScrollingLayer();
2416 return ScrollIgnored;
2417 }
2418
[email protected]db102612013-12-20 22:04:122419 if (!wheel_scrolling_) {
2420 // Allow the fling to lock to the first layer that moves after the initial
2421 // fling |ScrollBy()| event.
2422 did_lock_scrolling_layer_ = false;
2423 should_bubble_scrolls_ = false;
2424 }
[email protected]df0c42342013-10-08 20:52:122425
[email protected]75147f52013-08-02 19:11:462426 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212427}
2428
[email protected]3244c9132014-01-23 10:39:122429void LayerTreeHostImpl::NotifyCurrentFlingVelocity(
2430 const gfx::Vector2dF& velocity) {
[email protected]6e921bd2013-04-29 21:10:202431 current_fling_velocity_ = velocity;
2432}
2433
[email protected]bf1cfd9a2013-09-26 05:43:022434float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472435 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022436 LayerImpl* layer_impl) {
2437 if (!layer_impl)
2438 return std::numeric_limits<float>::max();
2439
2440 gfx::Rect layer_impl_bounds(
2441 layer_impl->content_bounds());
2442
2443 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2444 layer_impl->screen_space_transform(),
2445 layer_impl_bounds);
2446
2447 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2448 device_viewport_point);
2449}
2450
[email protected]47a723f2014-03-05 12:42:492451void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) {
[email protected]bf1cfd9a2013-09-26 05:43:022452 if (!EnsureRenderSurfaceLayerList())
2453 return;
2454
[email protected]f620b0e72013-10-01 21:38:242455 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2456 device_scale_factor_);
2457
2458 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2459 device_viewport_point,
2460 active_tree_->RenderSurfaceLayerList());
2461 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2462 return;
2463
2464 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2465 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2466 scroll_layer_id_when_mouse_over_scrollbar_);
2467
[email protected]5cd9ac12014-02-05 21:48:532468 // The check for a null scroll_layer_impl below was added to see if it will
2469 // eliminate the crashes described in http://crbug.com/326635.
2470 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242471 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532472 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2473 : NULL;
[email protected]f620b0e72013-10-01 21:38:242474 if (animation_controller) {
2475 animation_controller->DidMouseMoveOffScrollbar(
2476 CurrentPhysicalTimeTicks());
2477 StartScrollbarAnimation();
2478 }
2479 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2480 }
2481
[email protected]bf1cfd9a2013-09-26 05:43:022482 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242483 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2484 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022485 &scroll_on_main_thread);
2486 if (scroll_on_main_thread || !scroll_layer_impl)
2487 return;
2488
2489 ScrollbarAnimationController* animation_controller =
2490 scroll_layer_impl->scrollbar_animation_controller();
2491 if (!animation_controller)
2492 return;
2493
[email protected]adeda572014-01-31 00:49:472494 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2495 float distance_to_scrollbar = std::numeric_limits<float>::max();
2496 for (LayerImpl::ScrollbarSet::iterator it =
2497 scroll_layer_impl->scrollbars()->begin();
2498 it != scroll_layer_impl->scrollbars()->end();
2499 ++it)
2500 distance_to_scrollbar =
2501 std::min(distance_to_scrollbar,
2502 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022503
2504 bool should_animate = animation_controller->DidMouseMoveNear(
2505 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202506 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022507 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022508}
2509
[email protected]f620b0e72013-10-01 21:38:242510bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472511 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242512 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2513 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2514 layer_impl = active_tree_->LayerById(scroll_layer_id);
2515 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2516 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202517 bool should_animate =
2518 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2519 CurrentPhysicalTimeTicks(), 0);
2520 if (should_animate)
2521 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242522 } else {
2523 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2524 }
2525
2526 return true;
2527 }
2528
2529 return false;
2530}
2531
[email protected]c1bb5af2013-03-13 19:06:272532void LayerTreeHostImpl::PinchGestureBegin() {
2533 pinch_gesture_active_ = true;
2534 previous_pinch_anchor_ = gfx::Point();
2535 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132536 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472537 if (active_tree_->OuterViewportScrollLayer()) {
2538 active_tree_->SetCurrentlyScrollingLayer(
2539 active_tree_->OuterViewportScrollLayer());
2540 } else {
2541 active_tree_->SetCurrentlyScrollingLayer(
2542 active_tree_->InnerViewportScrollLayer());
2543 }
[email protected]a9bda5e2013-10-25 18:43:372544 if (top_controls_manager_)
2545 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142546}
2547
[email protected]c1bb5af2013-03-13 19:06:272548void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
[email protected]47a723f2014-03-05 12:42:492549 const gfx::Point& anchor) {
[email protected]b93480862014-02-20 13:07:032550 if (!InnerViewportScrollLayer())
2551 return;
[email protected]d3afa112012-12-08 06:24:282552
[email protected]b93480862014-02-20 13:07:032553 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282554
[email protected]c1bb5af2013-03-13 19:06:272555 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2556 // position over the course of the magnify.
2557 float page_scale_delta = active_tree_->page_scale_delta();
2558 gfx::PointF previous_scale_anchor =
2559 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2560 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2561 page_scale_delta = active_tree_->page_scale_delta();
2562 gfx::PointF new_scale_anchor =
2563 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2564 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2565
2566 previous_pinch_anchor_ = anchor;
2567
2568 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472569 // If clamping the inner viewport scroll offset causes a change, it should
2570 // be accounted for from the intended move.
2571 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272572
[email protected]adeda572014-01-31 00:49:472573 // We manually manage the bubbling behaviour here as it is different to that
2574 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2575 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2576 // 2) we don't want the directional limitations on the unused parts that
2577 // ScrollBy() implements, and
2578 // 3) pinching should not engage the top controls manager.
2579 gfx::Vector2dF unused = OuterViewportScrollLayer()
2580 ? OuterViewportScrollLayer()->ScrollBy(move)
2581 : move;
2582
2583 if (!unused.IsZero()) {
2584 InnerViewportScrollLayer()->ScrollBy(unused);
2585 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2586 }
[email protected]c1bb5af2013-03-13 19:06:272587
[email protected]c1bb5af2013-03-13 19:06:272588 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442589 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272590 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282591}
2592
[email protected]c1bb5af2013-03-13 19:06:272593void LayerTreeHostImpl::PinchGestureEnd() {
2594 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132595 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2596 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2597 ClearCurrentlyScrollingLayer();
2598 }
[email protected]a9bda5e2013-10-25 18:43:372599 if (top_controls_manager_)
2600 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272601 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142602}
2603
[email protected]c1bb5af2013-03-13 19:06:272604static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2605 LayerImpl* layer_impl) {
2606 if (!layer_impl)
2607 return;
[email protected]94f206c12012-08-25 00:09:142608
[email protected]c1bb5af2013-03-13 19:06:272609 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472610 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272611 if (!scroll_delta.IsZero()) {
2612 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392613 scroll.layer_id = layer_impl->id();
2614 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272615 scroll_info->scrolls.push_back(scroll);
2616 layer_impl->SetSentScrollDelta(scroll_delta);
2617 }
[email protected]94f206c12012-08-25 00:09:142618
[email protected]c1bb5af2013-03-13 19:06:272619 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2620 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142621}
2622
[email protected]c1bb5af2013-03-13 19:06:272623scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2624 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302625
[email protected]c1bb5af2013-03-13 19:06:272626 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392627 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2628 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302629
[email protected]c1bb5af2013-03-13 19:06:272630 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302631}
2632
[email protected]c1bb5af2013-03-13 19:06:272633void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282634 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102635}
2636
[email protected]adeda572014-01-31 00:49:472637void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2638 DCHECK(InnerViewportScrollLayer());
2639 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2640 ? OuterViewportScrollLayer()
2641 : InnerViewportScrollLayer();
2642
2643 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2644
2645 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2646 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2647}
2648
[email protected]c1bb5af2013-03-13 19:06:272649void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
[email protected]adeda572014-01-31 00:49:472650 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272651 return;
2652
2653 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472654 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272655
[email protected]34e1b512013-09-26 17:32:332656 if (!page_scale_animation_->IsAnimationStarted())
2657 page_scale_animation_->StartAnimation(monotonic_time);
2658
[email protected]c1bb5af2013-03-13 19:06:272659 active_tree_->SetPageScaleDelta(
2660 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2661 active_tree_->page_scale_factor());
2662 gfx::Vector2dF next_scroll =
2663 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2664
[email protected]adeda572014-01-31 00:49:472665 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442666 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272667
2668 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2669 page_scale_animation_.reset();
2670 client_->SetNeedsCommitOnImplThread();
2671 client_->RenewTreePriority();
2672 }
[email protected]829ad972013-01-28 23:36:102673}
2674
[email protected]ffb2720f2013-03-15 19:18:372675void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082676 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372677 return;
2678 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472679 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172680 return;
[email protected]5f7d9112014-01-17 21:31:422681 if (scroll.IsZero()) {
2682 // This may happen on the first animation step. Force redraw otherwise
2683 // the animation would stop because of no new frames.
2684 SetNeedsRedraw();
2685 } else {
[email protected]adeda572014-01-31 00:49:472686 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422687 scroll, 1.f / active_tree_->total_page_scale_factor()));
2688 }
[email protected]ffb2720f2013-03-15 19:18:372689}
2690
[email protected]c1bb5af2013-03-13 19:06:272691void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2692 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522693 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272694 animation_registrar_->active_animation_controllers().empty() ||
2695 !active_tree_->root_layer())
2696 return;
2697
2698 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2699
[email protected]c1bb5af2013-03-13 19:06:272700 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2701
2702 AnimationRegistrar::AnimationControllerMap copy =
2703 animation_registrar_->active_animation_controllers();
2704 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2705 iter != copy.end();
2706 ++iter)
2707 (*iter).second->Animate(monotonic_seconds);
2708
[email protected]4e4136d2013-11-29 02:22:442709 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082710}
2711
[email protected]3d9f7432013-04-06 00:35:182712void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522713 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272714 animation_registrar_->active_animation_controllers().empty() ||
2715 !active_tree_->root_layer())
2716 return;
2717
2718 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2719 scoped_ptr<AnimationEventsVector> events =
2720 make_scoped_ptr(new AnimationEventsVector);
2721 AnimationRegistrar::AnimationControllerMap copy =
2722 animation_registrar_->active_animation_controllers();
2723 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2724 iter != copy.end();
2725 ++iter)
[email protected]3d9f7432013-04-06 00:35:182726 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272727
2728 if (!events->empty()) {
[email protected]85b57502014-03-11 15:37:482729 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass());
[email protected]c1bb5af2013-03-13 19:06:272730 }
[email protected]131a0c22013-02-12 18:31:082731}
2732
[email protected]c1bb5af2013-03-13 19:06:272733base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2734 return base::TimeDelta::FromSeconds(1);
2735}
2736
[email protected]50644642013-06-20 13:58:552737void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352738 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502739 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352740 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552741 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352742 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552743 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352744 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552745 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352746}
2747
[email protected]b5174d712013-08-28 08:10:432748void LayerTreeHostImpl::SetOffscreenContextProvider(
2749 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2750 if (!offscreen_context_provider.get()) {
2751 offscreen_context_provider_ = NULL;
2752 return;
2753 }
2754
2755 if (!offscreen_context_provider->BindToCurrentThread()) {
2756 offscreen_context_provider_ = NULL;
2757 return;
2758 }
2759
2760 offscreen_context_provider_ = offscreen_context_provider;
2761}
2762
[email protected]c1bb5af2013-03-13 19:06:272763std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2764 std::string str;
2765 if (active_tree_->root_layer()) {
2766 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2767 base::JSONWriter::WriteWithOptions(
2768 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2769 }
2770 return str;
2771}
2772
[email protected]c1bb5af2013-03-13 19:06:272773int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362774 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272775}
2776
[email protected]c1bb5af2013-03-13 19:06:272777void LayerTreeHostImpl::SendManagedMemoryStats(
2778 size_t memory_visible_bytes,
2779 size_t memory_visible_and_nearby_bytes,
2780 size_t memory_use_bytes) {
2781 if (!renderer_)
2782 return;
2783
2784 // Round the numbers being sent up to the next 8MB, to throttle the rate
2785 // at which we spam the GPU process.
2786 static const size_t rounding_step = 8 * 1024 * 1024;
2787 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2788 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2789 rounding_step);
2790 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2791 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2792 last_sent_memory_visible_and_nearby_bytes_ ==
2793 memory_visible_and_nearby_bytes &&
2794 last_sent_memory_use_bytes_ == memory_use_bytes) {
2795 return;
2796 }
2797 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2798 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2799 last_sent_memory_use_bytes_ = memory_use_bytes;
2800
2801 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2802 last_sent_memory_visible_and_nearby_bytes_,
2803 last_sent_memory_use_bytes_);
2804}
2805
2806void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2807 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2808}
2809
2810void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2811 base::TimeTicks time) {
2812 if (!layer)
2813 return;
2814
2815 ScrollbarAnimationController* scrollbar_controller =
2816 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352817 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202818 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422819 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2820 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442821 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202822 }
[email protected]c1bb5af2013-03-13 19:06:272823
2824 for (size_t i = 0; i < layer->children().size(); ++i)
2825 AnimateScrollbarsRecursive(layer->children()[i], time);
2826}
2827
[email protected]21c9dee72013-06-15 01:20:052828void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202829 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052830 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202831}
2832
2833void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2834 base::TimeTicks time) {
2835 if (!layer)
2836 return;
2837
2838 ScrollbarAnimationController* scrollbar_controller =
2839 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352840 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2841 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202842 if (delay > base::TimeDelta())
2843 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352844 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442845 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202846 }
2847
2848 for (size_t i = 0; i < layer->children().size(); ++i)
2849 StartScrollbarAnimationRecursive(layer->children()[i], time);
2850}
2851
[email protected]c1bb5af2013-03-13 19:06:272852void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2853 if (!tile_manager_)
2854 return;
2855
[email protected]1bcced22013-09-24 13:51:192856 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272857 return;
[email protected]1bcced22013-09-24 13:51:192858 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082859 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272860}
2861
[email protected]8347d692013-05-17 23:22:382862void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552863 current_frame_timeticks_ = base::TimeTicks();
2864 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272865}
2866
[email protected]21c9dee72013-06-15 01:20:052867void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2868 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552869 if (ticks->is_null()) {
2870 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052871 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552872 *now = base::Time::Now();
2873 }
2874}
2875
2876base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2877 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2878 return current_frame_timeticks_;
2879}
2880
2881base::Time LayerTreeHostImpl::CurrentFrameTime() {
2882 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272883 return current_frame_time_;
2884}
2885
[email protected]21c9dee72013-06-15 01:20:052886base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462887 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052888}
2889
[email protected]34806722013-08-09 23:51:212890scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2891 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272892 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222893 if (this->pending_tree_)
2894 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272895 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212896 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272897 if (tile_manager_)
2898 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2899 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222900 if (pending_tree_)
2901 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212902 if (frame)
2903 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222904 return state.PassAs<base::Value>();
2905}
2906
2907scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222908 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2909 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472910 if (tile_manager_)
2911 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272912 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082913}
2914
[email protected]6e7fdeb2013-07-09 14:28:382915void LayerTreeHostImpl::SetDebugState(
2916 const LayerTreeDebugState& new_debug_state) {
2917 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2918 return;
2919 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272920 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242921
[email protected]6e7fdeb2013-07-09 14:28:382922 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122923 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382924 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022925}
2926
[email protected]741fba422013-09-20 03:34:142927void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2928 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572929 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572930
[email protected]efa48412013-09-05 15:30:162931 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142932 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162933 case UIResourceBitmap::CLAMP_TO_EDGE:
2934 wrap_mode = GL_CLAMP_TO_EDGE;
2935 break;
2936 case UIResourceBitmap::REPEAT:
2937 wrap_mode = GL_REPEAT;
2938 break;
2939 }
2940
[email protected]c9280762013-08-01 06:28:572941 // Allow for multiple creation requests with the same UIResourceId. The
2942 // previous resource is simply deleted.
2943 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2944 if (id)
2945 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122946
2947 ResourceFormat format = resource_provider_->best_texture_format();
2948 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2949 format = ETC1;
[email protected]c9280762013-08-01 06:28:572950 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142951 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162952 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142953 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122954 format);
[email protected]c9280762013-08-01 06:28:572955
[email protected]efbdb3a2013-10-04 00:35:132956 UIResourceData data;
2957 data.resource_id = id;
2958 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512959 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132960
2961 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032962
2963 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572964 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032965 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142966 gfx::Rect(bitmap.GetSize()),
2967 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572968 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482969 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572970}
2971
2972void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2973 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2974 if (id) {
2975 resource_provider_->DeleteResource(id);
2976 ui_resource_map_.erase(uid);
2977 }
[email protected]127bdc1a2013-09-11 01:44:482978 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572979}
2980
[email protected]127bdc1a2013-09-11 01:44:482981void LayerTreeHostImpl::EvictAllUIResources() {
2982 if (ui_resource_map_.empty())
2983 return;
2984
[email protected]5f4dc4f2013-09-05 14:58:212985 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2986 iter != ui_resource_map_.end();
2987 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482988 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132989 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212990 }
2991 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482992
2993 client_->SetNeedsCommitOnImplThread();
2994 client_->OnCanDrawStateChanged(CanDraw());
2995 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212996}
2997
[email protected]c9280762013-08-01 06:28:572998ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2999 UIResourceId uid) const {
3000 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3001 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:133002 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:573003 return 0;
3004}
3005
[email protected]709c9542013-10-26 01:43:513006bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
3007 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3008 DCHECK(iter != ui_resource_map_.end());
3009 return iter->second.opaque;
3010}
3011
[email protected]127bdc1a2013-09-11 01:44:483012bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
3013 return !evicted_ui_resources_.empty();
3014}
3015
3016void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
3017 std::set<UIResourceId>::iterator found_in_evicted =
3018 evicted_ui_resources_.find(uid);
3019 if (found_in_evicted == evicted_ui_resources_.end())
3020 return;
3021 evicted_ui_resources_.erase(found_in_evicted);
3022 if (evicted_ui_resources_.empty())
3023 client_->OnCanDrawStateChanged(CanDraw());
3024}
3025
[email protected]5e5648a2013-11-18 00:39:333026void LayerTreeHostImpl::ScheduleMicroBenchmark(
3027 scoped_ptr<MicroBenchmarkImpl> benchmark) {
3028 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
3029}
3030
[email protected]6be422b2013-12-08 06:47:313031void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3032 swap_promise_monitor_.insert(monitor);
3033}
3034
3035void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3036 swap_promise_monitor_.erase(monitor);
3037}
3038
3039void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3040 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3041 for (; it != swap_promise_monitor_.end(); it++)
3042 (*it)->OnSetNeedsRedrawOnImpl();
3043}
3044
[email protected]d3143c732012-10-05 19:17:593045} // namespace cc