blob: 788c71ec4d3e8538e56f3515ef5fb1a52a6c05df [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]d12aa932014-08-01 13:10:3812#include "base/debug/trace_event_argument.h"
[email protected]4a23c374c2012-12-08 08:38:5513#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1414#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3415#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4816#include "base/strings/stringprintf.h"
[email protected]749cbc62014-07-10 01:06:3517#include "cc/animation/animation_id_provider.h"
18#include "cc/animation/scroll_offset_animation_curve.h"
[email protected]95e4e1a02013-03-18 07:09:0919#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4020#include "cc/animation/timing_function.h"
[email protected]6be422b2013-12-08 06:47:3121#include "cc/base/latency_info_swap_promise_monitor.h"
[email protected]681ccff2013-03-18 06:13:5222#include "cc/base/math_util.h"
23#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3324#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2725#include "cc/debug/debug_rect_history.h"
[email protected]dbe759a2013-12-02 19:23:0226#include "cc/debug/devtools_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2727#include "cc/debug/frame_rate_counter.h"
[email protected]6e84de22013-03-18 06:54:2728#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4329#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2230#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2131#include "cc/input/page_scale_animation.h"
32#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5233#include "cc/layers/append_quads_data.h"
34#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2835#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5236#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1737#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2838#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0239#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3040#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4541#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3042#include "cc/output/delegating_renderer.h"
43#include "cc/output/gl_renderer.h"
44#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5645#include "cc/quads/render_pass_draw_quad.h"
46#include "cc/quads/shared_quad_state.h"
47#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1248#include "cc/quads/texture_draw_quad.h"
reveman5ae838bb2014-09-24 23:30:1449#include "cc/resources/bitmap_raster_worker_pool.h"
[email protected]bf050582014-07-24 11:10:5850#include "cc/resources/eviction_tile_priority_queue.h"
[email protected]e1878b52014-07-01 09:21:1751#include "cc/resources/gpu_raster_worker_pool.h"
[email protected]e12dd0e2013-03-18 08:24:4052#include "cc/resources/memory_history.h"
reveman423e7da2014-09-23 16:57:4453#include "cc/resources/one_copy_raster_worker_pool.h"
[email protected]e12dd0e2013-03-18 08:24:4054#include "cc/resources/picture_layer_tiling.h"
[email protected]ec7541d2014-04-09 01:23:5355#include "cc/resources/pixel_buffer_raster_worker_pool.h"
[email protected]e12dd0e2013-03-18 08:24:4056#include "cc/resources/prioritized_resource_manager.h"
[email protected]bf050582014-07-24 11:10:5857#include "cc/resources/raster_tile_priority_queue.h"
[email protected]7beac1b52014-04-10 23:09:5658#include "cc/resources/raster_worker_pool.h"
[email protected]eece11e2014-04-12 03:07:1659#include "cc/resources/resource_pool.h"
[email protected]ea468c6c2013-09-10 08:25:1160#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5761#include "cc/resources/ui_resource_bitmap.h"
reveman423e7da2014-09-23 16:57:4462#include "cc/resources/zero_copy_raster_worker_pool.h"
[email protected]be4655a2013-03-18 08:36:3163#include "cc/scheduler/delay_based_time_source.h"
[email protected]556fd292013-03-18 08:03:0464#include "cc/trees/damage_tracker.h"
65#include "cc/trees/layer_tree_host.h"
66#include "cc/trees/layer_tree_host_common.h"
67#include "cc/trees/layer_tree_impl.h"
[email protected]34ba1ffb2014-03-05 06:55:0368#include "cc/trees/occlusion_tracker.h"
[email protected]556fd292013-03-18 08:03:0469#include "cc/trees/single_thread_proxy.h"
70#include "cc/trees/tree_synchronizer.h"
[email protected]110fcc0e2014-07-16 15:52:4771#include "gpu/command_buffer/client/gles2_interface.h"
[email protected]8d7aa012013-11-23 21:19:1272#include "gpu/GLES2/gl2extchromium.h"
[email protected]de2cf8c2013-10-25 19:46:4673#include "ui/gfx/frame_time.h"
[email protected]bd5324592014-07-31 09:09:3374#include "ui/gfx/geometry/rect_conversions.h"
[email protected]d455d552012-11-02 00:19:0675#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1376#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1477
reveman5ae838bb2014-09-24 23:30:1478namespace cc {
[email protected]94f206c12012-08-25 00:09:1479namespace {
80
reveman5ae838bb2014-09-24 23:30:1481void DidVisibilityChange(LayerTreeHostImpl* id, bool visible) {
[email protected]c1bb5af2013-03-13 19:06:2782 if (visible) {
83 TRACE_EVENT_ASYNC_BEGIN1("webkit",
84 "LayerTreeHostImpl::SetVisible",
85 id,
86 "LayerTreeHostImpl",
87 id);
88 return;
89 }
[email protected]94f206c12012-08-25 00:09:1490
[email protected]c1bb5af2013-03-13 19:06:2791 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1492}
93
reveman5ae838bb2014-09-24 23:30:1494size_t GetMaxTransferBufferUsageBytes(
95 const ContextProvider::Capabilities& context_capabilities,
96 double refresh_rate) {
[email protected]b4664142013-11-08 00:50:3197 // We want to make sure the default transfer buffer size is equal to the
98 // amount of data that can be uploaded by the compositor to avoid stalling
99 // the pipeline.
100 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
101 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
danakje6a236bc2014-09-02 21:27:17102
103 // We need to upload at least enough work to keep the GPU process busy until
104 // the next time it can handle a request to start more uploads from the
105 // compositor. We assume that it will pick up any sent upload requests within
106 // the time of a vsync, since the browser will want to swap a frame within
107 // that time interval, and then uploads should have a chance to be processed.
108 size_t ms_per_frame = std::floor(1000.0 / refresh_rate);
109 size_t max_transfer_buffer_usage_bytes =
110 ms_per_frame * kMaxBytesUploadedPerMs;
111
112 // The context may request a lower limit based on the device capabilities.
reveman5ae838bb2014-09-24 23:30:14113 return std::min(context_capabilities.max_transfer_buffer_usage_bytes,
114 max_transfer_buffer_usage_bytes);
[email protected]b4664142013-11-08 00:50:31115}
116
reveman5ae838bb2014-09-24 23:30:14117unsigned GetMapImageTextureTarget(
118 const ContextProvider::Capabilities& context_capabilities) {
119 if (context_capabilities.gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12120 return GL_TEXTURE_EXTERNAL_OES;
reveman5ae838bb2014-09-24 23:30:14121 if (context_capabilities.gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12122 return GL_TEXTURE_RECTANGLE_ARB;
[email protected]8d7aa012013-11-23 21:19:12123
124 return GL_TEXTURE_2D;
125}
126
revemanc84602a02014-09-24 15:41:25127size_t GetMaxStagingResourceCount() {
128 // Upper bound for number of staging resource to allow.
129 return 32;
130}
131
[email protected]c1bb5af2013-03-13 19:06:27132} // namespace
[email protected]94f206c12012-08-25 00:09:14133
[email protected]96baf3e2012-10-22 23:09:55134class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27135 public:
136 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
137 LayerTreeHostImpl* layer_tree_host_impl,
138 scoped_refptr<DelayBasedTimeSource> time_source) {
139 return make_scoped_ptr(
140 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
141 time_source));
142 }
143 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26144 time_source_->SetClient(NULL);
145 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27146 }
147
[email protected]6d0e69d2013-03-20 14:53:26148 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27149 // In single threaded mode we attempt to simulate changing the current
150 // thread by maintaining a fake thread id. When we switch from one
151 // thread to another, we construct DebugScopedSetXXXThread objects that
152 // update the thread id. This lets DCHECKS that ensure we're on the
153 // right thread to work correctly in single threaded mode. The problem
154 // here is that the timer tasks are run via the message loop, and when
155 // they run, we've had no chance to construct a DebugScopedSetXXXThread
156 // object. The result is that we report that we're running on the main
157 // thread. In multi-threaded mode, this timer is run on the compositor
158 // thread, so to keep this consistent in single-threaded mode, we'll
159 // construct a DebugScopedSetImplThread object. There is no need to do
160 // this in multi-threaded mode since the real thread id's will be
161 // correct. In fact, setting fake thread id's interferes with the real
162 // thread id's and causes breakage.
163 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
164 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
165 set_impl_thread.reset(
166 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14167 }
168
[email protected]fb7425a2013-04-22 16:28:55169 layer_tree_host_impl_->Animate(
[email protected]04c5900d2014-08-18 13:38:36170 layer_tree_host_impl_->CurrentBeginFrameArgs().frame_time);
[email protected]3d9f7432013-04-06 00:35:18171 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
172 bool start_ready_animations = true;
173 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8f7f298822014-06-13 00:23:32174
175 if (layer_tree_host_impl_->pending_tree()) {
176 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
177 layer_tree_host_impl_->ManageTiles();
178 }
179
[email protected]04c5900d2014-08-18 13:38:36180 layer_tree_host_impl_->ResetCurrentBeginFrameArgsForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27181 }
[email protected]373974232013-01-10 22:20:50182
[email protected]c1bb5af2013-03-13 19:06:27183 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26184 if (active != time_source_->Active())
185 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27186 }
[email protected]94f206c12012-08-25 00:09:14187
[email protected]d9fce6722013-08-30 01:10:01188 bool Active() const { return time_source_->Active(); }
189
[email protected]c1bb5af2013-03-13 19:06:27190 private:
191 LayerTreeHostImplTimeSourceAdapter(
192 LayerTreeHostImpl* layer_tree_host_impl,
193 scoped_refptr<DelayBasedTimeSource> time_source)
194 : layer_tree_host_impl_(layer_tree_host_impl),
195 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26196 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27197 }
[email protected]94f206c12012-08-25 00:09:14198
[email protected]c1bb5af2013-03-13 19:06:27199 LayerTreeHostImpl* layer_tree_host_impl_;
200 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14201
[email protected]c1bb5af2013-03-13 19:06:27202 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14203};
204
[email protected]96baf3e2012-10-22 23:09:55205LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20206 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27207
208LayerTreeHostImpl::FrameData::~FrameData() {}
209
210scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
211 const LayerTreeSettings& settings,
212 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43213 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57214 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02215 SharedBitmapManager* manager,
216 int id) {
[email protected]a7f35682013-10-22 23:05:57217 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02218 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10219}
220
[email protected]372bad5f2013-03-21 16:38:43221LayerTreeHostImpl::LayerTreeHostImpl(
222 const LayerTreeSettings& settings,
223 LayerTreeHostImplClient* client,
224 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57225 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02226 SharedBitmapManager* manager,
227 int id)
[email protected]c1bb5af2013-03-13 19:06:27228 : client_(client),
229 proxy_(proxy),
[email protected]13525d62014-05-20 21:22:04230 use_gpu_rasterization_(false),
[email protected]200a9c062013-05-20 04:34:37231 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27232 did_lock_scrolling_layer_(false),
233 should_bubble_scrolls_(false),
234 wheel_scrolling_(false),
[email protected]f161ca9e2014-04-01 13:57:01235 scroll_affects_scroll_handler_(false),
[email protected]f620b0e72013-10-01 21:38:24236 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08237 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47238 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27239 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27240 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07241 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34242 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29243 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07244 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27245 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13246 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36247 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29248 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27249 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02250 debug_rect_history_(DebugRectHistory::Create()),
[email protected]dd909b1f2014-03-18 00:19:52251 texture_mailbox_deleter_(new TextureMailboxDeleter(
252 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
253 : proxy_->MainThreadTaskRunner())),
[email protected]d7626ffd2013-03-29 00:17:42254 max_memory_needed_bytes_(0),
[email protected]50644642013-06-20 13:58:55255 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32256 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12257 overhang_ui_resource_id_(0),
[email protected]c05dfbb2014-07-10 22:49:04258 resourceless_software_draw_(false),
[email protected]c92195e2014-05-07 18:18:49259 begin_impl_frame_interval_(BeginFrameArgs::DefaultInterval()),
[email protected]372bad5f2013-03-21 16:38:43260 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19261 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33262 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57263 need_to_update_visible_tiles_before_draw_(false),
[email protected]dbe759a2013-12-02 19:23:02264 shared_bitmap_manager_(manager),
revemanc84602a02014-09-24 15:41:25265 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27266 DCHECK(proxy_->IsImplThread());
267 DidVisibilityChange(this, visible_);
[email protected]5d8bec72014-07-03 03:03:11268 animation_registrar_->set_supports_scroll_animations(
269 proxy_->SupportsImplScrolling());
[email protected]c1bb5af2013-03-13 19:06:27270
[email protected]8e0176d2013-03-21 03:14:52271 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27272
bokan55b2f152014-09-15 14:47:59273 // LTHI always has an active tree.
274 active_tree_ = LayerTreeImpl::create(this);
275 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
276 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", id_);
277
[email protected]8e0176d2013-03-21 03:14:52278 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27279 top_controls_manager_ =
280 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52281 settings.top_controls_height,
282 settings.top_controls_show_threshold,
283 settings.top_controls_hide_threshold);
bokan0da0ba8b2014-09-25 19:17:26284
285 // TODO(bokan): This is a quick fix. The browser should lock the top
286 // controls to shown on creation but this appears not to work. Tracked
287 // in crbug.com/417680.
288 // Initialize with top controls showing.
289 SetControlsTopOffset(0.f);
[email protected]c1bb5af2013-03-13 19:06:27290 }
[email protected]493067512012-09-19 23:34:10291}
292
[email protected]c1bb5af2013-03-13 19:06:27293LayerTreeHostImpl::~LayerTreeHostImpl() {
294 DCHECK(proxy_->IsImplThread());
295 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51296 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
[email protected]a75441c2014-05-16 19:10:29297 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", id_);
[email protected]c1bb5af2013-03-13 19:06:27298
[email protected]200a9c062013-05-20 04:34:37299 if (input_handler_client_) {
300 input_handler_client_->WillShutdown();
301 input_handler_client_ = NULL;
302 }
303
[email protected]75deb742013-06-24 20:19:18304 // The layer trees must be destroyed before the layer tree host. We've
305 // made a contract with our animation controllers that the registrar
306 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40307 if (recycle_tree_)
308 recycle_tree_->Shutdown();
309 if (pending_tree_)
310 pending_tree_->Shutdown();
311 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18312 recycle_tree_.reset();
313 pending_tree_.reset();
314 active_tree_.reset();
[email protected]ced667b2014-05-22 21:49:53315 DestroyTileManager();
[email protected]94f206c12012-08-25 00:09:14316}
317
[email protected]097a0cbf2013-12-12 17:04:58318void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
319 // If the begin frame data was handled, then scroll and scale set was applied
320 // by the main thread, so the active tree needs to be updated as if these sent
321 // values were applied and committed.
322 if (did_handle) {
323 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
324 active_tree_->ResetContentsTexturesPurged();
325 }
326}
327
[email protected]12a63da2014-06-13 06:06:22328void LayerTreeHostImpl::BeginCommit() {
329 TRACE_EVENT0("cc", "LayerTreeHostImpl::BeginCommit");
330
[email protected]fc20d142014-07-01 00:49:15331 if (UsePendingTreeForSync())
[email protected]12a63da2014-06-13 06:06:22332 CreatePendingTree();
333}
[email protected]3b31c6ac2012-12-06 21:27:29334
[email protected]c1bb5af2013-03-13 19:06:27335void LayerTreeHostImpl::CommitComplete() {
336 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08337
[email protected]fc20d142014-07-01 00:49:15338 if (pending_tree_)
339 pending_tree_->ApplyScrollDeltasSinceBeginMainFrame();
340 sync_tree()->set_needs_update_draw_properties();
341
[email protected]8e0176d2013-03-21 03:14:52342 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25343 // Impl-side painting needs an update immediately post-commit to have the
344 // opportunity to create tilings. Other paths can call UpdateDrawProperties
345 // more lazily when needed prior to drawing.
[email protected]fc20d142014-07-01 00:49:15346 sync_tree()->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26347 // Start working on newly created tiles immediately if needed.
[email protected]fc20d142014-07-01 00:49:15348 if (tile_manager_ && tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38349 ManageTiles();
[email protected]fc20d142014-07-01 00:49:15350 else
351 NotifyReadyToActivate();
[email protected]c1bb5af2013-03-13 19:06:27352 } else {
[email protected]12a63da2014-06-13 06:06:22353 // If we're not in impl-side painting, the tree is immediately considered
354 // active.
[email protected]fc20d142014-07-01 00:49:15355 ActivateSyncTree();
[email protected]c1bb5af2013-03-13 19:06:27356 }
[email protected]3ba4cae2013-01-16 03:58:38357
[email protected]5e5648a2013-11-18 00:39:33358 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14359}
360
[email protected]6133cc232013-07-30 18:47:07361bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27362 // Note: If you are changing this function or any other function that might
363 // affect the result of CanDraw, make sure to call
364 // client_->OnCanDrawStateChanged in the proper places and update the
365 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14366
[email protected]6133cc232013-07-30 18:47:07367 if (!renderer_) {
368 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
369 TRACE_EVENT_SCOPE_THREAD);
370 return false;
371 }
372
373 // Must have an OutputSurface if |renderer_| is not NULL.
374 DCHECK(output_surface_);
375
376 // TODO(boliu): Make draws without root_layer work and move this below
377 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27378 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42379 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
380 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22381 return false;
[email protected]c1bb5af2013-03-13 19:06:27382 }
[email protected]6133cc232013-07-30 18:47:07383
384 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
385 return true;
386
[email protected]54af03522013-09-05 00:43:28387 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42388 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
389 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27390 return false;
391 }
392 if (active_tree_->ViewportSizeInvalid()) {
393 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42394 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
395 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27396 return false;
397 }
[email protected]c1bb5af2013-03-13 19:06:27398 if (active_tree_->ContentsTexturesPurged()) {
399 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42400 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
401 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27402 return false;
403 }
[email protected]127bdc1a2013-09-11 01:44:48404 if (EvictedUIResourcesExist()) {
405 TRACE_EVENT_INSTANT0(
406 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
407 TRACE_EVENT_SCOPE_THREAD);
408 return false;
409 }
[email protected]c1bb5af2013-03-13 19:06:27410 return true;
[email protected]2f1acc262012-11-16 21:42:22411}
412
[email protected]27152642014-03-11 20:42:00413void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time) {
[email protected]200a9c062013-05-20 04:34:37414 if (input_handler_client_)
415 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27416 AnimatePageScale(monotonic_time);
[email protected]27152642014-03-11 20:42:00417 AnimateLayers(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27418 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37419 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14420}
421
[email protected]c1bb5af2013-03-13 19:06:27422void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26423 if (!tile_manager_)
424 return;
[email protected]c48536a52013-09-14 00:02:08425 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26426 return;
[email protected]54af03522013-09-05 00:43:28427
[email protected]c48536a52013-09-14 00:02:08428 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19429 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27430
[email protected]99cc6b42014-01-17 00:12:57431 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]587941d2014-08-22 01:40:01444 gfx::SizeF viewport_size =
445 active_tree_->InnerViewportContainerLayer()->bounds();
[email protected]c1bb5af2013-03-13 19:06:27446
[email protected]85d136f782013-04-26 22:04:40447 // Easing constants experimentally determined.
448 scoped_ptr<TimingFunction> timing_function =
449 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
450
[email protected]c1bb5af2013-03-13 19:06:27451 page_scale_animation_ =
452 PageScaleAnimation::Create(scroll_total,
453 active_tree_->total_page_scale_factor(),
454 viewport_size,
455 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40456 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27457
458 if (anchor_point) {
459 gfx::Vector2dF anchor(target_offset);
460 page_scale_animation_->ZoomWithAnchor(anchor,
461 page_scale,
462 duration.InSecondsF());
463 } else {
464 gfx::Vector2dF scaled_target_offset = target_offset;
465 page_scale_animation_->ZoomTo(scaled_target_offset,
466 page_scale,
467 duration.InSecondsF());
468 }
469
[email protected]43b8f982014-04-30 21:24:33470 SetNeedsAnimate();
[email protected]c1bb5af2013-03-13 19:06:27471 client_->SetNeedsCommitOnImplThread();
472 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15473}
474
[email protected]edcc1a12014-05-06 01:26:39475bool LayerTreeHostImpl::IsCurrentlyScrollingLayerAt(
476 const gfx::Point& viewport_point,
477 InputHandler::ScrollInputType type) {
478 if (!CurrentlyScrollingLayer())
479 return false;
480
[email protected]edcc1a12014-05-06 01:26:39481 gfx::PointF device_viewport_point =
482 gfx::ScalePoint(viewport_point, device_scale_factor_);
483
[email protected]28336d52014-05-12 19:07:28484 LayerImpl* layer_impl =
485 active_tree_->FindLayerThatIsHitByPoint(device_viewport_point);
[email protected]edcc1a12014-05-06 01:26:39486
487 bool scroll_on_main_thread = false;
488 LayerImpl* scrolling_layer_impl = FindScrollLayerForDeviceViewportPoint(
489 device_viewport_point, type, layer_impl, &scroll_on_main_thread, NULL);
490 return CurrentlyScrollingLayer() == scrolling_layer_impl;
491}
492
[email protected]47a723f2014-03-05 12:42:49493bool LayerTreeHostImpl::HaveTouchEventHandlersAt(
494 const gfx::Point& viewport_point) {
[email protected]f57bbc02012-11-21 07:02:15495
[email protected]c1bb5af2013-03-13 19:06:27496 gfx::PointF device_viewport_point =
497 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15498
[email protected]c974d5d2013-10-24 01:02:48499 LayerImpl* layer_impl =
[email protected]28336d52014-05-12 19:07:28500 active_tree_->FindLayerThatIsHitByPointInTouchHandlerRegion(
501 device_viewport_point);
502
[email protected]c974d5d2013-10-24 01:02:48503 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27504}
505
[email protected]6be422b2013-12-08 06:47:31506scoped_ptr<SwapPromiseMonitor>
507LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
508 ui::LatencyInfo* latency) {
509 return scoped_ptr<SwapPromiseMonitor>(
510 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08511}
512
[email protected]dab0a422014-08-13 16:09:46513void LayerTreeHostImpl::QueueSwapPromiseForMainThreadScrollUpdate(
514 scoped_ptr<SwapPromise> swap_promise) {
515 swap_promises_for_main_thread_scroll_update_.push_back(swap_promise.Pass());
516}
517
[email protected]c1bb5af2013-03-13 19:06:27518void LayerTreeHostImpl::TrackDamageForAllSurfaces(
519 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28520 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27521 // For now, we use damage tracking to compute a global scissor. To do this, we
522 // must compute all damage tracking before drawing anything, so that we know
523 // the root damage rect. The root damage rect is then used to scissor each
524 // surface.
525
526 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06527 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27528 --surface_index) {
529 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
530 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
531 DCHECK(render_surface);
532 render_surface->damage_tracker()->UpdateDamageTrackingState(
533 render_surface->layer_list(),
534 render_surface_layer->id(),
535 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
536 render_surface->content_rect(),
537 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48538 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27539 }
540}
541
[email protected]d12aa932014-08-01 13:10:38542void LayerTreeHostImpl::FrameData::AsValueInto(
543 base::debug::TracedValue* value) const {
[email protected]34806722013-08-09 23:51:21544 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
545 value->SetBoolean("has_no_damage", has_no_damage);
546
547 // Quad data can be quite large, so only dump render passes if we select
548 // cc.debug.quads.
549 bool quads_enabled;
550 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
551 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
552 if (quads_enabled) {
[email protected]d12aa932014-08-01 13:10:38553 value->BeginArray("render_passes");
554 for (size_t i = 0; i < render_passes.size(); ++i) {
555 value->BeginDictionary();
556 render_passes[i]->AsValueInto(value);
557 value->EndDictionary();
558 }
559 value->EndArray();
[email protected]34806722013-08-09 23:51:21560 }
[email protected]34806722013-08-09 23:51:21561}
562
[email protected]c1bb5af2013-03-13 19:06:27563void LayerTreeHostImpl::FrameData::AppendRenderPass(
564 scoped_ptr<RenderPass> render_pass) {
565 render_passes_by_id[render_pass->id] = render_pass.get();
566 render_passes.push_back(render_pass.Pass());
567}
568
[email protected]c05dfbb2014-07-10 22:49:04569DrawMode LayerTreeHostImpl::GetDrawMode() const {
570 if (resourceless_software_draw_) {
[email protected]ffbb2212013-06-02 23:47:59571 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]c05dfbb2014-07-10 22:49:04572 } else if (output_surface_->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59573 return DRAW_MODE_HARDWARE;
574 } else {
[email protected]c05dfbb2014-07-10 22:49:04575 DCHECK_EQ(!output_surface_->software_device(),
576 output_surface_->capabilities().delegated_rendering &&
577 !output_surface_->capabilities().deferred_gl_initialization)
578 << output_surface_->capabilities().delegated_rendering << " "
579 << output_surface_->capabilities().deferred_gl_initialization;
[email protected]ffbb2212013-06-02 23:47:59580 return DRAW_MODE_SOFTWARE;
581 }
582}
583
[email protected]34ba1ffb2014-03-05 06:55:03584static void AppendQuadsForLayer(
585 RenderPass* target_render_pass,
586 LayerImpl* layer,
587 const OcclusionTracker<LayerImpl>& occlusion_tracker,
588 AppendQuadsData* append_quads_data) {
[email protected]c6707fd2014-06-23 05:50:36589 layer->AppendQuads(target_render_pass, occlusion_tracker, append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27590}
591
592static void AppendQuadsForRenderSurfaceLayer(
593 RenderPass* target_render_pass,
594 LayerImpl* layer,
595 const RenderPass* contributing_render_pass,
[email protected]34ba1ffb2014-03-05 06:55:03596 const OcclusionTracker<LayerImpl>& occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27597 AppendQuadsData* append_quads_data) {
[email protected]c1bb5af2013-03-13 19:06:27598 bool is_replica = false;
[email protected]c6707fd2014-06-23 05:50:36599 layer->render_surface()->AppendQuads(target_render_pass,
600 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27601 append_quads_data,
602 is_replica,
603 contributing_render_pass->id);
604
605 // Add replica after the surface so that it appears below the surface.
606 if (layer->has_replica()) {
607 is_replica = true;
[email protected]c6707fd2014-06-23 05:50:36608 layer->render_surface()->AppendQuads(target_render_pass,
609 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27610 append_quads_data,
611 is_replica,
612 contributing_render_pass->id);
613 }
614}
615
616static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08617 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20618 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06619 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27620 RenderPass* target_render_pass,
621 LayerImpl* root_layer,
622 SkColor screen_background_color,
[email protected]34ba1ffb2014-03-05 06:55:03623 const OcclusionTracker<LayerImpl>& occlusion_tracker) {
[email protected]c1bb5af2013-03-13 19:06:27624 if (!root_layer || !SkColorGetA(screen_background_color))
625 return;
626
627 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
628 if (fill_region.IsEmpty())
629 return;
630
[email protected]3744e27b2013-11-06 21:44:08631 // Divide the fill region into the part to be filled with the overhang
632 // resource and the part to be filled with the background color.
633 Region screen_background_color_region = fill_region;
634 Region overhang_region;
635 if (overhang_resource_id) {
636 overhang_region = fill_region;
637 overhang_region.Subtract(root_scroll_layer_rect);
638 screen_background_color_region.Intersect(root_scroll_layer_rect);
639 }
640
[email protected]c1bb5af2013-03-13 19:06:27641 // 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;
[email protected]a9d4d4f2014-06-19 06:49:28648 int sorting_context_id = 0;
[email protected]c6707fd2014-06-23 05:50:36649 SharedQuadState* shared_quad_state =
650 target_render_pass->CreateAndAppendSharedQuadState();
[email protected]7785e1002014-02-20 21:04:32651 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27652 root_target_rect.size(),
653 root_target_rect,
654 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01655 false,
[email protected]7bbeaf4e2013-11-26 10:27:22656 opacity,
[email protected]a9d4d4f2014-06-19 06:49:28657 SkXfermode::kSrcOver_Mode,
658 sorting_context_id);
[email protected]f57bbc02012-11-21 07:02:15659
[email protected]3744e27b2013-11-06 21:44:08660 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27661 fill_rects.has_rect();
662 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32663 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24664 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08665 // Skip the quad culler and just append the quads directly to avoid
666 // occlusion checks.
[email protected]f7030c32014-07-03 18:54:34667 SolidColorDrawQuad* quad =
668 target_render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
[email protected]9bf06c72014-03-07 18:16:24669 quad->SetNew(shared_quad_state,
670 screen_space_rect,
671 visible_screen_space_rect,
672 screen_background_color,
673 false);
[email protected]3744e27b2013-11-06 21:44:08674 }
675 for (Region::Iterator fill_rects(overhang_region);
676 fill_rects.has_rect();
677 fill_rects.next()) {
678 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32679 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24680 gfx::Rect opaque_screen_space_rect = screen_space_rect;
681 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]f7030c32014-07-03 18:54:34682 TextureDrawQuad* tex_quad =
683 target_render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
[email protected]3744e27b2013-11-06 21:44:08684 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
685 tex_quad->SetNew(
686 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32687 screen_space_rect,
[email protected]9bf06c72014-03-07 18:16:24688 opaque_screen_space_rect,
689 visible_screen_space_rect,
[email protected]3744e27b2013-11-06 21:44:08690 overhang_resource_id,
691 false,
[email protected]7785e1002014-02-20 21:04:32692 gfx::PointF(
693 screen_space_rect.x() / overhang_resource_scaled_size.width(),
694 screen_space_rect.y() / overhang_resource_scaled_size.height()),
695 gfx::PointF(
696 screen_space_rect.right() / overhang_resource_scaled_size.width(),
697 screen_space_rect.bottom() /
698 overhang_resource_scaled_size.height()),
[email protected]3744e27b2013-11-06 21:44:08699 screen_background_color,
700 vertex_opacity,
701 false);
[email protected]c1bb5af2013-03-13 19:06:27702 }
[email protected]467b3612012-08-28 07:41:16703}
704
[email protected]30d82142014-05-12 04:26:02705DrawResult LayerTreeHostImpl::CalculateRenderPasses(
[email protected]7ddfe7e82014-01-30 07:22:11706 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27707 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11708 DCHECK(CanDraw());
709 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32710
[email protected]c1bb5af2013-03-13 19:06:27711 TrackDamageForAllSurfaces(active_tree_->root_layer(),
712 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14713
[email protected]e0341352013-04-06 05:01:20714 // If the root render surface has no visible damage, then don't generate a
715 // frame at all.
716 RenderSurfaceImpl* root_surface =
717 active_tree_->root_layer()->render_surface();
718 bool root_surface_has_no_visible_damage =
719 !root_surface->damage_tracker()->current_damage_rect().Intersects(
720 root_surface->content_rect());
721 bool root_surface_has_contributing_layers =
722 !root_surface->layer_list().empty();
[email protected]5fda7f32014-03-12 20:44:29723 bool hud_wants_to_draw_ = active_tree_->hud_layer() &&
724 active_tree_->hud_layer()->IsAnimatingHUDContents();
[email protected]e0341352013-04-06 05:01:20725 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23726 root_surface_has_no_visible_damage &&
[email protected]5fda7f32014-03-12 20:44:29727 active_tree_->LayersWithCopyOutputRequest().empty() &&
728 !hud_wants_to_draw_) {
[email protected]e0341352013-04-06 05:01:20729 TRACE_EVENT0("cc",
730 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
731 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07732 DCHECK(!output_surface_->capabilities()
733 .draw_and_swap_full_viewport_every_frame);
[email protected]30d82142014-05-12 04:26:02734 return DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20735 }
736
[email protected]c1bb5af2013-03-13 19:06:27737 TRACE_EVENT1("cc",
738 "LayerTreeHostImpl::CalculateRenderPasses",
739 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06740 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14741
[email protected]c1bb5af2013-03-13 19:06:27742 // Create the render passes in dependency order.
743 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06744 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27745 --surface_index) {
746 LayerImpl* render_surface_layer =
747 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45748 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
749
750 bool should_draw_into_render_pass =
751 render_surface_layer->parent() == NULL ||
752 render_surface->contributes_to_drawn_surface() ||
753 render_surface_layer->HasCopyRequest();
754 if (should_draw_into_render_pass)
755 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27756 }
[email protected]94f206c12012-08-25 00:09:14757
[email protected]f636ce82014-03-10 22:33:08758 // When we are displaying the HUD, change the root damage rect to cover the
759 // entire root surface. This will disable partial-swap/scissor optimizations
760 // that would prevent the HUD from updating, since the HUD does not cause
761 // damage itself, to prevent it from messing with damage visualizations. Since
762 // damage visualizations are done off the LayerImpls and RenderSurfaceImpls,
763 // changing the RenderPass does not affect them.
764 if (active_tree_->hud_layer()) {
765 RenderPass* root_pass = frame->render_passes.back();
766 root_pass->damage_rect = root_pass->output_rect;
767 }
768
[email protected]34ba1ffb2014-03-05 06:55:03769 OcclusionTracker<LayerImpl> occlusion_tracker(
[email protected]97c6a7342014-03-12 20:36:43770 active_tree_->root_layer()->render_surface()->content_rect());
[email protected]c1bb5af2013-03-13 19:06:27771 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52772 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14773
[email protected]846f455b2013-03-18 19:07:41774 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27775 occlusion_tracker.set_occluding_screen_space_rects_container(
776 &frame->occluding_screen_space_rects);
777 }
[email protected]846f455b2013-03-18 19:07:41778 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27779 occlusion_tracker.set_non_occluding_screen_space_rects_container(
780 &frame->non_occluding_screen_space_rects);
781 }
[email protected]94f206c12012-08-25 00:09:14782
[email protected]ba1b33e2014-02-28 16:44:51783 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27784 // occlusion and performing culling during the tree walk.
[email protected]ba1b33e2014-02-28 16:44:51785 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14786
[email protected]c1bb5af2013-03-13 19:06:27787 // Typically when we are missing a texture and use a checkerboard quad, we
788 // still draw the frame. However when the layer being checkerboarded is moving
789 // due to an impl-animation, we drop the frame to avoid flashing due to the
790 // texture suddenly appearing in the future.
[email protected]30d82142014-05-12 04:26:02791 DrawResult draw_result = DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25792 // When we have a copy request for a layer, we need to draw no matter
793 // what, as the layer may disappear after this frame.
794 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14795
[email protected]372bad5f2013-03-21 16:38:43796 int layers_drawn = 0;
797
[email protected]c05dfbb2014-07-10 22:49:04798 const DrawMode draw_mode = GetDrawMode();
[email protected]ffbb2212013-06-02 23:47:59799
[email protected]76ca89b2014-07-14 20:32:05800 int num_missing_tiles = 0;
801 int num_incomplete_tiles = 0;
802
[email protected]c1bb5af2013-03-13 19:06:27803 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09804 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27805 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09806 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27807 it != end;
808 ++it) {
[email protected]0cd7d6f72014-08-22 14:50:51809 RenderPassId target_render_pass_id =
810 it.target_render_surface_layer()->render_surface()->GetRenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27811 RenderPass* target_render_pass =
812 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14813
[email protected]e47b0a0a2013-11-18 23:26:22814 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14815
[email protected]30fe19ff2013-07-04 00:54:45816 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30817
[email protected]18a70192013-04-26 16:18:25818 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36819 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25820 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11821 it->TakeCopyRequestsAndTransformToTarget(
822 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25823 }
[email protected]30fe19ff2013-07-04 00:54:45824 } else if (it.represents_contributing_render_surface() &&
825 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]0cd7d6f72014-08-22 14:50:51826 RenderPassId contributing_render_pass_id =
827 it->render_surface()->GetRenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27828 RenderPass* contributing_render_pass =
829 frame->render_passes_by_id[contributing_render_pass_id];
830 AppendQuadsForRenderSurfaceLayer(target_render_pass,
831 *it,
832 contributing_render_pass,
833 occlusion_tracker,
834 &append_quads_data);
[email protected]8d964342014-05-15 18:22:27835 } else if (it.represents_itself() &&
[email protected]e1e768f2013-03-26 08:48:09836 !it->visible_content_rect().IsEmpty()) {
vmpstr01a5bf352014-09-17 22:57:31837 bool occluded =
838 occlusion_tracker.GetCurrentOcclusionForLayer(it->draw_transform())
839 .IsOccluded(it->visible_content_rect());
[email protected]fbc293322013-10-01 05:07:15840 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27841 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59842
[email protected]c1bb5af2013-03-13 19:06:27843 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57844
[email protected]c1bb5af2013-03-13 19:06:27845 if (it->HasContributingDelegatedRenderPasses()) {
[email protected]0cd7d6f72014-08-22 14:50:51846 RenderPassId contributing_render_pass_id =
[email protected]c1bb5af2013-03-13 19:06:27847 it->FirstContributingRenderPassId();
848 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
849 frame->render_passes_by_id.end()) {
850 RenderPass* render_pass =
851 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14852
[email protected]c1bb5af2013-03-13 19:06:27853 AppendQuadsData append_quads_data(render_pass->id);
854 AppendQuadsForLayer(render_pass,
855 *it,
856 occlusion_tracker,
857 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57858
[email protected]c1bb5af2013-03-13 19:06:27859 contributing_render_pass_id =
860 it->NextContributingRenderPassId(contributing_render_pass_id);
861 }
[email protected]94f206c12012-08-25 00:09:14862 }
863
[email protected]c1bb5af2013-03-13 19:06:27864 AppendQuadsForLayer(target_render_pass,
865 *it,
866 occlusion_tracker,
867 &append_quads_data);
868 }
[email protected]89228202012-08-29 03:20:30869
[email protected]372bad5f2013-03-21 16:38:43870 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14871 }
872
[email protected]2bc19e532014-04-30 05:29:22873 rendering_stats_instrumentation_->AddVisibleContentArea(
874 append_quads_data.visible_content_area);
875 rendering_stats_instrumentation_->AddApproximatedVisibleContentArea(
876 append_quads_data.approximated_visible_content_area);
877
[email protected]76ca89b2014-07-14 20:32:05878 num_missing_tiles += append_quads_data.num_missing_tiles;
879 num_incomplete_tiles += append_quads_data.num_incomplete_tiles;
880
[email protected]e1e768f2013-03-26 08:48:09881 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27882 bool layer_has_animating_transform =
883 it->screen_space_transform_is_animating() ||
884 it->draw_transform_is_animating();
[email protected]30d82142014-05-12 04:26:02885 if (layer_has_animating_transform)
886 draw_result = DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
[email protected]c1bb5af2013-03-13 19:06:27887 }
888
[email protected]76ca89b2014-07-14 20:32:05889 if (append_quads_data.num_incomplete_tiles ||
890 append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27891 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23892 if (active_tree()->RequiresHighResToDraw())
[email protected]30d82142014-05-12 04:26:02893 draw_result = DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
[email protected]3d609bb2014-02-01 01:10:23894 }
[email protected]c1bb5af2013-03-13 19:06:27895
896 occlusion_tracker.LeaveLayer(it);
897 }
898
[email protected]6133cc232013-07-30 18:47:07899 if (have_copy_request ||
900 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]30d82142014-05-12 04:26:02901 draw_result = DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25902
[email protected]767f38d72014-03-18 21:26:41903#if DCHECK_IS_ON
[email protected]c1bb5af2013-03-13 19:06:27904 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
905 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
906 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
907 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
908 != frame->render_passes_by_id.end());
909 }
[email protected]94f206c12012-08-25 00:09:14910#endif
[email protected]c1bb5af2013-03-13 19:06:27911 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14912
[email protected]c1bb5af2013-03-13 19:06:27913 if (!active_tree_->has_transparent_background()) {
914 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08915 AppendQuadsToFillScreen(
916 ResourceIdForUIResource(overhang_ui_resource_id_),
917 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
918 active_tree_->RootScrollLayerDeviceViewportBounds(),
919 frame->render_passes.back(),
920 active_tree_->root_layer(),
921 active_tree_->background_color(),
922 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27923 }
[email protected]94f206c12012-08-25 00:09:14924
[email protected]c1bb5af2013-03-13 19:06:27925 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48926 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14927
[email protected]30fe19ff2013-07-04 00:54:45928 // Any copy requests left in the tree are not going to get serviced, and
929 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11930 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
931 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
932 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
933 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45934 }
[email protected]19334ac2013-12-12 23:59:11935 for (size_t i = 0; i < requests_to_abort.size(); ++i)
936 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45937
[email protected]e0341352013-04-06 05:01:20938 // If we're making a frame to draw, it better have at least one render pass.
939 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48940
[email protected]759dc9f2014-07-23 19:18:51941 if (active_tree_->has_ever_been_drawn()) {
[email protected]76ca89b2014-07-14 20:32:05942 UMA_HISTOGRAM_COUNTS_100(
943 "Compositing.RenderPass.AppendQuadData.NumMissingTiles",
944 num_missing_tiles);
945 UMA_HISTOGRAM_COUNTS_100(
946 "Compositing.RenderPass.AppendQuadData.NumIncompleteTiles",
947 num_incomplete_tiles);
948 }
949
[email protected]45948712013-09-27 02:46:48950 // Should only have one render pass in resourceless software mode.
[email protected]c05dfbb2014-07-10 22:49:04951 DCHECK(draw_mode != DRAW_MODE_RESOURCELESS_SOFTWARE ||
952 frame->render_passes.size() == 1u)
953 << frame->render_passes.size();
[email protected]45948712013-09-27 02:46:48954
[email protected]7ddfe7e82014-01-30 07:22:11955 return draw_result;
[email protected]94f206c12012-08-25 00:09:14956}
957
[email protected]200a9c062013-05-20 04:34:37958void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
959 if (input_handler_client_)
960 input_handler_client_->MainThreadHasStoppedFlinging();
961}
962
[email protected]3d9f7432013-04-06 00:35:18963void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
964 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15965 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01966 if (should_background_tick)
967 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15968
[email protected]cd77bb62014-03-27 05:16:36969 bool enabled = should_background_tick && needs_animate_layers();
[email protected]3d9f7432013-04-06 00:35:18970
[email protected]c1bb5af2013-03-13 19:06:27971 // Lazily create the time_source adapter so that we can vary the interval for
972 // testing.
973 if (!time_source_client_adapter_) {
974 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
975 this,
[email protected]810d40b72013-06-20 18:26:15976 DelayBasedTimeSource::Create(
977 LowFrequencyAnimationInterval(),
978 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
979 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27980 }
[email protected]94f206c12012-08-25 00:09:14981
[email protected]c1bb5af2013-03-13 19:06:27982 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14983}
984
[email protected]b8384e22013-12-03 02:20:48985void LayerTreeHostImpl::DidAnimateScrollOffset() {
986 client_->SetNeedsCommitOnImplThread();
987 client_->RenewTreePriority();
988}
989
[email protected]0023fc72014-01-10 20:05:06990void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02991 viewport_damage_rect_.Union(damage_rect);
992}
993
[email protected]c1bb5af2013-03-13 19:06:27994static inline RenderPass* FindRenderPassById(
[email protected]0cd7d6f72014-08-22 14:50:51995 RenderPassId render_pass_id,
[email protected]c1bb5af2013-03-13 19:06:27996 const LayerTreeHostImpl::FrameData& frame) {
997 RenderPassIdHashMap::const_iterator it =
998 frame.render_passes_by_id.find(render_pass_id);
999 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:141000}
1001
[email protected]0cd7d6f72014-08-22 14:50:511002static void RemoveRenderPassesRecursive(RenderPassId remove_render_pass_id,
[email protected]c1bb5af2013-03-13 19:06:271003 LayerTreeHostImpl::FrameData* frame) {
1004 RenderPass* remove_render_pass =
1005 FindRenderPassById(remove_render_pass_id, *frame);
1006 // The pass was already removed by another quad - probably the original, and
1007 // we are the replica.
1008 if (!remove_render_pass)
1009 return;
1010 RenderPassList& render_passes = frame->render_passes;
1011 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
1012 render_passes.end(),
1013 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:141014
[email protected]c1bb5af2013-03-13 19:06:271015 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:141016
[email protected]c1bb5af2013-03-13 19:06:271017 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
1018 frame->render_passes.erase(to_remove);
1019 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:141020
[email protected]c1bb5af2013-03-13 19:06:271021 // Now follow up for all RenderPass quads and remove their RenderPasses
1022 // recursively.
1023 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291024 QuadList::ConstBackToFrontIterator quad_list_iterator =
1025 quad_list.BackToFrontBegin();
1026 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271027 ++quad_list_iterator) {
1028 DrawQuad* current_quad = (*quad_list_iterator);
1029 if (current_quad->material != DrawQuad::RENDER_PASS)
1030 continue;
[email protected]94f206c12012-08-25 00:09:141031
[email protected]0cd7d6f72014-08-22 14:50:511032 RenderPassId next_remove_render_pass_id =
[email protected]c1bb5af2013-03-13 19:06:271033 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1034 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1035 }
[email protected]94f206c12012-08-25 00:09:141036}
1037
[email protected]c1bb5af2013-03-13 19:06:271038bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1039 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1040 const RenderPass* render_pass =
1041 FindRenderPassById(quad.render_pass_id, frame);
1042 if (!render_pass)
1043 return false;
[email protected]94f206c12012-08-25 00:09:141044
[email protected]c1bb5af2013-03-13 19:06:271045 // If any quad or RenderPass draws into this RenderPass, then keep it.
1046 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291047 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1048 quad_list.BackToFrontBegin();
1049 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271050 ++quad_list_iterator) {
1051 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141052
[email protected]c1bb5af2013-03-13 19:06:271053 if (current_quad->material != DrawQuad::RENDER_PASS)
1054 return false;
[email protected]94f206c12012-08-25 00:09:141055
[email protected]c1bb5af2013-03-13 19:06:271056 const RenderPass* contributing_pass = FindRenderPassById(
1057 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1058 if (contributing_pass)
1059 return false;
1060 }
1061 return true;
[email protected]94f206c12012-08-25 00:09:141062}
1063
1064// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271065template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271066 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1067 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141068
1069// static
[email protected]c1bb5af2013-03-13 19:06:271070template <typename RenderPassCuller>
1071void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1072 FrameData* frame) {
1073 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1074 it != culler.RenderPassListEnd(frame->render_passes);
1075 it = culler.RenderPassListNext(it)) {
1076 const RenderPass* current_pass = frame->render_passes[it];
1077 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291078 QuadList::ConstBackToFrontIterator quad_list_iterator =
1079 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141080
[email protected]ed511b8d2013-03-25 03:29:291081 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271082 ++quad_list_iterator) {
1083 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141084
[email protected]c1bb5af2013-03-13 19:06:271085 if (current_quad->material != DrawQuad::RENDER_PASS)
1086 continue;
[email protected]94f206c12012-08-25 00:09:141087
[email protected]c1bb5af2013-03-13 19:06:271088 const RenderPassDrawQuad* render_pass_quad =
1089 RenderPassDrawQuad::MaterialCast(current_quad);
1090 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1091 continue;
[email protected]94f206c12012-08-25 00:09:141092
[email protected]c1bb5af2013-03-13 19:06:271093 // We are changing the vector in the middle of iteration. Because we
1094 // delete render passes that draw into the current pass, we are
1095 // guaranteed that any data from the iterator to the end will not
1096 // change. So, capture the iterator position from the end of the
1097 // list, and restore it after the change.
1098 size_t position_from_end = frame->render_passes.size() - it;
1099 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1100 it = frame->render_passes.size() - position_from_end;
1101 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141102 }
[email protected]c1bb5af2013-03-13 19:06:271103 }
[email protected]94f206c12012-08-25 00:09:141104}
1105
[email protected]2aae96792014-05-15 23:10:501106DrawResult LayerTreeHostImpl::PrepareToDraw(FrameData* frame) {
[email protected]7a52f43e2013-07-10 01:58:471107 TRACE_EVENT1("cc",
1108 "LayerTreeHostImpl::PrepareToDraw",
1109 "SourceFrameNumber",
1110 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141111
[email protected]425e5332013-09-21 04:10:031112 if (need_to_update_visible_tiles_before_draw_ &&
1113 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1114 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191115 }
[email protected]425e5332013-09-21 04:10:031116 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191117
[email protected]aebf4622014-07-14 16:57:591118 UMA_HISTOGRAM_CUSTOM_COUNTS(
1119 "Compositing.NumActiveLayers", active_tree_->NumLayers(), 1, 400, 20);
1120
[email protected]8f7f298822014-06-13 00:23:321121 bool ok = active_tree_->UpdateDrawProperties();
1122 DCHECK(ok) << "UpdateDrawProperties failed during draw";
[email protected]2e7ca422012-12-20 02:57:271123
[email protected]c1bb5af2013-03-13 19:06:271124 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1125 frame->render_passes.clear();
1126 frame->render_passes_by_id.clear();
1127 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201128 frame->contains_incomplete_tile = false;
1129 frame->has_no_damage = false;
1130
1131 if (active_tree_->root_layer()) {
[email protected]2aae96792014-05-15 23:10:501132 gfx::Rect device_viewport_damage_rect = viewport_damage_rect_;
[email protected]878705be2013-04-15 22:44:021133 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201134
1135 active_tree_->root_layer()->render_surface()->damage_tracker()->
1136 AddDamageNextUpdate(device_viewport_damage_rect);
1137 }
[email protected]94f206c12012-08-25 00:09:141138
[email protected]30d82142014-05-12 04:26:021139 DrawResult draw_result = CalculateRenderPasses(frame);
1140 if (draw_result != DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071141 DCHECK(!output_surface_->capabilities()
1142 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111143 return draw_result;
[email protected]6133cc232013-07-30 18:47:071144 }
[email protected]94f206c12012-08-25 00:09:141145
[email protected]7ddfe7e82014-01-30 07:22:111146 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1147 // this function is called again.
1148 return draw_result;
[email protected]94f206c12012-08-25 00:09:141149}
1150
[email protected]0309ba5e02013-06-26 04:11:081151void LayerTreeHostImpl::EvictTexturesForTesting() {
1152 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1153}
1154
[email protected]4f48f6e2013-08-27 06:33:381155void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1156 NOTREACHED();
1157}
1158
[email protected]425e5332013-09-21 04:10:031159void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
[email protected]cfa7fd922014-04-29 11:50:031160 // Add arbitrary damage, to trigger prepare-to-draws.
1161 // Here, setting damage as viewport size, used only for testing.
1162 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031163 DidInitializeVisibleTile();
1164}
1165
[email protected]34bff2122014-05-16 04:09:341166void LayerTreeHostImpl::ResetTreesForTesting() {
1167 if (active_tree_)
1168 active_tree_->DetachLayerTree();
1169 active_tree_ = LayerTreeImpl::create(this);
1170 if (pending_tree_)
1171 pending_tree_->DetachLayerTree();
1172 pending_tree_.reset();
1173 if (recycle_tree_)
1174 recycle_tree_->DetachLayerTree();
1175 recycle_tree_.reset();
1176}
1177
vmpstr7fceb772014-08-26 18:51:521178void LayerTreeHostImpl::ResetRecycleTreeForTesting() {
1179 if (recycle_tree_)
1180 recycle_tree_->DetachLayerTree();
1181 recycle_tree_.reset();
1182}
1183
[email protected]c1bb5af2013-03-13 19:06:271184void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1185 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121186
[email protected]c1bb5af2013-03-13 19:06:271187 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351188 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351189 ManagedMemoryPolicy::PriorityCutoffToValue(
1190 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351191 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271192 if (evicted_resources) {
1193 active_tree_->SetContentsTexturesPurged();
1194 if (pending_tree_)
1195 pending_tree_->SetContentsTexturesPurged();
1196 client_->SetNeedsCommitOnImplThread();
1197 client_->OnCanDrawStateChanged(CanDraw());
1198 client_->RenewTreePriority();
1199 }
[email protected]8947cbe2012-11-28 05:27:431200
[email protected]8be1a9bf2013-05-01 03:45:191201 UpdateTileManagerMemoryPolicy(policy);
1202}
1203
1204void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1205 const ManagedMemoryPolicy& policy) {
1206 if (!tile_manager_)
1207 return;
1208
[email protected]25489c02014-02-13 23:18:411209 global_tile_state_.hard_memory_limit_in_bytes = 0;
1210 global_tile_state_.soft_memory_limit_in_bytes = 0;
1211 if (visible_ && policy.bytes_limit_when_visible > 0) {
1212 global_tile_state_.hard_memory_limit_in_bytes =
1213 policy.bytes_limit_when_visible;
1214 global_tile_state_.soft_memory_limit_in_bytes =
[email protected]831414e2014-02-22 02:27:371215 (static_cast<int64>(global_tile_state_.hard_memory_limit_in_bytes) *
1216 settings_.max_memory_for_prepaint_percentage) /
1217 100;
[email protected]25489c02014-02-13 23:18:411218 }
[email protected]1bcced22013-09-24 13:51:191219 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191220 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1221 visible_ ?
1222 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351223 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191224 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081225
[email protected]b7635e02014-04-28 07:08:431226 // TODO(reveman): We should avoid keeping around unused resources if
1227 // possible. crbug.com/224475
1228 // Unused limit is calculated from soft-limit, as hard-limit may
1229 // be very high and shouldn't typically be exceeded.
1230 size_t unused_memory_limit_in_bytes = static_cast<size_t>(
1231 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1232 settings_.max_unused_resource_memory_percentage) /
1233 100);
1234
[email protected]eece11e2014-04-12 03:07:161235 DCHECK(resource_pool_);
1236 resource_pool_->CheckBusyResources();
1237 // Soft limit is used for resource pool such that memory returns to soft
1238 // limit after going over.
1239 resource_pool_->SetResourceUsageLimits(
1240 global_tile_state_.soft_memory_limit_in_bytes,
[email protected]b7635e02014-04-28 07:08:431241 unused_memory_limit_in_bytes,
[email protected]eece11e2014-04-12 03:07:161242 global_tile_state_.num_resources_limit);
1243
revemanc84602a02014-09-24 15:41:251244 // Release all staging resources when invisible.
[email protected]2cccfef2014-05-01 06:05:161245 if (staging_resource_pool_) {
1246 staging_resource_pool_->CheckBusyResources();
1247 staging_resource_pool_->SetResourceUsageLimits(
revemanc84602a02014-09-24 15:41:251248 std::numeric_limits<size_t>::max(),
1249 std::numeric_limits<size_t>::max(),
1250 visible_ ? GetMaxStagingResourceCount() : 0);
[email protected]2cccfef2014-05-01 06:05:161251 }
1252
[email protected]c48536a52013-09-14 00:02:081253 DidModifyTilePriorities();
1254}
1255
1256void LayerTreeHostImpl::DidModifyTilePriorities() {
1257 DCHECK(settings_.impl_side_painting);
1258 // Mark priorities as dirty and schedule a ManageTiles().
1259 tile_priorities_dirty_ = true;
1260 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141261}
1262
[email protected]86126792013-03-16 20:07:541263void LayerTreeHostImpl::DidInitializeVisibleTile() {
[email protected]425e5332013-09-21 04:10:031264 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541265 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471266}
1267
[email protected]bf050582014-07-24 11:10:581268void LayerTreeHostImpl::GetPictureLayerImplPairs(
1269 std::vector<PictureLayerImpl::Pair>* layer_pairs) const {
1270 DCHECK(layer_pairs->empty());
1271 for (std::vector<PictureLayerImpl*>::const_iterator it =
1272 picture_layers_.begin();
1273 it != picture_layers_.end();
1274 ++it) {
1275 PictureLayerImpl* layer = *it;
1276
1277 // TODO(vmpstr): Iterators and should handle this instead. crbug.com/381704
1278 if (!layer->HasValidTilePriorities())
1279 continue;
1280
1281 PictureLayerImpl* twin_layer = layer->GetTwinLayer();
1282
1283 // Ignore the twin layer when tile priorities are invalid.
1284 // TODO(vmpstr): Iterators should handle this instead. crbug.com/381704
1285 if (twin_layer && !twin_layer->HasValidTilePriorities())
1286 twin_layer = NULL;
1287
1288 // If the current tree is ACTIVE_TREE, then always generate a layer_pair.
1289 // If current tree is PENDING_TREE, then only generate a layer_pair if
1290 // there is no twin layer.
1291 if (layer->GetTree() == ACTIVE_TREE) {
1292 DCHECK(!twin_layer || twin_layer->GetTree() == PENDING_TREE);
1293 layer_pairs->push_back(PictureLayerImpl::Pair(layer, twin_layer));
1294 } else if (!twin_layer) {
1295 layer_pairs->push_back(PictureLayerImpl::Pair(NULL, layer));
1296 }
1297 }
1298}
1299
1300void LayerTreeHostImpl::BuildRasterQueue(RasterTilePriorityQueue* queue,
1301 TreePriority tree_priority) {
1302 picture_layer_pairs_.clear();
1303 GetPictureLayerImplPairs(&picture_layer_pairs_);
1304 queue->Build(picture_layer_pairs_, tree_priority);
1305}
1306
1307void LayerTreeHostImpl::BuildEvictionQueue(EvictionTilePriorityQueue* queue,
1308 TreePriority tree_priority) {
1309 picture_layer_pairs_.clear();
1310 GetPictureLayerImplPairs(&picture_layer_pairs_);
1311 queue->Build(picture_layer_pairs_, tree_priority);
1312}
1313
1314const std::vector<PictureLayerImpl*>& LayerTreeHostImpl::GetPictureLayers()
1315 const {
[email protected]8aa39ecb2014-06-12 14:19:141316 return picture_layers_;
1317}
1318
[email protected]94bf75c2013-06-12 13:20:041319void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381320 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041321}
1322
[email protected]8f802b22014-05-27 21:28:121323void LayerTreeHostImpl::NotifyTileStateChanged(const Tile* tile) {
1324 TRACE_EVENT0("cc", "LayerTreeHostImpl::NotifyTileStateChanged");
[email protected]cfa7fd922014-04-29 11:50:031325
[email protected]90af23b2014-04-30 07:00:341326 if (active_tree_) {
1327 LayerImpl* layer_impl =
1328 active_tree_->FindActiveTreeLayerById(tile->layer_id());
1329 if (layer_impl)
[email protected]8f802b22014-05-27 21:28:121330 layer_impl->NotifyTileStateChanged(tile);
[email protected]90af23b2014-04-30 07:00:341331 }
1332
1333 if (pending_tree_) {
1334 LayerImpl* layer_impl =
1335 pending_tree_->FindPendingTreeLayerById(tile->layer_id());
1336 if (layer_impl)
[email protected]8f802b22014-05-27 21:28:121337 layer_impl->NotifyTileStateChanged(tile);
[email protected]cfa7fd922014-04-29 11:50:031338 }
1339}
1340
[email protected]3f2ff112013-08-03 02:41:071341void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1342 SetManagedMemoryPolicy(policy, zero_budget_);
1343}
1344
[email protected]5f07afc2013-07-19 05:17:181345void LayerTreeHostImpl::SetTreeActivationCallback(
1346 const base::Closure& callback) {
1347 DCHECK(proxy_->IsImplThread());
1348 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261349 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181350}
1351
[email protected]c1bb5af2013-03-13 19:06:271352void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071353 const ManagedMemoryPolicy& policy, bool zero_budget) {
1354 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271355 return;
[email protected]61de5812012-11-08 07:03:441356
[email protected]3f2ff112013-08-03 02:41:071357 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1358
1359 cached_managed_memory_policy_ = policy;
1360 zero_budget_ = zero_budget;
1361 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1362
1363 if (old_policy == actual_policy)
1364 return;
1365
1366 if (!proxy_->HasImplThread()) {
1367 // In single-thread mode, this can be called on the main thread by
1368 // GLRenderer::OnMemoryAllocationChanged.
1369 DebugScopedSetImplThread impl_thread(proxy_);
1370 EnforceManagedMemoryPolicy(actual_policy);
1371 } else {
1372 DCHECK(proxy_->IsImplThread());
1373 EnforceManagedMemoryPolicy(actual_policy);
1374 }
[email protected]50644642013-06-20 13:58:551375
[email protected]d7626ffd2013-03-29 00:17:421376 // If there is already enough memory to draw everything imaginable and the
1377 // new memory limit does not change this, then do not re-commit. Don't bother
1378 // skipping commits if this is not visible (commits don't happen when not
1379 // visible, there will almost always be a commit when this becomes visible).
1380 bool needs_commit = true;
1381 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071382 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1383 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1384 actual_policy.priority_cutoff_when_visible ==
1385 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421386 needs_commit = false;
1387 }
1388
[email protected]d7626ffd2013-03-29 00:17:421389 if (needs_commit)
1390 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141391}
1392
[email protected]f224cc92013-06-06 23:23:321393void LayerTreeHostImpl::SetExternalDrawConstraints(
1394 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061395 const gfx::Rect& viewport,
1396 const gfx::Rect& clip,
[email protected]bd5324592014-07-31 09:09:331397 const gfx::Rect& viewport_rect_for_tile_priority,
1398 const gfx::Transform& transform_for_tile_priority,
[email protected]c05dfbb2014-07-10 22:49:041399 bool resourceless_software_draw) {
[email protected]bd5324592014-07-31 09:09:331400 gfx::Rect viewport_rect_for_tile_priority_in_view_space;
1401 if (!resourceless_software_draw) {
1402 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
1403 if (transform_for_tile_priority.GetInverse(&screen_to_view)) {
1404 // Convert from screen space to view space.
1405 viewport_rect_for_tile_priority_in_view_space =
1406 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
1407 screen_to_view, viewport_rect_for_tile_priority));
1408 }
1409 }
1410
[email protected]c05dfbb2014-07-10 22:49:041411 if (external_transform_ != transform || external_viewport_ != viewport ||
[email protected]bd5324592014-07-31 09:09:331412 resourceless_software_draw_ != resourceless_software_draw ||
1413 viewport_rect_for_tile_priority_ !=
1414 viewport_rect_for_tile_priority_in_view_space) {
[email protected]ea12b5502014-06-24 21:37:361415 active_tree_->set_needs_update_draw_properties();
1416 }
1417
[email protected]f224cc92013-06-06 23:23:321418 external_transform_ = transform;
1419 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281420 external_clip_ = clip;
[email protected]bd5324592014-07-31 09:09:331421 viewport_rect_for_tile_priority_ =
1422 viewport_rect_for_tile_priority_in_view_space;
[email protected]c05dfbb2014-07-10 22:49:041423 resourceless_software_draw_ = resourceless_software_draw;
[email protected]f224cc92013-06-06 23:23:321424}
1425
[email protected]0023fc72014-01-10 20:05:061426void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311427 if (damage_rect.IsEmpty())
1428 return;
1429 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031430 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1431}
1432
[email protected]97e9ccd62014-04-12 01:07:151433void LayerTreeHostImpl::BeginFrame(const BeginFrameArgs& args) {
1434 client_->BeginFrame(args);
[email protected]7ed47512013-03-26 22:28:451435}
1436
[email protected]4d7e46a2013-11-08 05:33:401437void LayerTreeHostImpl::DidSwapBuffers() {
1438 client_->DidSwapBuffersOnImplThread();
1439}
1440
[email protected]c14902662014-04-18 05:06:111441void LayerTreeHostImpl::DidSwapBuffersComplete() {
1442 client_->DidSwapBuffersCompleteOnImplThread();
[email protected]a7335e0b2013-09-18 09:34:511443}
1444
1445void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271446 // TODO(piman): We may need to do some validation on this ack before
1447 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511448 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291449 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]43b552672014-04-05 00:38:571450
1451 // In OOM, we now might be able to release more resources that were held
1452 // because they were exported.
1453 if (tile_manager_) {
[email protected]eece11e2014-04-12 03:07:161454 DCHECK(resource_pool_);
[email protected]43b552672014-04-05 00:38:571455
[email protected]eece11e2014-04-12 03:07:161456 resource_pool_->CheckBusyResources();
1457 resource_pool_->ReduceResourceUsage();
[email protected]43b552672014-04-05 00:38:571458 }
[email protected]e97908d2014-04-09 12:48:041459 // If we're not visible, we likely released resources, so we want to
1460 // aggressively flush here to make sure those DeleteTextures make it to the
1461 // GPU process to free up the memory.
[email protected]110fcc0e2014-07-16 15:52:471462 if (output_surface_->context_provider() && !visible_) {
1463 output_surface_->context_provider()->ContextGL()->ShallowFlushCHROMIUM();
1464 }
[email protected]a46f32932012-12-07 21:43:161465}
1466
[email protected]c1bb5af2013-03-13 19:06:271467void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1468 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291469}
1470
[email protected]c1bb5af2013-03-13 19:06:271471CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1472 CompositorFrameMetadata metadata;
1473 metadata.device_scale_factor = device_scale_factor_;
1474 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
[email protected]f3f611f2014-07-09 04:05:321475 metadata.scrollable_viewport_size = active_tree_->ScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:271476 metadata.root_layer_size = active_tree_->ScrollableSize();
1477 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1478 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1479 if (top_controls_manager_) {
1480 metadata.location_bar_offset =
bokan88eae012014-09-09 20:40:421481 gfx::Vector2dF(0.f, top_controls_manager_->ControlsTopOffset());
[email protected]c1bb5af2013-03-13 19:06:271482 metadata.location_bar_content_translation =
bokan88eae012014-09-09 20:40:421483 gfx::Vector2dF(0.f, top_controls_manager_->ContentTopOffset());
[email protected]c1bb5af2013-03-13 19:06:271484 }
[email protected]bf189f62012-12-18 03:42:111485
[email protected]ebb179b2014-07-16 17:54:411486 active_tree_->GetViewportSelection(&metadata.selection_start,
1487 &metadata.selection_end);
[email protected]19aec372014-07-01 19:08:491488
[email protected]adeda572014-01-31 00:49:471489 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111490 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271491
[email protected]adeda572014-01-31 00:49:471492 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271493
1494 return metadata;
[email protected]bf189f62012-12-18 03:42:111495}
1496
[email protected]6f50b8fa2013-07-01 19:51:491497static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1498 layer->DidBeginTracing();
1499}
[email protected]37349bc2013-06-04 01:31:521500
[email protected]2e316b272014-07-19 05:01:201501void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1502 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351503 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271504 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201505
[email protected]3519b872013-07-30 07:17:501506 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191507 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071508 DCHECK(!output_surface_->capabilities()
1509 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201510 return;
[email protected]3519b872013-07-30 07:17:501511 }
[email protected]e0341352013-04-06 05:01:201512
[email protected]c1bb5af2013-03-13 19:06:271513 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141514
[email protected]2e316b272014-07-19 05:01:201515 fps_counter_->SaveTimeStamp(frame_begin_time,
[email protected]213fcfc2013-08-24 14:59:271516 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351517 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091518 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351519 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431520
[email protected]c1bb5af2013-03-13 19:06:271521 if (tile_manager_) {
1522 memory_history_->SaveEntry(
1523 tile_manager_->memory_stats_from_last_assign());
1524 }
[email protected]1191d9d2013-02-02 06:00:331525
[email protected]846f455b2013-03-18 19:07:411526 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021527 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271528 active_tree_->root_layer(),
[email protected]f636ce82014-03-10 22:33:081529 active_tree_->hud_layer(),
[email protected]c1bb5af2013-03-13 19:06:271530 *frame->render_surface_layer_list,
1531 frame->occluding_screen_space_rects,
1532 frame->non_occluding_screen_space_rects,
1533 debug_state_);
1534 }
[email protected]94f206c12012-08-25 00:09:141535
[email protected]a848c102013-03-26 08:59:091536 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1537 const RenderingStats& stats =
1538 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181539 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091540 }
1541
[email protected]37349bc2013-06-04 01:31:521542 bool is_new_trace;
1543 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1544 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491545 if (pending_tree_) {
1546 LayerTreeHostCommon::CallFunctionForSubtree(
1547 pending_tree_->root_layer(),
1548 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1549 }
1550 LayerTreeHostCommon::CallFunctionForSubtree(
1551 active_tree_->root_layer(),
1552 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521553 }
1554
[email protected]5b8da122014-06-25 20:00:081555 {
1556 TRACE_EVENT0("cc", "DrawLayers.FrameViewerTracing");
1557 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
1558 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1559 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads") ","
1560 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.layers"),
1561 "cc::LayerTreeHostImpl",
1562 id_,
[email protected]d12aa932014-08-01 13:10:381563 AsValueWithFrame(frame));
[email protected]5b8da122014-06-25 20:00:081564 }
[email protected]131a0c22013-02-12 18:31:081565
[email protected]c05dfbb2014-07-10 22:49:041566 const DrawMode draw_mode = GetDrawMode();
1567
[email protected]c1bb5af2013-03-13 19:06:271568 // Because the contents of the HUD depend on everything else in the frame, the
1569 // contents of its texture are updated as the last thing before the frame is
1570 // drawn.
[email protected]671f6672013-10-01 07:31:071571 if (active_tree_->hud_layer()) {
1572 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]c05dfbb2014-07-10 22:49:041573 active_tree_->hud_layer()->UpdateHudTexture(draw_mode,
1574 resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071575 }
[email protected]94f206c12012-08-25 00:09:141576
[email protected]c05dfbb2014-07-10 22:49:041577 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) {
[email protected]251699b2013-10-09 00:21:261578 bool disable_picture_quad_image_filtering =
1579 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321580
[email protected]b09c1942013-05-10 00:06:371581 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321582 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301583 temp_software_renderer->DrawFrame(&frame->render_passes,
[email protected]59fee302013-12-10 17:53:301584 device_scale_factor_,
1585 DeviceViewport(),
1586 DeviceClip(),
[email protected]59fee302013-12-10 17:53:301587 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371588 } else {
[email protected]b5174d712013-08-28 08:10:431589 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321590 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301591 DeviceViewport(),
1592 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261593 false);
[email protected]b09c1942013-05-10 00:06:371594 }
[email protected]c1bb5af2013-03-13 19:06:271595 // The render passes should be consumed by the renderer.
1596 DCHECK(frame->render_passes.empty());
1597 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141598
[email protected]c1bb5af2013-03-13 19:06:271599 // The next frame should start by assuming nothing has changed, and changes
1600 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321601 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1602 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541603 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271604 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1605 DidDrawDamagedArea();
1606 }
1607 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351608
[email protected]759dc9f2014-07-23 19:18:511609 active_tree_->set_has_ever_been_drawn(true);
[email protected]d73094482014-02-13 21:28:281610 devtools_instrumentation::DidDrawFrame(id_);
[email protected]1bbed7a2014-07-08 02:54:001611 benchmark_instrumentation::IssueImplThreadRenderingStatsEvent(
[email protected]adbe30f2013-10-11 21:12:331612 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181613 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141614}
1615
[email protected]c1bb5af2013-03-13 19:06:271616void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1617 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1618 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521619
[email protected]c1bb5af2013-03-13 19:06:271620 // Once all layers have been drawn, pending texture uploads should no
1621 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151622 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141623}
1624
[email protected]c1bb5af2013-03-13 19:06:271625void LayerTreeHostImpl::FinishAllRendering() {
1626 if (renderer_)
1627 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141628}
1629
[email protected]13525d62014-05-20 21:22:041630void LayerTreeHostImpl::SetUseGpuRasterization(bool use_gpu) {
1631 if (use_gpu == use_gpu_rasterization_)
1632 return;
1633
1634 use_gpu_rasterization_ = use_gpu;
1635 ReleaseTreeResources();
1636
[email protected]ced667b2014-05-22 21:49:531637 // Replace existing tile manager with another one that uses appropriate
1638 // rasterizer.
1639 if (tile_manager_) {
1640 DestroyTileManager();
1641 CreateAndSetTileManager();
1642 }
1643
[email protected]13525d62014-05-20 21:22:041644 // We have released tilings for both active and pending tree.
1645 // We would not have any content to draw until the pending tree is activated.
1646 // Prevent the active tree from drawing until activation.
1647 active_tree_->SetRequiresHighResToDraw();
1648}
1649
[email protected]7a8bcd262014-01-15 12:54:581650const RendererCapabilitiesImpl&
1651LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271652 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141653}
1654
[email protected]e0341352013-04-06 05:01:201655bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231656 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191657 if (frame.has_no_damage) {
1658 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201659 return false;
[email protected]b69c1db2013-11-27 00:05:191660 }
[email protected]4a28a612013-11-27 02:06:331661 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551662 active_tree()->FinishSwapPromises(&metadata);
[email protected]15ba6dea2014-04-02 01:44:131663 for (size_t i = 0; i < metadata.latency_info.size(); i++) {
1664 TRACE_EVENT_FLOW_STEP0(
1665 "input",
1666 "LatencyInfo.Flow",
1667 TRACE_ID_DONT_MANGLE(metadata.latency_info[i].trace_id),
1668 "SwapBuffers");
1669 }
[email protected]4a28a612013-11-27 02:06:331670 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161671 return true;
[email protected]94f206c12012-08-25 00:09:141672}
1673
[email protected]97e9ccd62014-04-12 01:07:151674void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451675 if (output_surface_)
[email protected]97e9ccd62014-04-12 01:07:151676 output_surface_->SetNeedsBeginFrame(enable);
[email protected]3dc0c772014-04-26 10:06:051677 else
1678 DCHECK(!enable);
[email protected]97e9ccd62014-04-12 01:07:151679}
1680
1681void LayerTreeHostImpl::WillBeginImplFrame(const BeginFrameArgs& args) {
1682 // Sample the frame time now. This time will be used for updating animations
1683 // when we draw.
[email protected]04c5900d2014-08-18 13:38:361684 UpdateCurrentBeginFrameArgs(args);
[email protected]c92195e2014-05-07 18:18:491685 // Cache the begin impl frame interval
1686 begin_impl_frame_interval_ = args.interval;
[email protected]7ed47512013-03-26 22:28:451687}
1688
[email protected]fef74fd2014-02-27 06:28:171689void LayerTreeHostImpl::UpdateInnerViewportContainerSize() {
1690 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
1691 if (!container_layer)
1692 return;
1693
bokan88eae012014-09-09 20:40:421694 if (top_controls_manager_) {
[email protected]587941d2014-08-22 01:40:011695 container_layer->SetBoundsDelta(
bokan88eae012014-09-09 20:40:421696 gfx::Vector2dF(0, active_tree_->top_controls_layout_height() -
bokan55b2f152014-09-15 14:47:591697 active_tree_->total_top_controls_content_offset()));
bokan88eae012014-09-09 20:40:421698 }
1699}
1700
1701void LayerTreeHostImpl::SetTopControlsLayoutHeight(float height) {
1702 if (active_tree_->top_controls_layout_height() == height)
1703 return;
1704
1705 active_tree_->set_top_controls_layout_height(height);
1706 UpdateInnerViewportContainerSize();
1707 SetFullRootLayerDamage();
[email protected]ffb2720f2013-03-15 19:18:371708}
1709
[email protected]c1bb5af2013-03-13 19:06:271710void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191711 if (resource_provider_)
1712 resource_provider_->DidLoseOutputSurface();
dnetob71e30c2014-08-25 23:27:201713 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141714}
1715
[email protected]59adb112013-04-09 04:48:441716bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471717 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011718}
1719
[email protected]c1bb5af2013-03-13 19:06:271720LayerImpl* LayerTreeHostImpl::RootLayer() const {
1721 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081722}
1723
[email protected]adeda572014-01-31 00:49:471724LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1725 return active_tree_->InnerViewportScrollLayer();
1726}
1727
1728LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1729 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081730}
1731
[email protected]c1bb5af2013-03-13 19:06:271732LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1733 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081734}
1735
[email protected]251699b2013-10-09 00:21:261736bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1737 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471738 (InnerViewportScrollLayer() &&
1739 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1740 (OuterViewportScrollLayer() &&
1741 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261742}
1743
[email protected]94f206c12012-08-25 00:09:141744// Content layers can be either directly scrollable or contained in an outer
1745// scrolling layer which applies the scroll transform. Given a content layer,
1746// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271747static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1748 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021749 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271750
1751 if (layer_impl->scrollable())
1752 return layer_impl;
1753
1754 if (layer_impl->DrawsContent() &&
1755 layer_impl->parent() &&
1756 layer_impl->parent()->scrollable())
1757 return layer_impl->parent();
1758
[email protected]bf1cfd9a2013-09-26 05:43:021759 return NULL;
[email protected]94f206c12012-08-25 00:09:141760}
1761
[email protected]c1bb5af2013-03-13 19:06:271762void LayerTreeHostImpl::CreatePendingTree() {
1763 CHECK(!pending_tree_);
1764 if (recycle_tree_)
1765 recycle_tree_.swap(pending_tree_);
1766 else
1767 pending_tree_ = LayerTreeImpl::create(this);
[email protected]12a63da2014-06-13 06:06:221768
1769 // Update the delta from the active tree, which may have
1770 // adjusted its delta prior to the pending tree being created.
1771 DCHECK_EQ(1.f, pending_tree_->sent_page_scale_delta());
bokan88eae012014-09-09 20:40:421772 DCHECK_EQ(0.f, pending_tree_->sent_top_controls_delta());
[email protected]12a63da2014-06-13 06:06:221773 pending_tree_->SetPageScaleDelta(active_tree_->page_scale_delta() /
1774 active_tree_->sent_page_scale_delta());
bokan88eae012014-09-09 20:40:421775 pending_tree_->set_top_controls_delta(
1776 active_tree_->top_controls_delta() -
1777 active_tree_->sent_top_controls_delta());
[email protected]12a63da2014-06-13 06:06:221778
[email protected]c1bb5af2013-03-13 19:06:271779 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111780 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271781}
1782
[email protected]8bb6cfa2013-07-23 00:11:191783void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191784 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1785 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191786 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481787}
1788
[email protected]fc20d142014-07-01 00:49:151789void LayerTreeHostImpl::ActivateSyncTree() {
[email protected]4f48f6e2013-08-27 06:33:381790 need_to_update_visible_tiles_before_draw_ = true;
1791
[email protected]fc20d142014-07-01 00:49:151792 if (pending_tree_) {
1793 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
1794
1795 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
1796 active_tree_->PushPersistedState(pending_tree_.get());
1797 // Process any requests in the UI resource queue. The request queue is
1798 // given in LayerTreeHost::FinishCommitOnImplThread. This must take place
1799 // before the swap.
1800 pending_tree_->ProcessUIResourceRequestQueue();
1801
1802 if (pending_tree_->needs_full_tree_sync()) {
1803 active_tree_->SetRootLayer(
1804 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
1805 active_tree_->DetachLayerTree(),
1806 active_tree_.get()));
1807 }
1808 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
1809 active_tree_->root_layer());
1810 pending_tree_->PushPropertiesTo(active_tree_.get());
1811
1812 // Now that we've synced everything from the pending tree to the active
1813 // tree, rename the pending tree the recycle tree so we can reuse it on the
1814 // next sync.
1815 DCHECK(!recycle_tree_);
1816 pending_tree_.swap(recycle_tree_);
1817
1818 active_tree_->SetRootLayerScrollOffsetDelegate(
1819 root_layer_scroll_offset_delegate_);
bokan88eae012014-09-09 20:40:421820
1821 if (top_controls_manager_) {
1822 top_controls_manager_->SetControlsTopOffset(
bokan55b2f152014-09-15 14:47:591823 active_tree_->total_top_controls_content_offset() -
1824 top_controls_manager_->top_controls_height());
bokan88eae012014-09-09 20:40:421825 }
1826
[email protected]fc20d142014-07-01 00:49:151827 UpdateInnerViewportContainerSize();
1828 } else {
1829 active_tree_->ProcessUIResourceRequestQueue();
[email protected]c1bb5af2013-03-13 19:06:271830 }
[email protected]48871fc2013-01-23 07:36:511831
[email protected]7d1b07e2013-10-01 17:31:301832 active_tree_->DidBecomeActive();
[email protected]b33348f2014-04-30 18:17:341833 ActivateAnimations();
[email protected]fc20d142014-07-01 00:49:151834 if (settings_.impl_side_painting)
1835 client_->RenewTreePriority();
[email protected]37386f052013-01-13 00:42:221836
[email protected]c1bb5af2013-03-13 19:06:271837 client_->OnCanDrawStateChanged(CanDraw());
[email protected]fc20d142014-07-01 00:49:151838 client_->DidActivateSyncTree();
1839 if (!tree_activation_callback_.is_null())
1840 tree_activation_callback_.Run();
[email protected]652cf132013-02-15 21:53:241841
[email protected]a848c102013-03-26 08:59:091842 if (debug_state_.continuous_painting) {
1843 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431844 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091845 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1846 stats.main_stats.record_time +
1847 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271848 }
[email protected]2a61ad52013-05-13 14:01:291849
[email protected]d9fce6722013-08-30 01:10:011850 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1851 DCHECK(active_tree_->root_layer());
[email protected]2e7ca422012-12-20 02:57:271852}
1853
[email protected]c1bb5af2013-03-13 19:06:271854void LayerTreeHostImpl::SetVisible(bool visible) {
1855 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141856
[email protected]c1bb5af2013-03-13 19:06:271857 if (visible_ == visible)
1858 return;
1859 visible_ = visible;
1860 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121861 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141862
[email protected]10608052014-05-13 21:30:031863 // If we just became visible, we have to ensure that we draw high res tiles,
1864 // to prevent checkerboard/low res flashes.
1865 if (visible_)
[email protected]3d609bb2014-02-01 01:10:231866 active_tree()->SetRequiresHighResToDraw();
[email protected]10608052014-05-13 21:30:031867 else
[email protected]127bdc1a2013-09-11 01:44:481868 EvictAllUIResources();
1869
[email protected]0fa42e02013-08-20 01:16:051870 // Evict tiles immediately if invisible since this tab may never get another
1871 // draw or timer tick.
1872 if (!visible_)
1873 ManageTiles();
1874
[email protected]c1bb5af2013-03-13 19:06:271875 if (!renderer_)
1876 return;
[email protected]94f206c12012-08-25 00:09:141877
[email protected]c1bb5af2013-03-13 19:06:271878 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141879}
1880
[email protected]43b8f982014-04-30 21:24:331881void LayerTreeHostImpl::SetNeedsAnimate() {
1882 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1883 client_->SetNeedsAnimateOnImplThread();
1884}
1885
[email protected]6be422b2013-12-08 06:47:311886void LayerTreeHostImpl::SetNeedsRedraw() {
1887 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1888 client_->SetNeedsRedrawOnImplThread();
1889}
1890
[email protected]206a3922013-05-17 06:34:121891ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071892 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]c92195e2014-05-07 18:18:491893 if (debug_state_.rasterize_only_visible_content) {
[email protected]3f2ff112013-08-03 02:41:071894 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291895 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]13525d62014-05-20 21:22:041896 } else if (use_gpu_rasterization()) {
[email protected]c92195e2014-05-07 18:18:491897 actual.priority_cutoff_when_visible =
1898 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
[email protected]3f2ff112013-08-03 02:41:071899 }
[email protected]206a3922013-05-17 06:34:121900
[email protected]3f2ff112013-08-03 02:41:071901 if (zero_budget_) {
1902 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071903 }
1904
[email protected]206a3922013-05-17 06:34:121905 return actual;
1906}
1907
[email protected]3f2ff112013-08-03 02:41:071908size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1909 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1910}
1911
[email protected]990e050a2013-09-23 18:50:211912int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1913 return ManagedMemoryPolicy::PriorityCutoffToValue(
1914 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1915}
1916
[email protected]50644642013-06-20 13:58:551917void LayerTreeHostImpl::ReleaseTreeResources() {
[email protected]aeef2f02014-05-10 12:15:481918 active_tree_->ReleaseResources();
1919 if (pending_tree_)
1920 pending_tree_->ReleaseResources();
1921 if (recycle_tree_)
1922 recycle_tree_->ReleaseResources();
[email protected]c9280762013-08-01 06:28:571923
[email protected]127bdc1a2013-09-11 01:44:481924 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551925}
1926
[email protected]ced667b2014-05-22 21:49:531927void LayerTreeHostImpl::CreateAndSetRenderer() {
[email protected]50644642013-06-20 13:58:551928 DCHECK(!renderer_);
[email protected]ced667b2014-05-22 21:49:531929 DCHECK(output_surface_);
1930 DCHECK(resource_provider_);
1931
1932 if (output_surface_->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321933 renderer_ = DelegatingRenderer::Create(
[email protected]ced667b2014-05-22 21:49:531934 this, &settings_, output_surface_.get(), resource_provider_.get());
1935 } else if (output_surface_->context_provider()) {
[email protected]50644642013-06-20 13:58:551936 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321937 &settings_,
[email protected]ced667b2014-05-22 21:49:531938 output_surface_.get(),
1939 resource_provider_.get(),
[email protected]ea468c6c2013-09-10 08:25:111940 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241941 settings_.highp_threshold_min);
[email protected]ced667b2014-05-22 21:49:531942 } else if (output_surface_->software_device()) {
[email protected]573e8e32013-09-09 17:24:321943 renderer_ = SoftwareRenderer::Create(
[email protected]ced667b2014-05-22 21:49:531944 this, &settings_, output_surface_.get(), resource_provider_.get());
[email protected]50644642013-06-20 13:58:551945 }
[email protected]0cfda302014-04-23 19:02:371946 DCHECK(renderer_);
[email protected]50644642013-06-20 13:58:551947
[email protected]0cfda302014-04-23 19:02:371948 renderer_->SetVisible(visible_);
1949 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561950
[email protected]0cfda302014-04-23 19:02:371951 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1952 // initialized to get max texture size. Also, after releasing resources,
1953 // trees need another update to generate new ones.
1954 active_tree_->set_needs_update_draw_properties();
1955 if (pending_tree_)
1956 pending_tree_->set_needs_update_draw_properties();
1957 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]50644642013-06-20 13:58:551958}
1959
[email protected]ced667b2014-05-22 21:49:531960void LayerTreeHostImpl::CreateAndSetTileManager() {
1961 DCHECK(!tile_manager_);
[email protected]50af3c22013-07-13 03:50:201962 DCHECK(settings_.impl_side_painting);
[email protected]ced667b2014-05-22 21:49:531963 DCHECK(output_surface_);
1964 DCHECK(resource_provider_);
henrikaea769fd2014-09-08 07:15:051965 DCHECK(proxy_->ImplThreadTaskRunner());
[email protected]ec7541d2014-04-09 01:23:531966
[email protected]ced667b2014-05-22 21:49:531967 ContextProvider* context_provider = output_surface_->context_provider();
reveman5ae838bb2014-09-24 23:30:141968 if (!context_provider) {
1969 resource_pool_ =
1970 ResourcePool::Create(resource_provider_.get(),
1971 GL_TEXTURE_2D,
1972 resource_provider_->best_texture_format());
1973
1974 raster_worker_pool_ =
1975 BitmapRasterWorkerPool::Create(proxy_->ImplThreadTaskRunner(),
1976 RasterWorkerPool::GetTaskGraphRunner(),
1977 resource_provider_.get());
1978 } else if (use_gpu_rasterization_) {
[email protected]eece11e2014-04-12 03:07:161979 resource_pool_ =
[email protected]ced667b2014-05-22 21:49:531980 ResourcePool::Create(resource_provider_.get(),
1981 GL_TEXTURE_2D,
1982 resource_provider_->best_texture_format());
1983
henrikaea769fd2014-09-08 07:15:051984 raster_worker_pool_ =
1985 GpuRasterWorkerPool::Create(proxy_->ImplThreadTaskRunner(),
1986 context_provider,
1987 resource_provider_.get());
reveman5ae838bb2014-09-24 23:30:141988 } else if (UseZeroCopyRasterizer()) {
1989 resource_pool_ = ResourcePool::Create(
1990 resource_provider_.get(),
1991 GetMapImageTextureTarget(context_provider->ContextCapabilities()),
1992 resource_provider_->best_texture_format());
1993
1994 raster_worker_pool_ =
1995 ZeroCopyRasterWorkerPool::Create(proxy_->ImplThreadTaskRunner(),
1996 RasterWorkerPool::GetTaskGraphRunner(),
1997 resource_provider_.get());
1998 } else if (UseOneCopyRasterizer()) {
[email protected]2cccfef2014-05-01 06:05:161999 // We need to create a staging resource pool when using copy rasterizer.
reveman5ae838bb2014-09-24 23:30:142000 staging_resource_pool_ = ResourcePool::Create(
2001 resource_provider_.get(),
2002 GetMapImageTextureTarget(context_provider->ContextCapabilities()),
2003 resource_provider_->best_texture_format());
[email protected]2cccfef2014-05-01 06:05:162004 resource_pool_ =
[email protected]ced667b2014-05-22 21:49:532005 ResourcePool::Create(resource_provider_.get(),
[email protected]2cccfef2014-05-01 06:05:162006 GL_TEXTURE_2D,
[email protected]ced667b2014-05-22 21:49:532007 resource_provider_->best_texture_format());
[email protected]2cccfef2014-05-01 06:05:162008
reveman423e7da2014-09-23 16:57:442009 raster_worker_pool_ =
2010 OneCopyRasterWorkerPool::Create(proxy_->ImplThreadTaskRunner(),
2011 RasterWorkerPool::GetTaskGraphRunner(),
2012 context_provider,
2013 resource_provider_.get(),
2014 staging_resource_pool_.get());
reveman5ae838bb2014-09-24 23:30:142015 } else {
[email protected]ced667b2014-05-22 21:49:532016 resource_pool_ = ResourcePool::Create(
2017 resource_provider_.get(),
2018 GL_TEXTURE_2D,
2019 resource_provider_->memory_efficient_texture_format());
2020
[email protected]7beac1b52014-04-10 23:09:562021 raster_worker_pool_ = PixelBufferRasterWorkerPool::Create(
henrikaea769fd2014-09-08 07:15:052022 proxy_->ImplThreadTaskRunner(),
[email protected]7beac1b52014-04-10 23:09:562023 RasterWorkerPool::GetTaskGraphRunner(),
[email protected]110fcc0e2014-07-16 15:52:472024 context_provider,
[email protected]ced667b2014-05-22 21:49:532025 resource_provider_.get(),
reveman5ae838bb2014-09-24 23:30:142026 GetMaxTransferBufferUsageBytes(context_provider->ContextCapabilities(),
revemanc84602a02014-09-24 15:41:252027 settings_.refresh_rate));
[email protected]ec7541d2014-04-09 01:23:532028 }
[email protected]ced667b2014-05-22 21:49:532029
henrikaea769fd2014-09-08 07:15:052030 tile_manager_ =
2031 TileManager::Create(this,
2032 proxy_->ImplThreadTaskRunner(),
2033 resource_pool_.get(),
2034 raster_worker_pool_->AsRasterizer(),
2035 rendering_stats_instrumentation_);
[email protected]b6eb8e332013-09-10 00:51:012036
[email protected]50af3c22013-07-13 03:50:202037 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:192038 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:202039}
2040
[email protected]ced667b2014-05-22 21:49:532041void LayerTreeHostImpl::DestroyTileManager() {
2042 tile_manager_.reset();
2043 resource_pool_.reset();
2044 staging_resource_pool_.reset();
2045 raster_worker_pool_.reset();
2046}
2047
[email protected]fc20d142014-07-01 00:49:152048bool LayerTreeHostImpl::UsePendingTreeForSync() const {
2049 // In impl-side painting, synchronize to the pending tree so that it has
2050 // time to raster before being displayed.
2051 return settings_.impl_side_painting;
2052}
2053
reveman423e7da2014-09-23 16:57:442054bool LayerTreeHostImpl::UseZeroCopyRasterizer() const {
reveman5ae838bb2014-09-24 23:30:142055 return settings_.use_zero_copy && GetRendererCapabilities().using_map_image;
[email protected]ced667b2014-05-22 21:49:532056}
2057
reveman423e7da2014-09-23 16:57:442058bool LayerTreeHostImpl::UseOneCopyRasterizer() const {
[email protected]ced667b2014-05-22 21:49:532059 // Sync query support is required by one-copy rasterizer.
2060 return settings_.use_one_copy && GetRendererCapabilities().using_map_image &&
2061 resource_provider_->use_sync_query();
2062}
2063
[email protected]50644642013-06-20 13:58:552064void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:072065 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:552066}
2067
[email protected]c1bb5af2013-03-13 19:06:272068bool LayerTreeHostImpl::InitializeRenderer(
2069 scoped_ptr<OutputSurface> output_surface) {
[email protected]06630f832014-05-29 23:15:142070 TRACE_EVENT0("cc", "LayerTreeHostImpl::InitializeRenderer");
[email protected]ce2e8112013-11-28 07:44:362071
[email protected]c1bb5af2013-03-13 19:06:272072 // Since we will create a new resource provider, we cannot continue to use
2073 // the old resources (i.e. render_surfaces and texture IDs). Clear them
2074 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:552075 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:402076
[email protected]c1bb5af2013-03-13 19:06:272077 // Note: order is important here.
2078 renderer_.reset();
[email protected]ced667b2014-05-22 21:49:532079 DestroyTileManager();
[email protected]c1bb5af2013-03-13 19:06:272080 resource_provider_.reset();
2081 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:142082
[email protected]c1bb5af2013-03-13 19:06:272083 if (!output_surface->BindToClient(this))
2084 return false;
[email protected]be3181652012-09-25 13:02:132085
[email protected]ced667b2014-05-22 21:49:532086 output_surface_ = output_surface.Pass();
2087 resource_provider_ =
2088 ResourceProvider::Create(output_surface_.get(),
[email protected]a7f35682013-10-22 23:05:572089 shared_bitmap_manager_,
skyostil3976a3f2014-09-04 22:07:232090 proxy_->blocking_main_thread_task_runner(),
[email protected]a7f35682013-10-22 23:05:572091 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:342092 settings_.use_rgba_4444_textures,
[email protected]b242b142014-05-07 14:48:492093 settings_.texture_id_allocation_chunk_size,
2094 settings_.use_distance_field_text);
[email protected]2b154b22013-06-07 09:03:272095
[email protected]ced667b2014-05-22 21:49:532096 if (output_surface_->capabilities().deferred_gl_initialization)
[email protected]50644642013-06-20 13:58:552097 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:132098
[email protected]ced667b2014-05-22 21:49:532099 CreateAndSetRenderer();
[email protected]be3181652012-09-25 13:02:132100
[email protected]ced667b2014-05-22 21:49:532101 if (settings_.impl_side_painting)
2102 CreateAndSetTileManager();
[email protected]fbe89f72013-05-21 07:24:242103
[email protected]3dc0c772014-04-26 10:06:052104 // Initialize vsync parameters to sane values.
2105 const base::TimeDelta display_refresh_interval =
2106 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond /
2107 settings_.refresh_rate);
2108 CommitVSyncParameters(base::TimeTicks(), display_refresh_interval);
[email protected]049fc7a2013-06-18 12:32:352109
[email protected]3dc0c772014-04-26 10:06:052110 // TODO(brianderson): Don't use a hard-coded parent draw time.
2111 base::TimeDelta parent_draw_time =
[email protected]4851dd22014-06-17 16:06:292112 (!settings_.begin_frame_scheduling_enabled &&
2113 output_surface_->capabilities().adjust_deadline_for_parent)
[email protected]1d142de82014-06-11 12:48:232114 ? BeginFrameArgs::DefaultEstimatedParentDrawTime()
[email protected]3dc0c772014-04-26 10:06:052115 : base::TimeDelta();
2116 client_->SetEstimatedParentDrawTime(parent_draw_time);
[email protected]049fc7a2013-06-18 12:32:352117
[email protected]ced667b2014-05-22 21:49:532118 int max_frames_pending = output_surface_->capabilities().max_frames_pending;
[email protected]049fc7a2013-06-18 12:32:352119 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:352120 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]c14902662014-04-18 05:06:112121 client_->SetMaxSwapsPendingOnImplThread(max_frames_pending);
[email protected]c1bb5af2013-03-13 19:06:272122 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:212123
sohan.jyoti9d1e4202014-09-23 21:02:332124 // There will not be anything to draw here, so set high res
2125 // to avoid checkerboards, typically when we are recovering
2126 // from lost context.
2127 active_tree_->SetRequiresHighResToDraw();
2128
[email protected]c1bb5af2013-03-13 19:06:272129 return true;
[email protected]94f206c12012-08-25 00:09:142130}
2131
[email protected]3dc0c772014-04-26 10:06:052132void LayerTreeHostImpl::CommitVSyncParameters(base::TimeTicks timebase,
2133 base::TimeDelta interval) {
2134 client_->CommitVSyncParameters(timebase, interval);
2135}
2136
[email protected]3b2eb882014-04-24 19:48:562137void LayerTreeHostImpl::DeferredInitialize() {
[email protected]2b154b22013-06-07 09:03:272138 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:552139 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:092140 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:272141
[email protected]50644642013-06-20 13:58:552142 ReleaseTreeResources();
2143 renderer_.reset();
[email protected]105a723202014-06-12 14:18:272144 DestroyTileManager();
[email protected]2b154b22013-06-07 09:03:272145
[email protected]0cfda302014-04-23 19:02:372146 resource_provider_->InitializeGL();
[email protected]d9ca99e2013-08-28 21:49:482147
[email protected]ced667b2014-05-22 21:49:532148 CreateAndSetRenderer();
[email protected]3b2eb882014-04-24 19:48:562149 EnforceZeroBudget(false);
[email protected]105a723202014-06-12 14:18:272150 CreateAndSetTileManager();
2151
[email protected]3b2eb882014-04-24 19:48:562152 client_->SetNeedsCommitOnImplThread();
[email protected]2b154b22013-06-07 09:03:272153}
2154
[email protected]50af3c22013-07-13 03:50:202155void LayerTreeHostImpl::ReleaseGL() {
2156 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
2157 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:092158 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:202159
2160 ReleaseTreeResources();
2161 renderer_.reset();
[email protected]ced667b2014-05-22 21:49:532162 DestroyTileManager();
[email protected]105a723202014-06-12 14:18:272163
[email protected]50af3c22013-07-13 03:50:202164 resource_provider_->InitializeSoftware();
[email protected]41b53ac2014-05-22 03:22:192165 output_surface_->ReleaseContextProvider();
[email protected]50af3c22013-07-13 03:50:202166
[email protected]105a723202014-06-12 14:18:272167 CreateAndSetRenderer();
[email protected]50af3c22013-07-13 03:50:202168 EnforceZeroBudget(true);
[email protected]ced667b2014-05-22 21:49:532169 CreateAndSetTileManager();
[email protected]50af3c22013-07-13 03:50:202170
[email protected]50af3c22013-07-13 03:50:202171 client_->SetNeedsCommitOnImplThread();
2172}
2173
[email protected]64348ea2014-01-29 22:58:262174void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:402175 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:272176 return;
[email protected]94f206c12012-08-25 00:09:142177
[email protected]54af03522013-09-05 00:43:282178 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:272179 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:272180
[email protected]c1bb5af2013-03-13 19:06:272181 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:142182
[email protected]fef74fd2014-02-27 06:28:172183 UpdateInnerViewportContainerSize();
[email protected]c1bb5af2013-03-13 19:06:272184 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:442185 SetFullRootLayerDamage();
[email protected]a533d9b82014-07-11 20:29:242186 active_tree_->set_needs_update_draw_properties();
[email protected]94f206c12012-08-25 00:09:142187}
2188
[email protected]9f4f6a32013-09-04 21:35:122189void LayerTreeHostImpl::SetOverhangUIResource(
2190 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:262191 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:122192 overhang_ui_resource_id_ = overhang_ui_resource_id;
2193 overhang_ui_resource_size_ = overhang_ui_resource_size;
2194}
2195
[email protected]c1bb5af2013-03-13 19:06:272196void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
2197 if (device_scale_factor == device_scale_factor_)
2198 return;
2199 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:272200
[email protected]59adb112013-04-09 04:48:442201 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142202}
2203
[email protected]bd5324592014-07-31 09:09:332204const gfx::Rect LayerTreeHostImpl::ViewportRectForTilePriority() const {
2205 if (viewport_rect_for_tile_priority_.IsEmpty())
2206 return DeviceViewport();
2207
2208 return viewport_rect_for_tile_priority_;
2209}
2210
[email protected]54af03522013-09-05 00:43:282211gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
2212 return DeviceViewport().size();
2213}
2214
[email protected]f224cc92013-06-06 23:23:322215gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
2216 if (external_viewport_.IsEmpty())
2217 return gfx::Rect(device_viewport_size_);
2218
2219 return external_viewport_;
2220}
2221
[email protected]54af03522013-09-05 00:43:282222gfx::Rect LayerTreeHostImpl::DeviceClip() const {
2223 if (external_clip_.IsEmpty())
2224 return DeviceViewport();
2225
2226 return external_clip_;
2227}
2228
2229const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:322230 return external_transform_;
2231}
2232
[email protected]59adb112013-04-09 04:48:442233void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]fef74fd2014-02-27 06:28:172234 UpdateInnerViewportContainerSize();
[email protected]4e4136d2013-11-29 02:22:442235 SetNeedsRedraw();
[email protected]43b8f982014-04-30 21:24:332236 SetNeedsAnimate();
[email protected]59adb112013-04-09 04:48:442237 active_tree_->set_needs_update_draw_properties();
2238 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142239}
2240
bokan88eae012014-09-09 20:40:422241void LayerTreeHostImpl::SetControlsTopOffset(float offset) {
bokan55b2f152014-09-15 14:47:592242 float current_top_offset = active_tree_->top_controls_content_offset() -
2243 top_controls_manager_->top_controls_height();
2244 active_tree_->set_top_controls_delta(offset - current_top_offset);
bokan88eae012014-09-09 20:40:422245}
2246
2247float LayerTreeHostImpl::ControlsTopOffset() const {
bokan55b2f152014-09-15 14:47:592248 return active_tree_->total_top_controls_content_offset() -
2249 top_controls_manager_->top_controls_height();
bokan88eae012014-09-09 20:40:422250}
2251
[email protected]200a9c062013-05-20 04:34:372252void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2253 DCHECK(input_handler_client_ == NULL);
2254 input_handler_client_ = client;
2255}
2256
[email protected]420fdf6e2013-08-26 20:36:382257static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2258 if (LayerImpl* scroll_parent = layer->scroll_parent())
2259 return scroll_parent;
2260 return layer->parent();
2261}
2262
[email protected]f620b0e72013-10-01 21:38:242263LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472264 const gfx::PointF& device_viewport_point,
2265 InputHandler::ScrollInputType type,
[email protected]f161ca9e2014-04-01 13:57:012266 LayerImpl* layer_impl,
2267 bool* scroll_on_main_thread,
[email protected]edcc1a12014-05-06 01:26:392268 bool* optional_has_ancestor_scroll_handler) const {
[email protected]bf1cfd9a2013-09-26 05:43:022269 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142270
[email protected]c1bb5af2013-03-13 19:06:272271 // Walk up the hierarchy and look for a scrollable layer.
[email protected]8f7f298822014-06-13 00:23:322272 LayerImpl* potentially_scrolling_layer_impl = NULL;
[email protected]420fdf6e2013-08-26 20:36:382273 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272274 // The content layer can also block attempts to scroll outside the main
2275 // thread.
2276 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2277 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022278 *scroll_on_main_thread = true;
2279 return NULL;
[email protected]94f206c12012-08-25 00:09:142280 }
2281
[email protected]c1bb5af2013-03-13 19:06:272282 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2283 if (!scroll_layer_impl)
2284 continue;
[email protected]94f206c12012-08-25 00:09:142285
[email protected]c1bb5af2013-03-13 19:06:272286 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272287 // If any layer wants to divert the scroll event to the main thread, abort.
2288 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022289 *scroll_on_main_thread = true;
2290 return NULL;
[email protected]94f206c12012-08-25 00:09:142291 }
2292
[email protected]edcc1a12014-05-06 01:26:392293 if (optional_has_ancestor_scroll_handler &&
[email protected]f161ca9e2014-04-01 13:57:012294 scroll_layer_impl->have_scroll_event_handlers())
[email protected]edcc1a12014-05-06 01:26:392295 *optional_has_ancestor_scroll_handler = true;
[email protected]f161ca9e2014-04-01 13:57:012296
[email protected]c1bb5af2013-03-13 19:06:272297 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2298 potentially_scrolling_layer_impl = scroll_layer_impl;
2299 }
2300
[email protected]edcc1a12014-05-06 01:26:392301 // Falling back to the root scroll layer ensures generation of root overscroll
2302 // notifications while preventing scroll updates from being unintentionally
2303 // forwarded to the main thread.
2304 if (!potentially_scrolling_layer_impl)
2305 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2306 ? OuterViewportScrollLayer()
2307 : InnerViewportScrollLayer();
2308
[email protected]bf1cfd9a2013-09-26 05:43:022309 return potentially_scrolling_layer_impl;
2310}
2311
[email protected]edcc1a12014-05-06 01:26:392312// Similar to LayerImpl::HasAncestor, but walks up the scroll parents.
[email protected]05ba53c2014-04-16 21:22:512313static bool HasScrollAncestor(LayerImpl* child, LayerImpl* scroll_ancestor) {
2314 DCHECK(scroll_ancestor);
2315 for (LayerImpl* ancestor = child; ancestor;
2316 ancestor = NextScrollLayer(ancestor)) {
2317 if (ancestor->scrollable())
2318 return ancestor == scroll_ancestor;
2319 }
2320 return false;
2321}
2322
[email protected]bf1cfd9a2013-09-26 05:43:022323InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
[email protected]47a723f2014-03-05 12:42:492324 const gfx::Point& viewport_point,
2325 InputHandler::ScrollInputType type) {
[email protected]bf1cfd9a2013-09-26 05:43:022326 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2327
2328 if (top_controls_manager_)
2329 top_controls_manager_->ScrollBegin();
2330
2331 DCHECK(!CurrentlyScrollingLayer());
2332 ClearCurrentlyScrollingLayer();
2333
[email protected]f620b0e72013-10-01 21:38:242334 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2335 device_scale_factor_);
[email protected]28336d52014-05-12 19:07:282336 LayerImpl* layer_impl =
2337 active_tree_->FindLayerThatIsHitByPoint(device_viewport_point);
[email protected]05ba53c2014-04-16 21:22:512338
2339 if (layer_impl) {
2340 LayerImpl* scroll_layer_impl =
[email protected]28336d52014-05-12 19:07:282341 active_tree_->FindFirstScrollingLayerThatIsHitByPoint(
2342 device_viewport_point);
[email protected]05ba53c2014-04-16 21:22:512343 if (scroll_layer_impl && !HasScrollAncestor(layer_impl, scroll_layer_impl))
2344 return ScrollUnknown;
2345 }
2346
[email protected]bf1cfd9a2013-09-26 05:43:022347 bool scroll_on_main_thread = false;
[email protected]edcc1a12014-05-06 01:26:392348 LayerImpl* scrolling_layer_impl =
[email protected]f161ca9e2014-04-01 13:57:012349 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2350 type,
2351 layer_impl,
2352 &scroll_on_main_thread,
2353 &scroll_affects_scroll_handler_);
[email protected]bf1cfd9a2013-09-26 05:43:022354
2355 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022356 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2357 return ScrollOnMainThread;
2358 }
2359
[email protected]edcc1a12014-05-06 01:26:392360 if (scrolling_layer_impl) {
2361 active_tree_->SetCurrentlyScrollingLayer(scrolling_layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272362 should_bubble_scrolls_ = (type != NonBubblingGesture);
2363 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272364 client_->RenewTreePriority();
2365 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2366 return ScrollStarted;
2367 }
2368 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142369}
2370
[email protected]749cbc62014-07-10 01:06:352371InputHandler::ScrollStatus LayerTreeHostImpl::ScrollAnimated(
2372 const gfx::Point& viewport_point,
2373 const gfx::Vector2dF& scroll_delta) {
[email protected]7a7d5be2014-07-18 22:37:262374 if (LayerImpl* layer_impl = CurrentlyScrollingLayer()) {
2375 Animation* animation =
2376 layer_impl->layer_animation_controller()->GetAnimation(
2377 Animation::ScrollOffset);
2378 if (!animation)
2379 return ScrollIgnored;
2380
2381 ScrollOffsetAnimationCurve* curve =
2382 animation->curve()->ToScrollOffsetAnimationCurve();
2383
2384 gfx::Vector2dF new_target = curve->target_value() + scroll_delta;
2385 new_target.SetToMax(gfx::Vector2dF());
2386 new_target.SetToMin(layer_impl->MaxScrollOffset());
2387
[email protected]04c5900d2014-08-18 13:38:362388 curve->UpdateTarget(animation->TrimTimeToCurrentIteration(
2389 CurrentBeginFrameArgs().frame_time),
2390 new_target);
[email protected]7a7d5be2014-07-18 22:37:262391
2392 return ScrollStarted;
[email protected]749cbc62014-07-10 01:06:352393 }
2394 // ScrollAnimated is only used for wheel scrolls. We use the same bubbling
2395 // behavior as ScrollBy to determine which layer to animate, but we do not
2396 // do the Android-specific things in ScrollBy like showing top controls.
2397 InputHandler::ScrollStatus scroll_status = ScrollBegin(viewport_point, Wheel);
2398 if (scroll_status == ScrollStarted) {
2399 gfx::Vector2dF pending_delta = scroll_delta;
2400 for (LayerImpl* layer_impl = CurrentlyScrollingLayer(); layer_impl;
2401 layer_impl = layer_impl->parent()) {
2402 if (!layer_impl->scrollable())
2403 continue;
2404
2405 gfx::Vector2dF current_offset = layer_impl->TotalScrollOffset();
2406 gfx::Vector2dF target_offset = current_offset + pending_delta;
2407 target_offset.SetToMax(gfx::Vector2dF());
2408 target_offset.SetToMin(layer_impl->MaxScrollOffset());
2409 gfx::Vector2dF actual_delta = target_offset - current_offset;
2410
2411 const float kEpsilon = 0.1f;
2412 bool can_layer_scroll = (std::abs(actual_delta.x()) > kEpsilon ||
2413 std::abs(actual_delta.y()) > kEpsilon);
2414
2415 if (!can_layer_scroll) {
2416 layer_impl->ScrollBy(actual_delta);
2417 pending_delta -= actual_delta;
2418 continue;
2419 }
2420
2421 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2422
2423 scoped_ptr<ScrollOffsetAnimationCurve> curve =
2424 ScrollOffsetAnimationCurve::Create(target_offset,
2425 EaseInOutTimingFunction::Create());
2426 curve->SetInitialValue(current_offset);
2427
2428 scoped_ptr<Animation> animation =
[email protected]7a7d5be2014-07-18 22:37:262429 Animation::Create(curve.PassAs<AnimationCurve>(),
[email protected]749cbc62014-07-10 01:06:352430 AnimationIdProvider::NextAnimationId(),
2431 AnimationIdProvider::NextGroupId(),
2432 Animation::ScrollOffset);
2433 animation->set_is_impl_only(true);
2434
2435 layer_impl->layer_animation_controller()->AddAnimation(animation.Pass());
2436
2437 SetNeedsAnimate();
2438 return ScrollStarted;
2439 }
2440 }
2441 ScrollEnd();
2442 return scroll_status;
2443}
2444
[email protected]c1bb5af2013-03-13 19:06:272445gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2446 LayerImpl* layer_impl,
2447 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472448 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122449 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272450 // Layers with non-invertible screen space transforms should not have passed
2451 // the scroll hit test in the first place.
2452 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2453 gfx::Transform inverse_screen_space_transform(
2454 gfx::Transform::kSkipInitialization);
2455 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2456 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352457 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2458 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272459 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142460
[email protected]c1bb5af2013-03-13 19:06:272461 gfx::PointF screen_space_point =
2462 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142463
[email protected]c1bb5af2013-03-13 19:06:272464 gfx::Vector2dF screen_space_delta = viewport_delta;
2465 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2466
2467 // First project the scroll start and end points to local layer space to find
2468 // the scroll delta in layer coordinates.
2469 bool start_clipped, end_clipped;
2470 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2471 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212472 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272473 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212474 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272475 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212476 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272477 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212478 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272479
2480 // In general scroll point coordinates should not get clipped.
2481 DCHECK(!start_clipped);
2482 DCHECK(!end_clipped);
2483 if (start_clipped || end_clipped)
2484 return gfx::Vector2dF();
2485
2486 // local_start_point and local_end_point are in content space but we want to
2487 // move them to layer space for scrolling.
2488 float width_scale = 1.f / layer_impl->contents_scale_x();
2489 float height_scale = 1.f / layer_impl->contents_scale_y();
2490 local_start_point.Scale(width_scale, height_scale);
2491 local_end_point.Scale(width_scale, height_scale);
2492
2493 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472494 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272495 layer_impl->ScrollBy(local_end_point - local_start_point);
2496
2497 // Get the end point in the layer's content space so we can apply its
2498 // ScreenSpaceTransform.
2499 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472500 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272501 previous_delta;
2502 gfx::PointF actual_local_content_end_point =
2503 gfx::ScalePoint(actual_local_end_point,
2504 1.f / width_scale,
2505 1.f / height_scale);
2506
2507 // Calculate the applied scroll delta in viewport space coordinates.
2508 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212509 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272510 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212511 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272512 DCHECK(!end_clipped);
2513 if (end_clipped)
2514 return gfx::Vector2dF();
2515 gfx::PointF actual_viewport_end_point =
2516 gfx::ScalePoint(actual_screen_space_end_point,
2517 1.f / scale_from_viewport_to_screen_space);
2518 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142519}
2520
[email protected]c1bb5af2013-03-13 19:06:272521static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122522 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472523 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272524 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472525 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272526}
2527
[email protected]47a723f2014-03-05 12:42:492528bool LayerTreeHostImpl::ScrollBy(const gfx::Point& viewport_point,
[email protected]3244c9132014-01-23 10:39:122529 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272530 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2531 if (!CurrentlyScrollingLayer())
2532 return false;
2533
2534 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532535 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292536 bool did_scroll_x = false;
2537 bool did_scroll_y = false;
[email protected]f42cffb2014-03-08 18:03:252538 bool did_scroll_top_controls = false;
[email protected]adeda572014-01-31 00:49:472539 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2540 // here?
2541 bool consume_by_top_controls =
2542 top_controls_manager_ &&
2543 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2544 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2545 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2546 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062547
[email protected]c1bb5af2013-03-13 19:06:272548 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2549 layer_impl;
2550 layer_impl = layer_impl->parent()) {
2551 if (!layer_impl->scrollable())
2552 continue;
2553
[email protected]adeda572014-01-31 00:49:472554 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532555 // Only allow bubble scrolling when the scroll is in the direction to make
2556 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472557 gfx::Vector2dF applied_delta;
2558 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532559 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472560 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2561 applied_delta = pending_delta - excess_delta;
2562 pending_delta = excess_delta;
2563 // Force updating of vertical adjust values if needed.
[email protected]f42cffb2014-03-08 18:03:252564 if (applied_delta.y() != 0) {
2565 did_scroll_top_controls = true;
[email protected]adeda572014-01-31 00:49:472566 layer_impl->ScrollbarParametersDidChange();
[email protected]f42cffb2014-03-08 18:03:252567 }
[email protected]a2b5ded2013-05-20 21:32:532568 }
2569 // Track root layer deltas for reporting overscroll.
2570 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422571 }
2572
[email protected]c1bb5af2013-03-13 19:06:272573 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272574 // Gesture events need to be transformed from viewport coordinates to local
2575 // layer coordinates so that the scrolling contents exactly follow the
2576 // user's finger. In contrast, wheel events represent a fixed amount of
2577 // scrolling so we can just apply them directly.
2578 if (!wheel_scrolling_) {
2579 float scale_from_viewport_to_screen_space = device_scale_factor_;
2580 applied_delta =
2581 ScrollLayerWithViewportSpaceDelta(layer_impl,
2582 scale_from_viewport_to_screen_space,
2583 viewport_point, pending_delta);
2584 } else {
2585 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142586 }
[email protected]94f206c12012-08-25 00:09:142587
[email protected]9328ede2014-04-04 07:45:522588 const float kEpsilon = 0.1f;
[email protected]5b8a865c2014-04-03 00:35:512589 if (layer_impl == InnerViewportScrollLayer()) {
2590 unused_root_delta.Subtract(applied_delta);
[email protected]9328ede2014-04-04 07:45:522591 if (std::abs(unused_root_delta.x()) < kEpsilon)
[email protected]5b8a865c2014-04-03 00:35:512592 unused_root_delta.set_x(0.0f);
[email protected]9328ede2014-04-04 07:45:522593 if (std::abs(unused_root_delta.y()) < kEpsilon)
[email protected]5b8a865c2014-04-03 00:35:512594 unused_root_delta.set_y(0.0f);
[email protected]e9eb23f2014-04-11 13:42:032595 // Disable overscroll on axes which is impossible to scroll.
2596 if (settings_.report_overscroll_only_for_scrollable_axes) {
2597 if (std::abs(active_tree_->TotalMaxScrollOffset().x()) <= kEpsilon)
2598 unused_root_delta.set_x(0.0f);
2599 if (std::abs(active_tree_->TotalMaxScrollOffset().y()) <= kEpsilon)
2600 unused_root_delta.set_y(0.0f);
2601 }
[email protected]5b8a865c2014-04-03 00:35:512602 }
2603
[email protected]c1bb5af2013-03-13 19:06:272604 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]9328ede2014-04-04 07:45:522605 bool did_move_layer_x = std::abs(applied_delta.x()) > kEpsilon;
2606 bool did_move_layer_y = std::abs(applied_delta.y()) > kEpsilon;
[email protected]2bd503f2013-07-23 05:35:292607 did_scroll_x |= did_move_layer_x;
2608 did_scroll_y |= did_move_layer_y;
2609 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142610 // Scrolls should always bubble between the outer and inner viewports
2611 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2612 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272613 continue;
[email protected]db102612013-12-20 22:04:122614 else
2615 break;
[email protected]94f206c12012-08-25 00:09:142616 }
[email protected]a2b5ded2013-05-20 21:32:532617
[email protected]c1bb5af2013-03-13 19:06:272618 did_lock_scrolling_layer_ = true;
2619 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202620 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272621 break;
[email protected]94f206c12012-08-25 00:09:142622 }
[email protected]94f206c12012-08-25 00:09:142623
[email protected]c1bb5af2013-03-13 19:06:272624 // If the applied delta is within 45 degrees of the input delta, bail out to
2625 // make it easier to scroll just one layer in one direction without
2626 // affecting any of its parents.
2627 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212628 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272629 applied_delta, pending_delta) < angle_threshold) {
2630 pending_delta = gfx::Vector2d();
2631 break;
[email protected]4a23c374c2012-12-08 08:38:552632 }
[email protected]c1bb5af2013-03-13 19:06:272633
2634 // Allow further movement only on an axis perpendicular to the direction in
2635 // which the layer moved.
2636 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212637 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272638
[email protected]a2b5ded2013-05-20 21:32:532639 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272640 break;
2641 }
2642
[email protected]f42cffb2014-03-08 18:03:252643 bool did_scroll_content = did_scroll_x || did_scroll_y;
2644 if (did_scroll_content) {
[email protected]dab0a422014-08-13 16:09:462645 // If we are scrolling with an active scroll handler, forward latency
2646 // tracking information to the main thread so the delay introduced by the
2647 // handler is accounted for.
2648 if (scroll_affects_scroll_handler())
2649 NotifySwapPromiseMonitorsOfForwardingToMainThread();
[email protected]c1bb5af2013-03-13 19:06:272650 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442651 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272652 client_->RenewTreePriority();
2653 }
[email protected]a2b5ded2013-05-20 21:32:532654
[email protected]2bd503f2013-07-23 05:35:292655 // Scrolling along an axis resets accumulated root overscroll for that axis.
2656 if (did_scroll_x)
2657 accumulated_root_overscroll_.set_x(0);
2658 if (did_scroll_y)
2659 accumulated_root_overscroll_.set_y(0);
2660
[email protected]a2b5ded2013-05-20 21:32:532661 accumulated_root_overscroll_ += unused_root_delta;
[email protected]485a42dc2014-03-26 22:44:342662 bool did_overscroll = !unused_root_delta.IsZero();
[email protected]a2b5ded2013-05-20 21:32:532663 if (did_overscroll && input_handler_client_) {
[email protected]cffc3282014-08-15 23:38:242664 input_handler_client_->DidOverscroll(
2665 viewport_point, accumulated_root_overscroll_, unused_root_delta);
[email protected]a2b5ded2013-05-20 21:32:532666 }
2667
[email protected]f42cffb2014-03-08 18:03:252668 return did_scroll_content || did_scroll_top_controls;
[email protected]4a23c374c2012-12-08 08:38:552669}
2670
[email protected]be782f52013-03-23 21:36:142671// This implements scrolling by page as described here:
2672// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2673// for events with WHEEL_PAGESCROLL set.
[email protected]47a723f2014-03-05 12:42:492674bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
[email protected]c28df4c12013-05-22 17:36:492675 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142676 DCHECK(wheel_scrolling_);
2677
2678 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2679 layer_impl;
2680 layer_impl = layer_impl->parent()) {
2681 if (!layer_impl->scrollable())
2682 continue;
2683
[email protected]adeda572014-01-31 00:49:472684 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142685 continue;
2686
[email protected]adeda572014-01-31 00:49:472687 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142688
2689 // These magical values match WebKit and are designed to scroll nearly the
2690 // entire visible content height but leave a bit of overlap.
2691 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492692 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142693 page = -page;
2694
2695 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2696
2697 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2698
2699 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142700 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442701 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142702 client_->RenewTreePriority();
2703 return true;
2704 }
2705
2706 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2707 }
2708
2709 return false;
2710}
2711
[email protected]1960a712013-04-30 17:06:472712void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2713 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2714 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2715 active_tree_->SetRootLayerScrollOffsetDelegate(
2716 root_layer_scroll_offset_delegate_);
2717}
2718
2719void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2720 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2721 client_->SetNeedsCommitOnImplThread();
[email protected]b4a3d462014-06-17 09:17:082722 active_tree_->set_needs_update_draw_properties();
[email protected]1960a712013-04-30 17:06:472723}
2724
[email protected]c1bb5af2013-03-13 19:06:272725void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2726 active_tree_->ClearCurrentlyScrollingLayer();
2727 did_lock_scrolling_layer_ = false;
[email protected]f161ca9e2014-04-01 13:57:012728 scroll_affects_scroll_handler_ = false;
[email protected]a2b5ded2013-05-20 21:32:532729 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142730}
2731
[email protected]c1bb5af2013-03-13 19:06:272732void LayerTreeHostImpl::ScrollEnd() {
2733 if (top_controls_manager_)
2734 top_controls_manager_->ScrollEnd();
2735 ClearCurrentlyScrollingLayer();
[email protected]94f206c12012-08-25 00:09:142736}
2737
[email protected]5ff3c9782013-04-29 17:35:122738InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462739 if (!active_tree_->CurrentlyScrollingLayer())
2740 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212741
[email protected]75147f52013-08-02 19:11:462742 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472743 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2744 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462745 ClearCurrentlyScrollingLayer();
2746 return ScrollIgnored;
2747 }
2748
[email protected]db102612013-12-20 22:04:122749 if (!wheel_scrolling_) {
2750 // Allow the fling to lock to the first layer that moves after the initial
2751 // fling |ScrollBy()| event.
2752 did_lock_scrolling_layer_ = false;
2753 should_bubble_scrolls_ = false;
2754 }
[email protected]df0c42342013-10-08 20:52:122755
[email protected]75147f52013-08-02 19:11:462756 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212757}
2758
[email protected]bf1cfd9a2013-09-26 05:43:022759float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472760 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022761 LayerImpl* layer_impl) {
2762 if (!layer_impl)
2763 return std::numeric_limits<float>::max();
2764
2765 gfx::Rect layer_impl_bounds(
2766 layer_impl->content_bounds());
2767
2768 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2769 layer_impl->screen_space_transform(),
2770 layer_impl_bounds);
2771
2772 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2773 device_viewport_point);
2774}
2775
[email protected]47a723f2014-03-05 12:42:492776void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242777 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2778 device_scale_factor_);
[email protected]28336d52014-05-12 19:07:282779 LayerImpl* layer_impl =
2780 active_tree_->FindLayerThatIsHitByPoint(device_viewport_point);
[email protected]f620b0e72013-10-01 21:38:242781 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2782 return;
2783
2784 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2785 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2786 scroll_layer_id_when_mouse_over_scrollbar_);
2787
[email protected]5cd9ac12014-02-05 21:48:532788 // The check for a null scroll_layer_impl below was added to see if it will
2789 // eliminate the crashes described in http://crbug.com/326635.
2790 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242791 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532792 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2793 : NULL;
[email protected]930ff43b2014-05-02 05:24:002794 if (animation_controller)
2795 animation_controller->DidMouseMoveOffScrollbar();
[email protected]f620b0e72013-10-01 21:38:242796 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2797 }
2798
[email protected]bf1cfd9a2013-09-26 05:43:022799 bool scroll_on_main_thread = false;
[email protected]f161ca9e2014-04-01 13:57:012800 LayerImpl* scroll_layer_impl =
2801 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2802 InputHandler::Gesture,
2803 layer_impl,
2804 &scroll_on_main_thread,
2805 NULL);
[email protected]bf1cfd9a2013-09-26 05:43:022806 if (scroll_on_main_thread || !scroll_layer_impl)
2807 return;
2808
2809 ScrollbarAnimationController* animation_controller =
2810 scroll_layer_impl->scrollbar_animation_controller();
2811 if (!animation_controller)
2812 return;
2813
[email protected]adeda572014-01-31 00:49:472814 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2815 float distance_to_scrollbar = std::numeric_limits<float>::max();
2816 for (LayerImpl::ScrollbarSet::iterator it =
2817 scroll_layer_impl->scrollbars()->begin();
2818 it != scroll_layer_impl->scrollbars()->end();
2819 ++it)
2820 distance_to_scrollbar =
2821 std::min(distance_to_scrollbar,
2822 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022823
[email protected]930ff43b2014-05-02 05:24:002824 animation_controller->DidMouseMoveNear(distance_to_scrollbar /
2825 device_scale_factor_);
[email protected]bf1cfd9a2013-09-26 05:43:022826}
2827
[email protected]f620b0e72013-10-01 21:38:242828bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472829 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242830 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2831 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2832 layer_impl = active_tree_->LayerById(scroll_layer_id);
2833 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2834 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]930ff43b2014-05-02 05:24:002835 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(0);
[email protected]f620b0e72013-10-01 21:38:242836 } else {
2837 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2838 }
2839
2840 return true;
2841 }
2842
2843 return false;
2844}
2845
[email protected]c1bb5af2013-03-13 19:06:272846void LayerTreeHostImpl::PinchGestureBegin() {
2847 pinch_gesture_active_ = true;
2848 previous_pinch_anchor_ = gfx::Point();
2849 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132850 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472851 if (active_tree_->OuterViewportScrollLayer()) {
2852 active_tree_->SetCurrentlyScrollingLayer(
2853 active_tree_->OuterViewportScrollLayer());
2854 } else {
2855 active_tree_->SetCurrentlyScrollingLayer(
2856 active_tree_->InnerViewportScrollLayer());
2857 }
[email protected]a9bda5e2013-10-25 18:43:372858 if (top_controls_manager_)
2859 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142860}
2861
[email protected]c1bb5af2013-03-13 19:06:272862void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
[email protected]47a723f2014-03-05 12:42:492863 const gfx::Point& anchor) {
[email protected]b93480862014-02-20 13:07:032864 if (!InnerViewportScrollLayer())
2865 return;
[email protected]d3afa112012-12-08 06:24:282866
[email protected]b93480862014-02-20 13:07:032867 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282868
[email protected]ec2322e2014-05-15 16:32:002869 // For a moment the scroll offset ends up being outside of the max range. This
2870 // confuses the delegate so we switch it off till after we're done processing
2871 // the pinch update.
2872 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
2873
[email protected]c1bb5af2013-03-13 19:06:272874 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2875 // position over the course of the magnify.
2876 float page_scale_delta = active_tree_->page_scale_delta();
2877 gfx::PointF previous_scale_anchor =
2878 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2879 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2880 page_scale_delta = active_tree_->page_scale_delta();
2881 gfx::PointF new_scale_anchor =
2882 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2883 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2884
2885 previous_pinch_anchor_ = anchor;
2886
2887 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472888 // If clamping the inner viewport scroll offset causes a change, it should
2889 // be accounted for from the intended move.
2890 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272891
[email protected]adeda572014-01-31 00:49:472892 // We manually manage the bubbling behaviour here as it is different to that
2893 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2894 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2895 // 2) we don't want the directional limitations on the unused parts that
2896 // ScrollBy() implements, and
2897 // 3) pinching should not engage the top controls manager.
2898 gfx::Vector2dF unused = OuterViewportScrollLayer()
2899 ? OuterViewportScrollLayer()->ScrollBy(move)
2900 : move;
2901
2902 if (!unused.IsZero()) {
2903 InnerViewportScrollLayer()->ScrollBy(unused);
2904 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2905 }
[email protected]c1bb5af2013-03-13 19:06:272906
[email protected]ec2322e2014-05-15 16:32:002907 active_tree_->SetRootLayerScrollOffsetDelegate(
2908 root_layer_scroll_offset_delegate_);
2909
[email protected]c1bb5af2013-03-13 19:06:272910 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442911 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272912 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282913}
2914
[email protected]c1bb5af2013-03-13 19:06:272915void LayerTreeHostImpl::PinchGestureEnd() {
2916 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132917 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2918 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2919 ClearCurrentlyScrollingLayer();
2920 }
[email protected]a9bda5e2013-10-25 18:43:372921 if (top_controls_manager_)
2922 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272923 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142924}
2925
[email protected]c1bb5af2013-03-13 19:06:272926static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2927 LayerImpl* layer_impl) {
2928 if (!layer_impl)
2929 return;
[email protected]94f206c12012-08-25 00:09:142930
[email protected]c1bb5af2013-03-13 19:06:272931 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472932 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272933 if (!scroll_delta.IsZero()) {
2934 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392935 scroll.layer_id = layer_impl->id();
2936 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272937 scroll_info->scrolls.push_back(scroll);
2938 layer_impl->SetSentScrollDelta(scroll_delta);
2939 }
[email protected]94f206c12012-08-25 00:09:142940
[email protected]c1bb5af2013-03-13 19:06:272941 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2942 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142943}
2944
[email protected]c1bb5af2013-03-13 19:06:272945scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2946 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302947
[email protected]c1bb5af2013-03-13 19:06:272948 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392949 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2950 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]dab0a422014-08-13 16:09:462951 scroll_info->swap_promises.swap(swap_promises_for_main_thread_scroll_update_);
bokan88eae012014-09-09 20:40:422952 scroll_info->top_controls_delta = active_tree()->top_controls_delta();
2953 active_tree_->set_sent_top_controls_delta(scroll_info->top_controls_delta);
[email protected]362f1e8b2013-01-21 16:54:302954
[email protected]c1bb5af2013-03-13 19:06:272955 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302956}
2957
[email protected]c1bb5af2013-03-13 19:06:272958void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282959 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102960}
2961
[email protected]adeda572014-01-31 00:49:472962void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2963 DCHECK(InnerViewportScrollLayer());
2964 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2965 ? OuterViewportScrollLayer()
2966 : InnerViewportScrollLayer();
2967
2968 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2969
2970 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2971 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2972}
2973
[email protected]27152642014-03-11 20:42:002974void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks monotonic_time) {
[email protected]adeda572014-01-31 00:49:472975 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272976 return;
2977
[email protected]adeda572014-01-31 00:49:472978 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272979
[email protected]34e1b512013-09-26 17:32:332980 if (!page_scale_animation_->IsAnimationStarted())
[email protected]ac22516a2014-05-15 17:31:362981 page_scale_animation_->StartAnimation(monotonic_time);
[email protected]34e1b512013-09-26 17:32:332982
[email protected]ac22516a2014-05-15 17:31:362983 active_tree_->SetPageScaleDelta(
2984 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2985 active_tree_->page_scale_factor());
2986 gfx::Vector2dF next_scroll =
2987 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:272988
[email protected]adeda572014-01-31 00:49:472989 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442990 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272991
[email protected]ac22516a2014-05-15 17:31:362992 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
[email protected]c1bb5af2013-03-13 19:06:272993 page_scale_animation_.reset();
2994 client_->SetNeedsCommitOnImplThread();
2995 client_->RenewTreePriority();
[email protected]43b8f982014-04-30 21:24:332996 } else {
2997 SetNeedsAnimate();
[email protected]c1bb5af2013-03-13 19:06:272998 }
[email protected]829ad972013-01-28 23:36:102999}
3000
[email protected]ffb2720f2013-03-15 19:18:373001void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:083002 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:373003 return;
[email protected]ac3446d2014-08-13 08:51:203004
[email protected]ffb2720f2013-03-15 19:18:373005 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]ac3446d2014-08-13 08:51:203006
3007 if (top_controls_manager_->animation())
3008 SetNeedsAnimate();
3009
[email protected]adeda572014-01-31 00:49:473010 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:173011 return;
[email protected]ac3446d2014-08-13 08:51:203012
3013 if (scroll.IsZero())
3014 return;
3015
3016 ScrollViewportBy(gfx::ScaleVector2d(
3017 scroll, 1.f / active_tree_->total_page_scale_factor()));
3018 SetNeedsRedraw();
3019 client_->SetNeedsCommitOnImplThread();
3020 client_->RenewTreePriority();
[email protected]ffb2720f2013-03-15 19:18:373021}
3022
[email protected]27152642014-03-11 20:42:003023void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time) {
[email protected]8e0176d2013-03-21 03:14:523024 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:363025 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:273026 !active_tree_->root_layer())
3027 return;
3028
3029 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
[email protected]c1bb5af2013-03-13 19:06:273030 AnimationRegistrar::AnimationControllerMap copy =
3031 animation_registrar_->active_animation_controllers();
3032 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
3033 iter != copy.end();
3034 ++iter)
[email protected]ac22516a2014-05-15 17:31:363035 (*iter).second->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:273036
[email protected]43b8f982014-04-30 21:24:333037 SetNeedsAnimate();
[email protected]131a0c22013-02-12 18:31:083038}
3039
[email protected]3d9f7432013-04-06 00:35:183040void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:523041 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:363042 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:273043 !active_tree_->root_layer())
3044 return;
3045
3046 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
3047 scoped_ptr<AnimationEventsVector> events =
3048 make_scoped_ptr(new AnimationEventsVector);
3049 AnimationRegistrar::AnimationControllerMap copy =
3050 animation_registrar_->active_animation_controllers();
3051 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
3052 iter != copy.end();
3053 ++iter)
[email protected]3d9f7432013-04-06 00:35:183054 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:273055
3056 if (!events->empty()) {
[email protected]85b57502014-03-11 15:37:483057 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass());
[email protected]c1bb5af2013-03-13 19:06:273058 }
[email protected]43b8f982014-04-30 21:24:333059
3060 SetNeedsAnimate();
[email protected]131a0c22013-02-12 18:31:083061}
3062
[email protected]b33348f2014-04-30 18:17:343063void LayerTreeHostImpl::ActivateAnimations() {
3064 if (!settings_.accelerated_animation_enabled || !needs_animate_layers() ||
3065 !active_tree_->root_layer())
3066 return;
3067
3068 TRACE_EVENT0("cc", "LayerTreeHostImpl::ActivateAnimations");
3069 AnimationRegistrar::AnimationControllerMap copy =
3070 animation_registrar_->active_animation_controllers();
3071 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
3072 iter != copy.end();
3073 ++iter)
3074 (*iter).second->ActivateAnimations();
3075}
3076
[email protected]c1bb5af2013-03-13 19:06:273077base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
3078 return base::TimeDelta::FromSeconds(1);
3079}
3080
[email protected]c1bb5af2013-03-13 19:06:273081std::string LayerTreeHostImpl::LayerTreeAsJson() const {
3082 std::string str;
3083 if (active_tree_->root_layer()) {
3084 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
3085 base::JSONWriter::WriteWithOptions(
3086 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
3087 }
3088 return str;
3089}
3090
[email protected]c1bb5af2013-03-13 19:06:273091int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:363092 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:273093}
3094
[email protected]c1bb5af2013-03-13 19:06:273095void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
3096 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
3097}
3098
3099void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
3100 base::TimeTicks time) {
3101 if (!layer)
3102 return;
3103
3104 ScrollbarAnimationController* scrollbar_controller =
3105 layer->scrollbar_animation_controller();
[email protected]930ff43b2014-05-02 05:24:003106 if (scrollbar_controller)
3107 scrollbar_controller->Animate(time);
[email protected]c1bb5af2013-03-13 19:06:273108
3109 for (size_t i = 0; i < layer->children().size(); ++i)
3110 AnimateScrollbarsRecursive(layer->children()[i], time);
3111}
3112
[email protected]930ff43b2014-05-02 05:24:003113void LayerTreeHostImpl::PostDelayedScrollbarFade(
3114 const base::Closure& start_fade,
3115 base::TimeDelta delay) {
3116 client_->PostDelayedScrollbarFadeOnImplThread(start_fade, delay);
[email protected]0fc818e2013-03-18 06:45:203117}
3118
[email protected]930ff43b2014-05-02 05:24:003119void LayerTreeHostImpl::SetNeedsScrollbarAnimationFrame() {
3120 TRACE_EVENT_INSTANT0(
3121 "cc",
3122 "LayerTreeHostImpl::SetNeedsRedraw due to scrollbar fade",
3123 TRACE_EVENT_SCOPE_THREAD);
3124 SetNeedsAnimate();
[email protected]0fc818e2013-03-18 06:45:203125}
3126
[email protected]c1bb5af2013-03-13 19:06:273127void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
3128 if (!tile_manager_)
3129 return;
3130
[email protected]1bcced22013-09-24 13:51:193131 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:273132 return;
[email protected]1bcced22013-09-24 13:51:193133 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:083134 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:273135}
3136
[email protected]04c5900d2014-08-18 13:38:363137void LayerTreeHostImpl::UpdateCurrentBeginFrameArgs(
3138 const BeginFrameArgs& args) {
3139 DCHECK(!current_begin_frame_args_.IsValid());
3140 current_begin_frame_args_ = args;
3141 // TODO(skyostil): Stop overriding the frame time once the usage of frame
3142 // timing is unified.
3143 current_begin_frame_args_.frame_time = gfx::FrameTime::Now();
[email protected]1dc06162014-03-26 22:54:453144}
3145
[email protected]04c5900d2014-08-18 13:38:363146void LayerTreeHostImpl::ResetCurrentBeginFrameArgsForNextFrame() {
3147 current_begin_frame_args_ = BeginFrameArgs();
[email protected]c1bb5af2013-03-13 19:06:273148}
3149
[email protected]04c5900d2014-08-18 13:38:363150BeginFrameArgs LayerTreeHostImpl::CurrentBeginFrameArgs() const {
[email protected]1dc06162014-03-26 22:54:453151 // Try to use the current frame time to keep animations non-jittery. But if
3152 // we're not in a frame (because this is during an input event or a delayed
3153 // task), fall back to physical time. This should still be monotonic.
[email protected]04c5900d2014-08-18 13:38:363154 if (current_begin_frame_args_.IsValid())
3155 return current_begin_frame_args_;
3156 return BeginFrameArgs::Create(gfx::FrameTime::Now(),
3157 base::TimeTicks(),
3158 BeginFrameArgs::DefaultInterval());
[email protected]21c9dee72013-06-15 01:20:053159}
3160
[email protected]d12aa932014-08-01 13:10:383161scoped_refptr<base::debug::ConvertableToTraceFormat>
3162LayerTreeHostImpl::AsValue() const {
3163 return AsValueWithFrame(NULL);
[email protected]f6742f52013-05-08 23:52:223164}
3165
[email protected]d12aa932014-08-01 13:10:383166scoped_refptr<base::debug::ConvertableToTraceFormat>
3167LayerTreeHostImpl::AsValueWithFrame(FrameData* frame) const {
3168 scoped_refptr<base::debug::TracedValue> state =
3169 new base::debug::TracedValue();
3170 AsValueWithFrameInto(frame, state.get());
3171 return state;
3172}
3173
3174void LayerTreeHostImpl::AsValueWithFrameInto(
3175 FrameData* frame,
3176 base::debug::TracedValue* state) const {
3177 if (this->pending_tree_) {
3178 state->BeginDictionary("activation_state");
3179 ActivationStateAsValueInto(state);
3180 state->EndDictionary();
3181 }
3182 state->BeginDictionary("device_viewport_size");
3183 MathUtil::AddToTracedValue(device_viewport_size_, state);
3184 state->EndDictionary();
[email protected]0eded9ea2014-08-14 03:39:203185
vmpstrd7de03c2014-08-27 18:11:013186 std::set<const Tile*> tiles;
3187 active_tree_->GetAllTilesForTracing(&tiles);
3188 if (pending_tree_)
3189 pending_tree_->GetAllTilesForTracing(&tiles);
3190
3191 state->BeginArray("active_tiles");
3192 for (std::set<const Tile*>::const_iterator it = tiles.begin();
3193 it != tiles.end();
3194 ++it) {
3195 const Tile* tile = *it;
3196
3197 state->BeginDictionary();
3198 tile->AsValueInto(state);
3199 state->EndDictionary();
3200 }
3201 state->EndArray();
3202
3203 if (tile_manager_) {
[email protected]0eded9ea2014-08-14 03:39:203204 state->BeginDictionary("tile_manager_basic_state");
3205 tile_manager_->BasicStateAsValueInto(state);
3206 state->EndDictionary();
[email protected]d12aa932014-08-01 13:10:383207 }
3208 state->BeginDictionary("active_tree");
3209 active_tree_->AsValueInto(state);
3210 state->EndDictionary();
3211 if (pending_tree_) {
3212 state->BeginDictionary("pending_tree");
3213 pending_tree_->AsValueInto(state);
3214 state->EndDictionary();
3215 }
3216 if (frame) {
3217 state->BeginDictionary("frame");
3218 frame->AsValueInto(state);
3219 state->EndDictionary();
3220 }
3221}
3222
3223scoped_refptr<base::debug::ConvertableToTraceFormat>
3224LayerTreeHostImpl::ActivationStateAsValue() const {
3225 scoped_refptr<base::debug::TracedValue> state =
3226 new base::debug::TracedValue();
3227 ActivationStateAsValueInto(state.get());
3228 return state;
3229}
3230
3231void LayerTreeHostImpl::ActivationStateAsValueInto(
3232 base::debug::TracedValue* state) const {
3233 TracedValue::SetIDRef(this, state, "lthi");
3234 if (tile_manager_) {
3235 state->BeginDictionary("tile_manager");
3236 tile_manager_->BasicStateAsValueInto(state);
3237 state->EndDictionary();
3238 }
[email protected]131a0c22013-02-12 18:31:083239}
3240
[email protected]6e7fdeb2013-07-09 14:28:383241void LayerTreeHostImpl::SetDebugState(
3242 const LayerTreeDebugState& new_debug_state) {
3243 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
3244 return;
3245 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:273246 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:243247
[email protected]6e7fdeb2013-07-09 14:28:383248 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:123249 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:383250 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:023251}
3252
[email protected]741fba422013-09-20 03:34:143253void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
3254 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:573255 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:573256
[email protected]efa48412013-09-05 15:30:163257 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:143258 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:163259 case UIResourceBitmap::CLAMP_TO_EDGE:
3260 wrap_mode = GL_CLAMP_TO_EDGE;
3261 break;
3262 case UIResourceBitmap::REPEAT:
3263 wrap_mode = GL_REPEAT;
3264 break;
3265 }
3266
[email protected]c9280762013-08-01 06:28:573267 // Allow for multiple creation requests with the same UIResourceId. The
3268 // previous resource is simply deleted.
3269 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
3270 if (id)
3271 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:123272
3273 ResourceFormat format = resource_provider_->best_texture_format();
[email protected]9b05baa2014-08-14 00:43:313274 switch (bitmap.GetFormat()) {
3275 case UIResourceBitmap::RGBA8:
3276 break;
3277 case UIResourceBitmap::ALPHA_8:
3278 format = ALPHA_8;
3279 break;
3280 case UIResourceBitmap::ETC1:
3281 format = ETC1;
3282 break;
[email protected]cffc3282014-08-15 23:38:243283 }
[email protected]655041f2014-08-21 22:58:583284 id =
3285 resource_provider_->CreateResource(bitmap.GetSize(),
3286 wrap_mode,
3287 ResourceProvider::TextureHintImmutable,
3288 format);
[email protected]c9280762013-08-01 06:28:573289
[email protected]efbdb3a2013-10-04 00:35:133290 UIResourceData data;
3291 data.resource_id = id;
3292 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:513293 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:133294
3295 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:033296
3297 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:573298 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:033299 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:143300 gfx::Rect(bitmap.GetSize()),
3301 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:573302 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:483303 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:573304}
3305
3306void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
3307 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
3308 if (id) {
3309 resource_provider_->DeleteResource(id);
3310 ui_resource_map_.erase(uid);
3311 }
[email protected]127bdc1a2013-09-11 01:44:483312 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:573313}
3314
[email protected]127bdc1a2013-09-11 01:44:483315void LayerTreeHostImpl::EvictAllUIResources() {
3316 if (ui_resource_map_.empty())
3317 return;
3318
[email protected]5f4dc4f2013-09-05 14:58:213319 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
3320 iter != ui_resource_map_.end();
3321 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:483322 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:133323 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:213324 }
3325 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:483326
3327 client_->SetNeedsCommitOnImplThread();
3328 client_->OnCanDrawStateChanged(CanDraw());
3329 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:213330}
3331
[email protected]c9280762013-08-01 06:28:573332ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
3333 UIResourceId uid) const {
3334 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3335 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:133336 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:573337 return 0;
3338}
3339
[email protected]709c9542013-10-26 01:43:513340bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
3341 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3342 DCHECK(iter != ui_resource_map_.end());
3343 return iter->second.opaque;
3344}
3345
[email protected]127bdc1a2013-09-11 01:44:483346bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
3347 return !evicted_ui_resources_.empty();
3348}
3349
3350void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
3351 std::set<UIResourceId>::iterator found_in_evicted =
3352 evicted_ui_resources_.find(uid);
3353 if (found_in_evicted == evicted_ui_resources_.end())
3354 return;
3355 evicted_ui_resources_.erase(found_in_evicted);
3356 if (evicted_ui_resources_.empty())
3357 client_->OnCanDrawStateChanged(CanDraw());
3358}
3359
[email protected]5e5648a2013-11-18 00:39:333360void LayerTreeHostImpl::ScheduleMicroBenchmark(
3361 scoped_ptr<MicroBenchmarkImpl> benchmark) {
3362 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
3363}
3364
[email protected]6be422b2013-12-08 06:47:313365void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3366 swap_promise_monitor_.insert(monitor);
3367}
3368
3369void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3370 swap_promise_monitor_.erase(monitor);
3371}
3372
3373void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3374 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3375 for (; it != swap_promise_monitor_.end(); it++)
3376 (*it)->OnSetNeedsRedrawOnImpl();
3377}
3378
[email protected]dab0a422014-08-13 16:09:463379void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfForwardingToMainThread() {
3380 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3381 for (; it != swap_promise_monitor_.end(); it++)
3382 (*it)->OnForwardScrollUpdateToMainThreadOnImpl();
3383}
3384
[email protected]8aa39ecb2014-06-12 14:19:143385void LayerTreeHostImpl::RegisterPictureLayerImpl(PictureLayerImpl* layer) {
3386 DCHECK(std::find(picture_layers_.begin(), picture_layers_.end(), layer) ==
3387 picture_layers_.end());
3388 picture_layers_.push_back(layer);
3389}
3390
3391void LayerTreeHostImpl::UnregisterPictureLayerImpl(PictureLayerImpl* layer) {
3392 std::vector<PictureLayerImpl*>::iterator it =
3393 std::find(picture_layers_.begin(), picture_layers_.end(), layer);
3394 DCHECK(it != picture_layers_.end());
3395 picture_layers_.erase(it);
3396}
3397
[email protected]d3143c732012-10-05 19:17:593398} // namespace cc