blob: 60c5f6bb73b8a7d55489eca27da385f246a09b7a [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
[email protected]b6eb8e332013-09-10 00:51:018#include <limits>
[email protected]ac7c7f52012-11-08 06:26:509
[email protected]ad5d1422012-10-19 13:40:2910#include "base/basictypes.h"
[email protected]34806722013-08-09 23:51:2111#include "base/containers/hash_tables.h"
[email protected]4a23c374c2012-12-08 08:38:5512#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1413#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3414#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4815#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0916#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4017#include "cc/animation/timing_function.h"
[email protected]6be422b2013-12-08 06:47:3118#include "cc/base/latency_info_swap_promise_monitor.h"
[email protected]681ccff2013-03-18 06:13:5219#include "cc/base/math_util.h"
20#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3321#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2722#include "cc/debug/debug_rect_history.h"
[email protected]dbe759a2013-12-02 19:23:0223#include "cc/debug/devtools_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2724#include "cc/debug/frame_rate_counter.h"
25#include "cc/debug/overdraw_metrics.h"
26#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4327#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2228#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2129#include "cc/input/page_scale_animation.h"
30#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5231#include "cc/layers/append_quads_data.h"
32#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2833#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5234#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1735#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2836#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0237#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3038#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4539#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3040#include "cc/output/delegating_renderer.h"
41#include "cc/output/gl_renderer.h"
42#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5643#include "cc/quads/render_pass_draw_quad.h"
44#include "cc/quads/shared_quad_state.h"
45#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1246#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4047#include "cc/resources/memory_history.h"
48#include "cc/resources/picture_layer_tiling.h"
49#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1150#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5751#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3152#include "cc/scheduler/delay_based_time_source.h"
53#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0454#include "cc/trees/damage_tracker.h"
55#include "cc/trees/layer_tree_host.h"
56#include "cc/trees/layer_tree_host_common.h"
57#include "cc/trees/layer_tree_impl.h"
58#include "cc/trees/quad_culler.h"
59#include "cc/trees/single_thread_proxy.h"
60#include "cc/trees/tree_synchronizer.h"
[email protected]8d7aa012013-11-23 21:19:1261#include "gpu/GLES2/gl2extchromium.h"
[email protected]de2cf8c2013-10-25 19:46:4662#include "ui/gfx/frame_time.h"
[email protected]d455d552012-11-02 00:19:0663#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1364#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1465
[email protected]94f206c12012-08-25 00:09:1466namespace {
67
[email protected]c1bb5af2013-03-13 19:06:2768void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
69 if (visible) {
70 TRACE_EVENT_ASYNC_BEGIN1("webkit",
71 "LayerTreeHostImpl::SetVisible",
72 id,
73 "LayerTreeHostImpl",
74 id);
75 return;
76 }
[email protected]94f206c12012-08-25 00:09:1477
[email protected]c1bb5af2013-03-13 19:06:2778 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1479}
80
[email protected]b6eb8e332013-09-10 00:51:0181size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
[email protected]b4664142013-11-08 00:50:3182 // Software compositing should not use this value in production. Just use a
83 // default value when testing uploads with the software compositor.
84 if (!context_provider)
[email protected]b6eb8e332013-09-10 00:51:0185 return std::numeric_limits<size_t>::max();
[email protected]b4664142013-11-08 00:50:3186
87 // We want to make sure the default transfer buffer size is equal to the
88 // amount of data that can be uploaded by the compositor to avoid stalling
89 // the pipeline.
90 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
91 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
92 // Assuming a two frame deep pipeline between CPU and GPU and we are
93 // drawing 60 frames per second which would require us to draw one
94 // frame in 16 milliseconds.
95 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
96 return std::min(
97 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
98 kMaxTransferBufferUsageBytes);
99}
100
101size_t GetMaxRasterTasksUsageBytes(cc::ContextProvider* context_provider) {
102 // Transfer-buffer/raster-tasks limits are different but related. We make
103 // equal here, as this is ideal when using transfer buffers. When not using
104 // transfer buffers we should still limit raster to something similar, to
105 // preserve caching behavior (and limit memory waste when priorities change).
106 return GetMaxTransferBufferUsageBytes(context_provider);
[email protected]b6eb8e332013-09-10 00:51:01107}
108
[email protected]8d7aa012013-11-23 21:19:12109GLenum GetMapImageTextureTarget(cc::ContextProvider* context_provider) {
110 if (!context_provider)
111 return GL_TEXTURE_2D;
112
113 // TODO(reveman): Determine if GL_TEXTURE_EXTERNAL_OES works well on
114 // Android before we enable this. crbug.com/322780
115#if !defined(OS_ANDROID)
[email protected]900db4412014-01-12 02:19:01116 if (context_provider->ContextCapabilities().gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12117 return GL_TEXTURE_EXTERNAL_OES;
[email protected]900db4412014-01-12 02:19:01118 if (context_provider->ContextCapabilities().gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12119 return GL_TEXTURE_RECTANGLE_ARB;
120#endif
121
122 return GL_TEXTURE_2D;
123}
124
[email protected]c1bb5af2013-03-13 19:06:27125} // namespace
[email protected]94f206c12012-08-25 00:09:14126
[email protected]9c88e562012-09-14 22:21:30127namespace cc {
[email protected]94f206c12012-08-25 00:09:14128
[email protected]96baf3e2012-10-22 23:09:55129class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27130 public:
131 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
132 LayerTreeHostImpl* layer_tree_host_impl,
133 scoped_refptr<DelayBasedTimeSource> time_source) {
134 return make_scoped_ptr(
135 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
136 time_source));
137 }
138 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26139 time_source_->SetClient(NULL);
140 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27141 }
142
[email protected]6d0e69d2013-03-20 14:53:26143 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27144 // In single threaded mode we attempt to simulate changing the current
145 // thread by maintaining a fake thread id. When we switch from one
146 // thread to another, we construct DebugScopedSetXXXThread objects that
147 // update the thread id. This lets DCHECKS that ensure we're on the
148 // right thread to work correctly in single threaded mode. The problem
149 // here is that the timer tasks are run via the message loop, and when
150 // they run, we've had no chance to construct a DebugScopedSetXXXThread
151 // object. The result is that we report that we're running on the main
152 // thread. In multi-threaded mode, this timer is run on the compositor
153 // thread, so to keep this consistent in single-threaded mode, we'll
154 // construct a DebugScopedSetImplThread object. There is no need to do
155 // this in multi-threaded mode since the real thread id's will be
156 // correct. In fact, setting fake thread id's interferes with the real
157 // thread id's and causes breakage.
158 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
159 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
160 set_impl_thread.reset(
161 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14162 }
163
[email protected]94bf75c2013-06-12 13:20:04164 // TODO(enne): This should probably happen post-animate.
165 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38166 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
167 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04168 }
169
[email protected]fb7425a2013-04-22 16:28:55170 layer_tree_host_impl_->Animate(
171 layer_tree_host_impl_->CurrentFrameTimeTicks(),
172 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18173 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
174 bool start_ready_animations = true;
175 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38176 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27177 }
[email protected]373974232013-01-10 22:20:50178
[email protected]c1bb5af2013-03-13 19:06:27179 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26180 if (active != time_source_->Active())
181 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27182 }
[email protected]94f206c12012-08-25 00:09:14183
[email protected]d9fce6722013-08-30 01:10:01184 bool Active() const { return time_source_->Active(); }
185
[email protected]c1bb5af2013-03-13 19:06:27186 private:
187 LayerTreeHostImplTimeSourceAdapter(
188 LayerTreeHostImpl* layer_tree_host_impl,
189 scoped_refptr<DelayBasedTimeSource> time_source)
190 : layer_tree_host_impl_(layer_tree_host_impl),
191 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26192 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27193 }
[email protected]94f206c12012-08-25 00:09:14194
[email protected]c1bb5af2013-03-13 19:06:27195 LayerTreeHostImpl* layer_tree_host_impl_;
196 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14197
[email protected]c1bb5af2013-03-13 19:06:27198 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14199};
200
[email protected]96baf3e2012-10-22 23:09:55201LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20202 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27203
204LayerTreeHostImpl::FrameData::~FrameData() {}
205
206scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
207 const LayerTreeSettings& settings,
208 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43209 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57210 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02211 SharedBitmapManager* manager,
212 int id) {
[email protected]a7f35682013-10-22 23:05:57213 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02214 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10215}
216
[email protected]372bad5f2013-03-21 16:38:43217LayerTreeHostImpl::LayerTreeHostImpl(
218 const LayerTreeSettings& settings,
219 LayerTreeHostImplClient* client,
220 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57221 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02222 SharedBitmapManager* manager,
223 int id)
[email protected]c1bb5af2013-03-13 19:06:27224 : client_(client),
225 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37226 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27227 did_lock_scrolling_layer_(false),
228 should_bubble_scrolls_(false),
229 wheel_scrolling_(false),
[email protected]f620b0e72013-10-01 21:38:24230 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08231 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47232 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27233 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27234 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07235 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34236 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29237 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07238 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27239 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13240 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36241 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29242 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27243 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02244 debug_rect_history_(DebugRectHistory::Create()),
[email protected]ea468c6c2013-09-10 08:25:11245 texture_mailbox_deleter_(new TextureMailboxDeleter),
[email protected]d7626ffd2013-03-29 00:17:42246 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27247 last_sent_memory_visible_bytes_(0),
248 last_sent_memory_visible_and_nearby_bytes_(0),
249 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55250 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32251 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12252 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32253 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28254 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17255 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43256 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19257 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33258 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57259 need_to_update_visible_tiles_before_draw_(false),
[email protected]ce2e8112013-11-28 07:44:36260#ifndef NDEBUG
261 did_lose_called_(false),
262#endif
[email protected]dbe759a2013-12-02 19:23:02263 shared_bitmap_manager_(manager),
264 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27265 DCHECK(proxy_->IsImplThread());
266 DidVisibilityChange(this, visible_);
267
[email protected]8e0176d2013-03-21 03:14:52268 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27269
[email protected]8e0176d2013-03-21 03:14:52270 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27271 top_controls_manager_ =
272 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52273 settings.top_controls_height,
274 settings.top_controls_show_threshold,
275 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27276 }
277
[email protected]8e0176d2013-03-21 03:14:52278 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27279
280 // LTHI always has an active tree.
281 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51282 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
283 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10284}
285
[email protected]c1bb5af2013-03-13 19:06:27286LayerTreeHostImpl::~LayerTreeHostImpl() {
287 DCHECK(proxy_->IsImplThread());
288 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51289 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
290 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27291
[email protected]200a9c062013-05-20 04:34:37292 if (input_handler_client_) {
293 input_handler_client_->WillShutdown();
294 input_handler_client_ = NULL;
295 }
296
[email protected]75deb742013-06-24 20:19:18297 // The layer trees must be destroyed before the layer tree host. We've
298 // made a contract with our animation controllers that the registrar
299 // will outlive them, and we must make good.
300 recycle_tree_.reset();
301 pending_tree_.reset();
302 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14303}
304
[email protected]097a0cbf2013-12-12 17:04:58305void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
306 // If the begin frame data was handled, then scroll and scale set was applied
307 // by the main thread, so the active tree needs to be updated as if these sent
308 // values were applied and committed.
309 if (did_handle) {
310 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
311 active_tree_->ResetContentsTexturesPurged();
312 }
313}
314
[email protected]c1bb5af2013-03-13 19:06:27315void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29316
[email protected]c1bb5af2013-03-13 19:06:27317void LayerTreeHostImpl::CommitComplete() {
318 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08319
[email protected]8e0176d2013-03-21 03:14:52320 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25321 // Impl-side painting needs an update immediately post-commit to have the
322 // opportunity to create tilings. Other paths can call UpdateDrawProperties
323 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50324 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27325 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04326 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26327 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08328 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38329 NotifyReadyToActivate();
330 else
331 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27332 } else {
333 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01334 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
335 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27336 }
[email protected]3ba4cae2013-01-16 03:58:38337
[email protected]c1bb5af2013-03-13 19:06:27338 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33339
340 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14341}
342
[email protected]6133cc232013-07-30 18:47:07343bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27344 // Note: If you are changing this function or any other function that might
345 // affect the result of CanDraw, make sure to call
346 // client_->OnCanDrawStateChanged in the proper places and update the
347 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14348
[email protected]6133cc232013-07-30 18:47:07349 if (!renderer_) {
350 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
351 TRACE_EVENT_SCOPE_THREAD);
352 return false;
353 }
354
355 // Must have an OutputSurface if |renderer_| is not NULL.
356 DCHECK(output_surface_);
357
358 // TODO(boliu): Make draws without root_layer work and move this below
359 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27360 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42361 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
362 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22363 return false;
[email protected]c1bb5af2013-03-13 19:06:27364 }
[email protected]6133cc232013-07-30 18:47:07365
366 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
367 return true;
368
[email protected]54af03522013-09-05 00:43:28369 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42370 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
371 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27372 return false;
373 }
374 if (active_tree_->ViewportSizeInvalid()) {
375 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42376 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
377 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27378 return false;
379 }
[email protected]c1bb5af2013-03-13 19:06:27380 if (active_tree_->ContentsTexturesPurged()) {
381 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42382 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
383 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27384 return false;
385 }
[email protected]127bdc1a2013-09-11 01:44:48386 if (EvictedUIResourcesExist()) {
387 TRACE_EVENT_INSTANT0(
388 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
389 TRACE_EVENT_SCOPE_THREAD);
390 return false;
391 }
[email protected]c1bb5af2013-03-13 19:06:27392 return true;
[email protected]2f1acc262012-11-16 21:42:22393}
394
[email protected]c1bb5af2013-03-13 19:06:27395void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
396 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37397 if (input_handler_client_)
398 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27399 AnimatePageScale(monotonic_time);
400 AnimateLayers(monotonic_time, wall_clock_time);
401 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37402 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14403}
404
[email protected]c1bb5af2013-03-13 19:06:27405void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26406 if (!tile_manager_)
407 return;
[email protected]c48536a52013-09-14 00:02:08408 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26409 return;
[email protected]54af03522013-09-05 00:43:28410 if (!device_viewport_valid_for_tile_management_)
411 return;
412
[email protected]c48536a52013-09-14 00:02:08413 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19414 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27415
416 size_t memory_required_bytes;
417 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03418 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27419 size_t memory_used_bytes;
420 tile_manager_->GetMemoryStats(&memory_required_bytes,
421 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03422 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27423 &memory_used_bytes);
424 SendManagedMemoryStats(memory_required_bytes,
425 memory_nice_to_have_bytes,
426 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57427
428 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15429}
430
[email protected]c1bb5af2013-03-13 19:06:27431void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
432 bool anchor_point,
433 float page_scale,
[email protected]c1bb5af2013-03-13 19:06:27434 base::TimeDelta duration) {
435 if (!RootScrollLayer())
436 return;
437
438 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47439 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27440 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28441 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27442
[email protected]85d136f782013-04-26 22:04:40443 // Easing constants experimentally determined.
444 scoped_ptr<TimingFunction> timing_function =
445 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
446
[email protected]c1bb5af2013-03-13 19:06:27447 page_scale_animation_ =
448 PageScaleAnimation::Create(scroll_total,
449 active_tree_->total_page_scale_factor(),
450 viewport_size,
451 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40452 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27453
454 if (anchor_point) {
455 gfx::Vector2dF anchor(target_offset);
456 page_scale_animation_->ZoomWithAnchor(anchor,
457 page_scale,
458 duration.InSecondsF());
459 } else {
460 gfx::Vector2dF scaled_target_offset = target_offset;
461 page_scale_animation_->ZoomTo(scaled_target_offset,
462 page_scale,
463 duration.InSecondsF());
464 }
465
[email protected]4e4136d2013-11-29 02:22:44466 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27467 client_->SetNeedsCommitOnImplThread();
468 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15469}
470
[email protected]c1bb5af2013-03-13 19:06:27471void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44472 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15473}
474
[email protected]c1bb5af2013-03-13 19:06:27475bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07476 if (!settings_.touch_hit_testing)
477 return true;
[email protected]c1bb5af2013-03-13 19:06:27478 if (!EnsureRenderSurfaceLayerList())
479 return false;
[email protected]f57bbc02012-11-21 07:02:15480
[email protected]c1bb5af2013-03-13 19:06:27481 gfx::PointF device_viewport_point =
482 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15483
[email protected]c974d5d2013-10-24 01:02:48484 LayerImpl* layer_impl =
485 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
486 device_viewport_point,
487 active_tree_->RenderSurfaceLayerList());
488 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27489}
490
[email protected]6be422b2013-12-08 06:47:31491scoped_ptr<SwapPromiseMonitor>
492LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
493 ui::LatencyInfo* latency) {
494 return scoped_ptr<SwapPromiseMonitor>(
495 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08496}
497
[email protected]c1bb5af2013-03-13 19:06:27498void LayerTreeHostImpl::TrackDamageForAllSurfaces(
499 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28500 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27501 // For now, we use damage tracking to compute a global scissor. To do this, we
502 // must compute all damage tracking before drawing anything, so that we know
503 // the root damage rect. The root damage rect is then used to scissor each
504 // surface.
505
506 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06507 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27508 --surface_index) {
509 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
510 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
511 DCHECK(render_surface);
512 render_surface->damage_tracker()->UpdateDamageTrackingState(
513 render_surface->layer_list(),
514 render_surface_layer->id(),
515 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
516 render_surface->content_rect(),
517 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48518 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27519 }
520}
521
[email protected]34806722013-08-09 23:51:21522scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
523 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
524 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
525 value->SetBoolean("has_no_damage", has_no_damage);
526
527 // Quad data can be quite large, so only dump render passes if we select
528 // cc.debug.quads.
529 bool quads_enabled;
530 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
531 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
532 if (quads_enabled) {
533 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
534 for (size_t i = 0; i < render_passes.size(); ++i)
535 render_pass_list->Append(render_passes[i]->AsValue().release());
536 value->Set("render_passes", render_pass_list.release());
537 }
538 return value.PassAs<base::Value>();
539}
540
[email protected]c1bb5af2013-03-13 19:06:27541void LayerTreeHostImpl::FrameData::AppendRenderPass(
542 scoped_ptr<RenderPass> render_pass) {
543 render_passes_by_id[render_pass->id] = render_pass.get();
544 render_passes.push_back(render_pass.Pass());
545}
546
[email protected]ffbb2212013-06-02 23:47:59547static DrawMode GetDrawMode(OutputSurface* output_surface) {
548 if (output_surface->ForcedDrawToSoftwareDevice()) {
549 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09550 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59551 return DRAW_MODE_HARDWARE;
552 } else {
[email protected]a7f35682013-10-22 23:05:57553 DCHECK_EQ(!output_surface->software_device(),
554 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59555 return DRAW_MODE_SOFTWARE;
556 }
557}
558
[email protected]c1bb5af2013-03-13 19:06:27559static void AppendQuadsForLayer(RenderPass* target_render_pass,
560 LayerImpl* layer,
561 const OcclusionTrackerImpl& occlusion_tracker,
562 AppendQuadsData* append_quads_data) {
563 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49564 QuadCuller quad_culler(&target_render_pass->quad_list,
565 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27566 layer,
567 occlusion_tracker,
568 layer->ShowDebugBorders(),
569 for_surface);
570 layer->AppendQuads(&quad_culler, append_quads_data);
571}
572
573static void AppendQuadsForRenderSurfaceLayer(
574 RenderPass* target_render_pass,
575 LayerImpl* layer,
576 const RenderPass* contributing_render_pass,
577 const OcclusionTrackerImpl& occlusion_tracker,
578 AppendQuadsData* append_quads_data) {
579 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49580 QuadCuller quad_culler(&target_render_pass->quad_list,
581 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27582 layer,
583 occlusion_tracker,
584 layer->ShowDebugBorders(),
585 for_surface);
586
587 bool is_replica = false;
588 layer->render_surface()->AppendQuads(&quad_culler,
589 append_quads_data,
590 is_replica,
591 contributing_render_pass->id);
592
593 // Add replica after the surface so that it appears below the surface.
594 if (layer->has_replica()) {
595 is_replica = true;
596 layer->render_surface()->AppendQuads(&quad_culler,
597 append_quads_data,
598 is_replica,
599 contributing_render_pass->id);
600 }
601}
602
603static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08604 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20605 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06606 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27607 RenderPass* target_render_pass,
608 LayerImpl* root_layer,
609 SkColor screen_background_color,
610 const OcclusionTrackerImpl& occlusion_tracker) {
611 if (!root_layer || !SkColorGetA(screen_background_color))
612 return;
613
614 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
615 if (fill_region.IsEmpty())
616 return;
617
[email protected]3744e27b2013-11-06 21:44:08618 // Divide the fill region into the part to be filled with the overhang
619 // resource and the part to be filled with the background color.
620 Region screen_background_color_region = fill_region;
621 Region overhang_region;
622 if (overhang_resource_id) {
623 overhang_region = fill_region;
624 overhang_region.Subtract(root_scroll_layer_rect);
625 screen_background_color_region.Intersect(root_scroll_layer_rect);
626 }
627
[email protected]c1bb5af2013-03-13 19:06:27628 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49629 QuadCuller quad_culler(&target_render_pass->quad_list,
630 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27631 root_layer,
632 occlusion_tracker,
633 root_layer->ShowDebugBorders(),
634 for_surface);
635
636 // Manually create the quad state for the gutter quads, as the root layer
637 // doesn't have any bounds and so can't generate this itself.
638 // TODO(danakj): Make the gutter quads generated by the solid color layer
639 // (make it smarter about generating quads to fill unoccluded areas).
640
641 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
642 float opacity = 1.f;
643 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49644 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27645 shared_quad_state->SetAll(root_layer->draw_transform(),
646 root_target_rect.size(),
647 root_target_rect,
648 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01649 false,
[email protected]7bbeaf4e2013-11-26 10:27:22650 opacity,
651 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15652
[email protected]c1bb5af2013-03-13 19:06:27653 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17654
[email protected]c1bb5af2013-03-13 19:06:27655 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
656 bool did_invert = root_layer->screen_space_transform().GetInverse(
657 &transform_to_layer_space);
658 DCHECK(did_invert);
[email protected]3744e27b2013-11-06 21:44:08659 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27660 fill_rects.has_rect();
661 fill_rects.next()) {
662 // The root layer transform is composed of translations and scales only,
663 // no perspective, so mapping is sufficient (as opposed to projecting).
664 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21665 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]3744e27b2013-11-06 21:44:08666 // Skip the quad culler and just append the quads directly to avoid
667 // occlusion checks.
668 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
669 quad->SetNew(
670 shared_quad_state, layer_rect, screen_background_color, false);
671 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
672 }
673 for (Region::Iterator fill_rects(overhang_region);
674 fill_rects.has_rect();
675 fill_rects.next()) {
676 DCHECK(overhang_resource_id);
677 gfx::Rect layer_rect =
678 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
679 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
680 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
681 tex_quad->SetNew(
682 shared_quad_state,
683 layer_rect,
684 layer_rect,
685 overhang_resource_id,
686 false,
687 gfx::PointF(layer_rect.x() / overhang_resource_scaled_size.width(),
688 layer_rect.y() / overhang_resource_scaled_size.height()),
689 gfx::PointF(layer_rect.right() /
690 overhang_resource_scaled_size.width(),
691 layer_rect.bottom() /
692 overhang_resource_scaled_size.height()),
693 screen_background_color,
694 vertex_opacity,
695 false);
696 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27697 }
[email protected]467b3612012-08-28 07:41:16698}
699
[email protected]c1bb5af2013-03-13 19:06:27700bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
701 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14702
[email protected]c1bb5af2013-03-13 19:06:27703 if (!CanDraw() || !active_tree_->root_layer())
704 return false;
[email protected]2d692992012-12-19 01:19:32705
[email protected]c1bb5af2013-03-13 19:06:27706 TrackDamageForAllSurfaces(active_tree_->root_layer(),
707 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14708
[email protected]e0341352013-04-06 05:01:20709 // If the root render surface has no visible damage, then don't generate a
710 // frame at all.
711 RenderSurfaceImpl* root_surface =
712 active_tree_->root_layer()->render_surface();
713 bool root_surface_has_no_visible_damage =
714 !root_surface->damage_tracker()->current_damage_rect().Intersects(
715 root_surface->content_rect());
716 bool root_surface_has_contributing_layers =
717 !root_surface->layer_list().empty();
718 if (root_surface_has_contributing_layers &&
719 root_surface_has_no_visible_damage) {
720 TRACE_EVENT0("cc",
721 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
722 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45723 // A copy request should cause damage, so we should not have any copy
724 // requests in this case.
725 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07726 DCHECK(!output_surface_->capabilities()
727 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20728 return true;
729 }
730
[email protected]c1bb5af2013-03-13 19:06:27731 TRACE_EVENT1("cc",
732 "LayerTreeHostImpl::CalculateRenderPasses",
733 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06734 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14735
[email protected]c1bb5af2013-03-13 19:06:27736 // Create the render passes in dependency order.
737 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06738 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27739 --surface_index) {
740 LayerImpl* render_surface_layer =
741 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45742 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
743
744 bool should_draw_into_render_pass =
745 render_surface_layer->parent() == NULL ||
746 render_surface->contributes_to_drawn_surface() ||
747 render_surface_layer->HasCopyRequest();
748 if (should_draw_into_render_pass)
749 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27750 }
[email protected]94f206c12012-08-25 00:09:14751
[email protected]c1bb5af2013-03-13 19:06:27752 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52753 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27754 base::debug::TraceLog::GetInstance() &&
755 base::debug::TraceLog::GetInstance()->IsEnabled();
756 OcclusionTrackerImpl occlusion_tracker(
757 active_tree_->root_layer()->render_surface()->content_rect(),
758 record_metrics_for_frame);
759 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52760 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14761
[email protected]846f455b2013-03-18 19:07:41762 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27763 occlusion_tracker.set_occluding_screen_space_rects_container(
764 &frame->occluding_screen_space_rects);
765 }
[email protected]846f455b2013-03-18 19:07:41766 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27767 occlusion_tracker.set_non_occluding_screen_space_rects_container(
768 &frame->non_occluding_screen_space_rects);
769 }
[email protected]94f206c12012-08-25 00:09:14770
[email protected]c1bb5af2013-03-13 19:06:27771 // Add quads to the Render passes in FrontToBack order to allow for testing
772 // occlusion and performing culling during the tree walk.
773 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28774 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27775 RenderSurfaceImpl,
776 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14777
[email protected]c1bb5af2013-03-13 19:06:27778 // Typically when we are missing a texture and use a checkerboard quad, we
779 // still draw the frame. However when the layer being checkerboarded is moving
780 // due to an impl-animation, we drop the frame to avoid flashing due to the
781 // texture suddenly appearing in the future.
782 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25783 // When we have a copy request for a layer, we need to draw no matter
784 // what, as the layer may disappear after this frame.
785 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14786
[email protected]372bad5f2013-03-21 16:38:43787 int layers_drawn = 0;
788
[email protected]ffbb2212013-06-02 23:47:59789 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
790
[email protected]c1bb5af2013-03-13 19:06:27791 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09792 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27793 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09794 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27795 it != end;
796 ++it) {
797 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09798 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27799 RenderPass* target_render_pass =
800 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14801
[email protected]e47b0a0a2013-11-18 23:26:22802 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14803
[email protected]30fe19ff2013-07-04 00:54:45804 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30805
[email protected]18a70192013-04-26 16:18:25806 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36807 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25808 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11809 it->TakeCopyRequestsAndTransformToTarget(
810 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25811 }
[email protected]30fe19ff2013-07-04 00:54:45812 } else if (it.represents_contributing_render_surface() &&
813 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27814 RenderPass::Id contributing_render_pass_id =
815 it->render_surface()->RenderPassId();
816 RenderPass* contributing_render_pass =
817 frame->render_passes_by_id[contributing_render_pass_id];
818 AppendQuadsForRenderSurfaceLayer(target_render_pass,
819 *it,
820 contributing_render_pass,
821 occlusion_tracker,
822 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45823 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09824 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27825 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15826 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27827 it->render_target(),
828 it->visible_content_rect(),
829 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45830 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15831 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27832 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59833
[email protected]c1bb5af2013-03-13 19:06:27834 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57835
[email protected]c1bb5af2013-03-13 19:06:27836 if (it->HasContributingDelegatedRenderPasses()) {
837 RenderPass::Id contributing_render_pass_id =
838 it->FirstContributingRenderPassId();
839 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
840 frame->render_passes_by_id.end()) {
841 RenderPass* render_pass =
842 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14843
[email protected]c1bb5af2013-03-13 19:06:27844 AppendQuadsData append_quads_data(render_pass->id);
845 AppendQuadsForLayer(render_pass,
846 *it,
847 occlusion_tracker,
848 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57849
[email protected]c1bb5af2013-03-13 19:06:27850 contributing_render_pass_id =
851 it->NextContributingRenderPassId(contributing_render_pass_id);
852 }
[email protected]94f206c12012-08-25 00:09:14853 }
854
[email protected]c1bb5af2013-03-13 19:06:27855 AppendQuadsForLayer(target_render_pass,
856 *it,
857 occlusion_tracker,
858 &append_quads_data);
859 }
[email protected]89228202012-08-29 03:20:30860
[email protected]372bad5f2013-03-21 16:38:43861 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14862 }
863
[email protected]e1e768f2013-03-26 08:48:09864 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27865 bool layer_has_animating_transform =
866 it->screen_space_transform_is_animating() ||
867 it->draw_transform_is_animating();
868 if (layer_has_animating_transform)
869 draw_frame = false;
870 }
871
[email protected]e1e768f2013-03-26 08:48:09872 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27873 frame->contains_incomplete_tile = true;
874
875 occlusion_tracker.LeaveLayer(it);
876 }
877
[email protected]6133cc232013-07-30 18:47:07878 if (have_copy_request ||
879 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25880 draw_frame = true;
881
[email protected]1d993172012-10-18 18:15:04882#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27883 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
884 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
885 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
886 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
887 != frame->render_passes_by_id.end());
888 }
[email protected]94f206c12012-08-25 00:09:14889#endif
[email protected]c1bb5af2013-03-13 19:06:27890 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14891
[email protected]c1bb5af2013-03-13 19:06:27892 if (!active_tree_->has_transparent_background()) {
893 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08894 AppendQuadsToFillScreen(
895 ResourceIdForUIResource(overhang_ui_resource_id_),
896 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
897 active_tree_->RootScrollLayerDeviceViewportBounds(),
898 frame->render_passes.back(),
899 active_tree_->root_layer(),
900 active_tree_->background_color(),
901 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27902 }
[email protected]94f206c12012-08-25 00:09:14903
[email protected]c1bb5af2013-03-13 19:06:27904 if (draw_frame)
905 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25906 else
907 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14908
[email protected]c1bb5af2013-03-13 19:06:27909 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48910 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14911
[email protected]30fe19ff2013-07-04 00:54:45912 // Any copy requests left in the tree are not going to get serviced, and
913 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11914 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
915 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
916 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
917 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45918 }
[email protected]19334ac2013-12-12 23:59:11919 for (size_t i = 0; i < requests_to_abort.size(); ++i)
920 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45921
[email protected]e0341352013-04-06 05:01:20922 // If we're making a frame to draw, it better have at least one render pass.
923 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48924
925 // Should only have one render pass in resourceless software mode.
926 if (output_surface_->ForcedDrawToSoftwareDevice())
927 DCHECK_EQ(1u, frame->render_passes.size());
928
[email protected]c1bb5af2013-03-13 19:06:27929 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14930}
931
[email protected]200a9c062013-05-20 04:34:37932void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
933 if (input_handler_client_)
934 input_handler_client_->MainThreadHasStoppedFlinging();
935}
936
[email protected]3d9f7432013-04-06 00:35:18937void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
938 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15939 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01940 if (should_background_tick)
941 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15942
[email protected]3d9f7432013-04-06 00:35:18943 bool enabled = should_background_tick &&
944 !animation_registrar_->active_animation_controllers().empty();
945
[email protected]c1bb5af2013-03-13 19:06:27946 // Lazily create the time_source adapter so that we can vary the interval for
947 // testing.
948 if (!time_source_client_adapter_) {
949 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
950 this,
[email protected]810d40b72013-06-20 18:26:15951 DelayBasedTimeSource::Create(
952 LowFrequencyAnimationInterval(),
953 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
954 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27955 }
[email protected]94f206c12012-08-25 00:09:14956
[email protected]c1bb5af2013-03-13 19:06:27957 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14958}
959
[email protected]b8384e22013-12-03 02:20:48960void LayerTreeHostImpl::DidAnimateScrollOffset() {
961 client_->SetNeedsCommitOnImplThread();
962 client_->RenewTreePriority();
963}
964
[email protected]0023fc72014-01-10 20:05:06965void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02966 viewport_damage_rect_.Union(damage_rect);
967}
968
[email protected]c1bb5af2013-03-13 19:06:27969static inline RenderPass* FindRenderPassById(
970 RenderPass::Id render_pass_id,
971 const LayerTreeHostImpl::FrameData& frame) {
972 RenderPassIdHashMap::const_iterator it =
973 frame.render_passes_by_id.find(render_pass_id);
974 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14975}
976
[email protected]c1bb5af2013-03-13 19:06:27977static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
978 LayerTreeHostImpl::FrameData* frame) {
979 RenderPass* remove_render_pass =
980 FindRenderPassById(remove_render_pass_id, *frame);
981 // The pass was already removed by another quad - probably the original, and
982 // we are the replica.
983 if (!remove_render_pass)
984 return;
985 RenderPassList& render_passes = frame->render_passes;
986 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
987 render_passes.end(),
988 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14989
[email protected]c1bb5af2013-03-13 19:06:27990 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14991
[email protected]c1bb5af2013-03-13 19:06:27992 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
993 frame->render_passes.erase(to_remove);
994 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14995
[email protected]c1bb5af2013-03-13 19:06:27996 // Now follow up for all RenderPass quads and remove their RenderPasses
997 // recursively.
998 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29999 QuadList::ConstBackToFrontIterator quad_list_iterator =
1000 quad_list.BackToFrontBegin();
1001 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271002 ++quad_list_iterator) {
1003 DrawQuad* current_quad = (*quad_list_iterator);
1004 if (current_quad->material != DrawQuad::RENDER_PASS)
1005 continue;
[email protected]94f206c12012-08-25 00:09:141006
[email protected]c1bb5af2013-03-13 19:06:271007 RenderPass::Id next_remove_render_pass_id =
1008 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1009 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1010 }
[email protected]94f206c12012-08-25 00:09:141011}
1012
[email protected]c1bb5af2013-03-13 19:06:271013bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1014 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1015 const RenderPass* render_pass =
1016 FindRenderPassById(quad.render_pass_id, frame);
1017 if (!render_pass)
1018 return false;
[email protected]94f206c12012-08-25 00:09:141019
[email protected]c1bb5af2013-03-13 19:06:271020 // If any quad or RenderPass draws into this RenderPass, then keep it.
1021 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291022 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1023 quad_list.BackToFrontBegin();
1024 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271025 ++quad_list_iterator) {
1026 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141027
[email protected]c1bb5af2013-03-13 19:06:271028 if (current_quad->material != DrawQuad::RENDER_PASS)
1029 return false;
[email protected]94f206c12012-08-25 00:09:141030
[email protected]c1bb5af2013-03-13 19:06:271031 const RenderPass* contributing_pass = FindRenderPassById(
1032 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1033 if (contributing_pass)
1034 return false;
1035 }
1036 return true;
[email protected]94f206c12012-08-25 00:09:141037}
1038
1039// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271040template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271041 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1042 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141043
1044// static
[email protected]c1bb5af2013-03-13 19:06:271045template <typename RenderPassCuller>
1046void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1047 FrameData* frame) {
1048 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1049 it != culler.RenderPassListEnd(frame->render_passes);
1050 it = culler.RenderPassListNext(it)) {
1051 const RenderPass* current_pass = frame->render_passes[it];
1052 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291053 QuadList::ConstBackToFrontIterator quad_list_iterator =
1054 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141055
[email protected]ed511b8d2013-03-25 03:29:291056 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271057 ++quad_list_iterator) {
1058 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141059
[email protected]c1bb5af2013-03-13 19:06:271060 if (current_quad->material != DrawQuad::RENDER_PASS)
1061 continue;
[email protected]94f206c12012-08-25 00:09:141062
[email protected]c1bb5af2013-03-13 19:06:271063 const RenderPassDrawQuad* render_pass_quad =
1064 RenderPassDrawQuad::MaterialCast(current_quad);
1065 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1066 continue;
[email protected]94f206c12012-08-25 00:09:141067
[email protected]c1bb5af2013-03-13 19:06:271068 // We are changing the vector in the middle of iteration. Because we
1069 // delete render passes that draw into the current pass, we are
1070 // guaranteed that any data from the iterator to the end will not
1071 // change. So, capture the iterator position from the end of the
1072 // list, and restore it after the change.
1073 size_t position_from_end = frame->render_passes.size() - it;
1074 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1075 it = frame->render_passes.size() - position_from_end;
1076 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141077 }
[email protected]c1bb5af2013-03-13 19:06:271078 }
[email protected]94f206c12012-08-25 00:09:141079}
1080
[email protected]e0341352013-04-06 05:01:201081bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
[email protected]0023fc72014-01-10 20:05:061082 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471083 TRACE_EVENT1("cc",
1084 "LayerTreeHostImpl::PrepareToDraw",
1085 "SourceFrameNumber",
1086 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141087
[email protected]425e5332013-09-21 04:10:031088 if (need_to_update_visible_tiles_before_draw_ &&
1089 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1090 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191091 }
[email protected]425e5332013-09-21 04:10:031092 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191093
[email protected]7d19dc342013-05-02 22:02:041094 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271095
[email protected]c1bb5af2013-03-13 19:06:271096 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1097 frame->render_passes.clear();
1098 frame->render_passes_by_id.clear();
1099 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201100 frame->contains_incomplete_tile = false;
1101 frame->has_no_damage = false;
1102
[email protected]0023fc72014-01-10 20:05:061103 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201104 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021105 device_viewport_damage_rect.Union(viewport_damage_rect_);
1106 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201107
1108 active_tree_->root_layer()->render_surface()->damage_tracker()->
1109 AddDamageNextUpdate(device_viewport_damage_rect);
1110 }
[email protected]94f206c12012-08-25 00:09:141111
[email protected]6133cc232013-07-30 18:47:071112 if (!CalculateRenderPasses(frame)) {
1113 DCHECK(!output_surface_->capabilities()
1114 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:271115 return false;
[email protected]6133cc232013-07-30 18:47:071116 }
[email protected]94f206c12012-08-25 00:09:141117
[email protected]c1bb5af2013-03-13 19:06:271118 // If we return true, then we expect DrawLayers() to be called before this
1119 // function is called again.
1120 return true;
[email protected]94f206c12012-08-25 00:09:141121}
1122
[email protected]0309ba5e02013-06-26 04:11:081123void LayerTreeHostImpl::EvictTexturesForTesting() {
1124 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1125}
1126
[email protected]4f48f6e2013-08-27 06:33:381127void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1128 NOTREACHED();
1129}
1130
[email protected]425e5332013-09-21 04:10:031131void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1132 DidInitializeVisibleTile();
1133}
1134
[email protected]c1bb5af2013-03-13 19:06:271135void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1136 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121137
[email protected]c1bb5af2013-03-13 19:06:271138 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351139 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351140 ManagedMemoryPolicy::PriorityCutoffToValue(
1141 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351142 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271143 if (evicted_resources) {
1144 active_tree_->SetContentsTexturesPurged();
1145 if (pending_tree_)
1146 pending_tree_->SetContentsTexturesPurged();
1147 client_->SetNeedsCommitOnImplThread();
1148 client_->OnCanDrawStateChanged(CanDraw());
1149 client_->RenewTreePriority();
1150 }
1151 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431152
[email protected]8be1a9bf2013-05-01 03:45:191153 UpdateTileManagerMemoryPolicy(policy);
1154}
1155
1156void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1157 const ManagedMemoryPolicy& policy) {
1158 if (!tile_manager_)
1159 return;
1160
[email protected]8be1a9bf2013-05-01 03:45:191161 // TODO(reveman): We should avoid keeping around unused resources if
1162 // possible. crbug.com/224475
[email protected]1bcced22013-09-24 13:51:191163 global_tile_state_.memory_limit_in_bytes =
1164 visible_ ?
[email protected]0b8ef2842013-11-01 09:53:351165 policy.bytes_limit_when_visible : 0;
[email protected]1bcced22013-09-24 13:51:191166 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
1167 (static_cast<int64>(global_tile_state_.memory_limit_in_bytes) *
[email protected]8be1a9bf2013-05-01 03:45:191168 settings_.max_unused_resource_memory_percentage) / 100);
[email protected]1bcced22013-09-24 13:51:191169 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191170 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1171 visible_ ?
1172 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351173 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191174 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081175
[email protected]c48536a52013-09-14 00:02:081176 DidModifyTilePriorities();
1177}
1178
1179void LayerTreeHostImpl::DidModifyTilePriorities() {
1180 DCHECK(settings_.impl_side_painting);
1181 // Mark priorities as dirty and schedule a ManageTiles().
1182 tile_priorities_dirty_ = true;
1183 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141184}
1185
[email protected]86126792013-03-16 20:07:541186void LayerTreeHostImpl::DidInitializeVisibleTile() {
1187 // TODO(reveman): Determine tiles that changed and only damage
1188 // what's necessary.
1189 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031190 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541191 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471192}
1193
[email protected]94bf75c2013-06-12 13:20:041194void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381195 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041196}
1197
[email protected]3f2ff112013-08-03 02:41:071198void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1199 SetManagedMemoryPolicy(policy, zero_budget_);
1200}
1201
[email protected]5f07afc2013-07-19 05:17:181202void LayerTreeHostImpl::SetTreeActivationCallback(
1203 const base::Closure& callback) {
1204 DCHECK(proxy_->IsImplThread());
1205 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261206 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181207}
1208
[email protected]c1bb5af2013-03-13 19:06:271209void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071210 const ManagedMemoryPolicy& policy, bool zero_budget) {
1211 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271212 return;
[email protected]61de5812012-11-08 07:03:441213
[email protected]3f2ff112013-08-03 02:41:071214 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1215
1216 cached_managed_memory_policy_ = policy;
1217 zero_budget_ = zero_budget;
1218 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1219
1220 if (old_policy == actual_policy)
1221 return;
1222
1223 if (!proxy_->HasImplThread()) {
1224 // In single-thread mode, this can be called on the main thread by
1225 // GLRenderer::OnMemoryAllocationChanged.
1226 DebugScopedSetImplThread impl_thread(proxy_);
1227 EnforceManagedMemoryPolicy(actual_policy);
1228 } else {
1229 DCHECK(proxy_->IsImplThread());
1230 EnforceManagedMemoryPolicy(actual_policy);
1231 }
[email protected]50644642013-06-20 13:58:551232
[email protected]d7626ffd2013-03-29 00:17:421233 // If there is already enough memory to draw everything imaginable and the
1234 // new memory limit does not change this, then do not re-commit. Don't bother
1235 // skipping commits if this is not visible (commits don't happen when not
1236 // visible, there will almost always be a commit when this becomes visible).
1237 bool needs_commit = true;
1238 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071239 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1240 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1241 actual_policy.priority_cutoff_when_visible ==
1242 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421243 needs_commit = false;
1244 }
1245
[email protected]d7626ffd2013-03-29 00:17:421246 if (needs_commit)
1247 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141248}
1249
[email protected]f224cc92013-06-06 23:23:321250void LayerTreeHostImpl::SetExternalDrawConstraints(
1251 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061252 const gfx::Rect& viewport,
1253 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281254 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321255 external_transform_ = transform;
1256 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281257 external_clip_ = clip;
1258 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321259}
1260
[email protected]0023fc72014-01-10 20:05:061261void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311262 if (damage_rect.IsEmpty())
1263 return;
1264 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031265 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1266}
1267
[email protected]daea3d42013-10-23 17:04:501268void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1269 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451270}
1271
[email protected]4d7e46a2013-11-08 05:33:401272void LayerTreeHostImpl::DidSwapBuffers() {
1273 client_->DidSwapBuffersOnImplThread();
1274}
1275
[email protected]a7335e0b2013-09-18 09:34:511276void LayerTreeHostImpl::OnSwapBuffersComplete() {
1277 client_->OnSwapBuffersCompleteOnImplThread();
1278}
1279
1280void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271281 // TODO(piman): We may need to do some validation on this ack before
1282 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511283 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291284 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161285}
1286
[email protected]c1bb5af2013-03-13 19:06:271287void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1288 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291289}
1290
[email protected]c1bb5af2013-03-13 19:06:271291CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1292 CompositorFrameMetadata metadata;
1293 metadata.device_scale_factor = device_scale_factor_;
1294 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1295 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1296 metadata.root_layer_size = active_tree_->ScrollableSize();
1297 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1298 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1299 if (top_controls_manager_) {
1300 metadata.location_bar_offset =
1301 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1302 metadata.location_bar_content_translation =
1303 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011304 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271305 }
[email protected]bf189f62012-12-18 03:42:111306
[email protected]c1bb5af2013-03-13 19:06:271307 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111308 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271309
[email protected]ffb2720f2013-03-15 19:18:371310 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271311
1312 return metadata;
[email protected]bf189f62012-12-18 03:42:111313}
1314
[email protected]6f50b8fa2013-07-01 19:51:491315static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1316 layer->DidBeginTracing();
1317}
[email protected]37349bc2013-06-04 01:31:521318
[email protected]f0c2a242013-03-15 19:34:521319void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1320 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351321 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271322 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201323
[email protected]3519b872013-07-30 07:17:501324 if (frame->has_no_damage) {
1325 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071326 DCHECK(!output_surface_->capabilities()
1327 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201328 return;
[email protected]3519b872013-07-30 07:17:501329 }
[email protected]e0341352013-04-06 05:01:201330
[email protected]c1bb5af2013-03-13 19:06:271331 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141332
[email protected]213fcfc2013-08-24 14:59:271333 fps_counter_->SaveTimeStamp(frame_begin_time,
1334 !output_surface_->context_provider());
[email protected]94f206c12012-08-25 00:09:141335
[email protected]c1e6cc062013-08-24 03:35:351336 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091337 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351338 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431339
[email protected]c1bb5af2013-03-13 19:06:271340 if (tile_manager_) {
1341 memory_history_->SaveEntry(
1342 tile_manager_->memory_stats_from_last_assign());
1343 }
[email protected]1191d9d2013-02-02 06:00:331344
[email protected]846f455b2013-03-18 19:07:411345 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021346 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271347 active_tree_->root_layer(),
1348 *frame->render_surface_layer_list,
1349 frame->occluding_screen_space_rects,
1350 frame->non_occluding_screen_space_rects,
1351 debug_state_);
1352 }
[email protected]94f206c12012-08-25 00:09:141353
[email protected]a848c102013-03-26 08:59:091354 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1355 const RenderingStats& stats =
1356 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181357 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091358 }
1359
[email protected]37349bc2013-06-04 01:31:521360 bool is_new_trace;
1361 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1362 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491363 if (pending_tree_) {
1364 LayerTreeHostCommon::CallFunctionForSubtree(
1365 pending_tree_->root_layer(),
1366 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1367 }
1368 LayerTreeHostCommon::CallFunctionForSubtree(
1369 active_tree_->root_layer(),
1370 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521371 }
1372
[email protected]9197dbcb2013-05-15 20:28:511373 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211374 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1375 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1376 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081377
[email protected]c1bb5af2013-03-13 19:06:271378 // Because the contents of the HUD depend on everything else in the frame, the
1379 // contents of its texture are updated as the last thing before the frame is
1380 // drawn.
[email protected]671f6672013-10-01 07:31:071381 if (active_tree_->hud_layer()) {
1382 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531383 active_tree_->hud_layer()->UpdateHudTexture(
1384 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071385 }
[email protected]94f206c12012-08-25 00:09:141386
[email protected]b09c1942013-05-10 00:06:371387 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321388 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261389 bool disable_picture_quad_image_filtering =
1390 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321391
[email protected]b09c1942013-05-10 00:06:371392 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321393 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301394 temp_software_renderer->DrawFrame(&frame->render_passes,
1395 NULL,
1396 device_scale_factor_,
1397 DeviceViewport(),
1398 DeviceClip(),
1399 allow_partial_swap,
1400 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371401 } else {
[email protected]573e8e32013-09-09 17:24:321402 // We don't track damage on the HUD layer (it interacts with damage tracking
1403 // visualizations), so disable partial swaps to make the HUD layer display
1404 // properly.
1405 bool allow_partial_swap = !debug_state_.ShowHudRects();
1406
[email protected]b5174d712013-08-28 08:10:431407 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321408 offscreen_context_provider_.get(),
1409 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301410 DeviceViewport(),
1411 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261412 allow_partial_swap,
1413 false);
[email protected]b09c1942013-05-10 00:06:371414 }
[email protected]c1bb5af2013-03-13 19:06:271415 // The render passes should be consumed by the renderer.
1416 DCHECK(frame->render_passes.empty());
1417 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141418
[email protected]c1bb5af2013-03-13 19:06:271419 // The next frame should start by assuming nothing has changed, and changes
1420 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321421 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1422 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541423 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271424 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1425 DidDrawDamagedArea();
1426 }
1427 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351428
[email protected]adbe30f2013-10-11 21:12:331429 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1430 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181431 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141432}
1433
[email protected]c1bb5af2013-03-13 19:06:271434void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1435 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1436 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521437
[email protected]c1bb5af2013-03-13 19:06:271438 // Once all layers have been drawn, pending texture uploads should no
1439 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151440 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141441}
1442
[email protected]c1bb5af2013-03-13 19:06:271443void LayerTreeHostImpl::FinishAllRendering() {
1444 if (renderer_)
1445 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141446}
1447
[email protected]c1bb5af2013-03-13 19:06:271448bool LayerTreeHostImpl::IsContextLost() {
1449 DCHECK(proxy_->IsImplThread());
1450 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141451}
1452
[email protected]7a8bcd262014-01-15 12:54:581453const RendererCapabilitiesImpl&
1454LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271455 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141456}
1457
[email protected]e0341352013-04-06 05:01:201458bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]b69c1db2013-11-27 00:05:191459 if (frame.has_no_damage) {
1460 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201461 return false;
[email protected]b69c1db2013-11-27 00:05:191462 }
[email protected]4a28a612013-11-27 02:06:331463 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551464 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331465 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161466 return true;
[email protected]94f206c12012-08-25 00:09:141467}
1468
[email protected]daea3d42013-10-23 17:04:501469void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451470 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501471 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451472}
1473
[email protected]54af03522013-09-05 00:43:281474gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341475 // Use the root container layer bounds if it clips to them, otherwise, the
1476 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441477 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341478 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441479 DCHECK(!top_controls_manager_);
1480 DCHECK_EQ(0, overdraw_bottom_height_);
1481 return container_layer->bounds();
1482 }
1483
[email protected]ffb2720f2013-03-15 19:18:371484 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281485 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371486
[email protected]ed511b8d2013-03-25 03:29:291487 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371488 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401489 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291490 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371491}
1492
[email protected]c1bb5af2013-03-13 19:06:271493void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191494 if (resource_provider_)
1495 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011496 // TODO(jamesr): The renderer_ check is needed to make some of the
1497 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1498 // important) in production. We should adjust the test to not need this.
1499 if (renderer_)
1500 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]ce2e8112013-11-28 07:44:361501#ifndef NDEBUG
1502 did_lose_called_ = true;
1503#endif
[email protected]94f206c12012-08-25 00:09:141504}
1505
[email protected]c1bb5af2013-03-13 19:06:271506void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061507 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271508 DCHECK(renderer_);
1509 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141510}
1511
[email protected]59adb112013-04-09 04:48:441512bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341513 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011514}
1515
[email protected]c1bb5af2013-03-13 19:06:271516LayerImpl* LayerTreeHostImpl::RootLayer() const {
1517 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081518}
1519
[email protected]c1bb5af2013-03-13 19:06:271520LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1521 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081522}
1523
[email protected]c1bb5af2013-03-13 19:06:271524LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1525 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081526}
1527
[email protected]251699b2013-10-09 00:21:261528bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1529 return CurrentlyScrollingLayer() ||
1530 (RootScrollLayer() && RootScrollLayer()->IsExternalFlingActive());
1531}
1532
[email protected]94f206c12012-08-25 00:09:141533// Content layers can be either directly scrollable or contained in an outer
1534// scrolling layer which applies the scroll transform. Given a content layer,
1535// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271536static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1537 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021538 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271539
1540 if (layer_impl->scrollable())
1541 return layer_impl;
1542
1543 if (layer_impl->DrawsContent() &&
1544 layer_impl->parent() &&
1545 layer_impl->parent()->scrollable())
1546 return layer_impl->parent();
1547
[email protected]bf1cfd9a2013-09-26 05:43:021548 return NULL;
[email protected]94f206c12012-08-25 00:09:141549}
1550
[email protected]c1bb5af2013-03-13 19:06:271551void LayerTreeHostImpl::CreatePendingTree() {
1552 CHECK(!pending_tree_);
1553 if (recycle_tree_)
1554 recycle_tree_.swap(pending_tree_);
1555 else
1556 pending_tree_ = LayerTreeImpl::create(this);
1557 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111558 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271559}
1560
[email protected]8bb6cfa2013-07-23 00:11:191561void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191562 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1563 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191564 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481565}
1566
[email protected]c1bb5af2013-03-13 19:06:271567void LayerTreeHostImpl::ActivatePendingTree() {
1568 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111569 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351570
[email protected]4f48f6e2013-08-27 06:33:381571 need_to_update_visible_tiles_before_draw_ = true;
1572
[email protected]1960a712013-04-30 17:06:471573 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271574 active_tree_->PushPersistedState(pending_tree_.get());
1575 if (pending_tree_->needs_full_tree_sync()) {
1576 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331577 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271578 active_tree_->DetachLayerTree(),
1579 active_tree_.get()));
1580 }
[email protected]b5651c22013-03-14 15:06:331581 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271582 active_tree_->root_layer());
1583 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511584
[email protected]c9280762013-08-01 06:28:571585 // Process any requests in the UI resource queue. The request queue is given
1586 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1587 // the swap.
1588 pending_tree_->ProcessUIResourceRequestQueue();
1589
[email protected]c1bb5af2013-03-13 19:06:271590 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511591
[email protected]c1bb5af2013-03-13 19:06:271592 // Now that we've synced everything from the pending tree to the active
1593 // tree, rename the pending tree the recycle tree so we can reuse it on the
1594 // next sync.
1595 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511596
[email protected]7d1b07e2013-10-01 17:31:301597 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471598 active_tree_->SetRootLayerScrollOffsetDelegate(
1599 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221600
[email protected]c1bb5af2013-03-13 19:06:271601 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441602 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271603 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241604
[email protected]a848c102013-03-26 08:59:091605 if (debug_state_.continuous_painting) {
1606 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431607 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091608 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1609 stats.main_stats.record_time +
1610 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271611 }
[email protected]2a61ad52013-05-13 14:01:291612
1613 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261614 if (!tree_activation_callback_.is_null())
1615 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011616
1617 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1618 DCHECK(active_tree_->root_layer());
[email protected]dbe759a2013-12-02 19:23:021619 devtools_instrumentation::didActivateLayerTree(id_,
1620 active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271621}
1622
[email protected]c1bb5af2013-03-13 19:06:271623void LayerTreeHostImpl::SetVisible(bool visible) {
1624 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141625
[email protected]c1bb5af2013-03-13 19:06:271626 if (visible_ == visible)
1627 return;
1628 visible_ = visible;
1629 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121630 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141631
[email protected]127bdc1a2013-09-11 01:44:481632 if (!visible_)
1633 EvictAllUIResources();
1634
[email protected]0fa42e02013-08-20 01:16:051635 // Evict tiles immediately if invisible since this tab may never get another
1636 // draw or timer tick.
1637 if (!visible_)
1638 ManageTiles();
1639
[email protected]c1bb5af2013-03-13 19:06:271640 if (!renderer_)
1641 return;
[email protected]94f206c12012-08-25 00:09:141642
[email protected]c1bb5af2013-03-13 19:06:271643 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141644}
1645
[email protected]6be422b2013-12-08 06:47:311646void LayerTreeHostImpl::SetNeedsRedraw() {
1647 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1648 client_->SetNeedsRedrawOnImplThread();
1649}
1650
[email protected]206a3922013-05-17 06:34:121651ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071652 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1653 if (debug_state_.rasterize_only_visible_content) {
[email protected]3f2ff112013-08-03 02:41:071654 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291655 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071656 }
[email protected]206a3922013-05-17 06:34:121657
[email protected]3f2ff112013-08-03 02:41:071658 if (zero_budget_) {
1659 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071660 }
1661
[email protected]206a3922013-05-17 06:34:121662 return actual;
1663}
1664
[email protected]3f2ff112013-08-03 02:41:071665size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1666 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1667}
1668
[email protected]990e050a2013-09-23 18:50:211669int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1670 return ManagedMemoryPolicy::PriorityCutoffToValue(
1671 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1672}
1673
[email protected]50644642013-06-20 13:58:551674void LayerTreeHostImpl::ReleaseTreeResources() {
1675 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551676 SendReleaseResourcesRecursive(active_tree_->root_layer());
1677 if (pending_tree_ && pending_tree_->root_layer())
1678 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1679 if (recycle_tree_ && recycle_tree_->root_layer())
1680 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571681
[email protected]127bdc1a2013-09-11 01:44:481682 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551683}
1684
1685void LayerTreeHostImpl::CreateAndSetRenderer(
1686 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201687 ResourceProvider* resource_provider,
1688 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551689 DCHECK(!renderer_);
1690 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321691 renderer_ = DelegatingRenderer::Create(
1692 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091693 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551694 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321695 &settings_,
[email protected]50644642013-06-20 13:58:551696 output_surface,
1697 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111698 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241699 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551700 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321701 renderer_ = SoftwareRenderer::Create(
1702 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551703 }
1704
[email protected]84975832013-06-29 00:24:111705 if (renderer_) {
[email protected]50644642013-06-20 13:58:551706 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111707 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561708
1709 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1710 // initialized to get max texture size. Also, after releasing resources,
1711 // trees need another update to generate new ones.
1712 active_tree_->set_needs_update_draw_properties();
1713 if (pending_tree_)
1714 pending_tree_->set_needs_update_draw_properties();
[email protected]84975832013-06-29 00:24:111715 }
[email protected]50644642013-06-20 13:58:551716}
1717
[email protected]50af3c22013-07-13 03:50:201718void LayerTreeHostImpl::CreateAndSetTileManager(
1719 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011720 ContextProvider* context_provider,
[email protected]50af3c22013-07-13 03:50:201721 bool using_map_image) {
1722 DCHECK(settings_.impl_side_painting);
1723 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011724 tile_manager_ =
1725 TileManager::Create(this,
1726 resource_provider,
[email protected]d50efc4d2014-01-09 10:39:281727 context_provider,
[email protected]b6eb8e332013-09-10 00:51:011728 settings_.num_raster_threads,
1729 rendering_stats_instrumentation_,
1730 using_map_image,
[email protected]b4664142013-11-08 00:50:311731 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121732 GetMaxRasterTasksUsageBytes(context_provider),
1733 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011734
[email protected]50af3c22013-07-13 03:50:201735 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191736 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201737}
1738
[email protected]50644642013-06-20 13:58:551739void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071740 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551741}
1742
[email protected]c1bb5af2013-03-13 19:06:271743bool LayerTreeHostImpl::InitializeRenderer(
1744 scoped_ptr<OutputSurface> output_surface) {
[email protected]ce2e8112013-11-28 07:44:361745#ifndef NDEBUG
1746 DCHECK(!renderer_ || did_lose_called_);
1747#endif
1748
[email protected]c1bb5af2013-03-13 19:06:271749 // Since we will create a new resource provider, we cannot continue to use
1750 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1751 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551752 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401753
[email protected]c1bb5af2013-03-13 19:06:271754 // Note: order is important here.
1755 renderer_.reset();
1756 tile_manager_.reset();
1757 resource_provider_.reset();
1758 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141759
[email protected]c1bb5af2013-03-13 19:06:271760 if (!output_surface->BindToClient(this))
1761 return false;
[email protected]be3181652012-09-25 13:02:131762
[email protected]a7f35682013-10-22 23:05:571763 scoped_ptr<ResourceProvider> resource_provider =
1764 ResourceProvider::Create(output_surface.get(),
1765 shared_bitmap_manager_,
1766 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341767 settings_.use_rgba_4444_textures,
1768 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551769 if (!resource_provider)
1770 return false;
[email protected]2b154b22013-06-07 09:03:271771
[email protected]50644642013-06-20 13:58:551772 if (output_surface->capabilities().deferred_gl_initialization)
1773 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131774
[email protected]50af3c22013-07-13 03:50:201775 bool skip_gl_renderer = false;
1776 CreateAndSetRenderer(
1777 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131778
[email protected]50644642013-06-20 13:58:551779 if (!renderer_)
1780 return false;
[email protected]6123930c2013-05-23 06:48:371781
[email protected]50644642013-06-20 13:58:551782 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201783 CreateAndSetTileManager(resource_provider.get(),
[email protected]b6eb8e332013-09-10 00:51:011784 output_surface->context_provider().get(),
[email protected]50af3c22013-07-13 03:50:201785 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241786 }
1787
[email protected]daea3d42013-10-23 17:04:501788 // Setup BeginImplFrameEmulation if it's not supported natively
1789 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351790 const base::TimeDelta display_refresh_interval =
1791 base::TimeDelta::FromMicroseconds(
1792 base::Time::kMicrosecondsPerSecond /
1793 settings_.refresh_rate);
1794
[email protected]daea3d42013-10-23 17:04:501795 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151796 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351797 settings_.throttle_frame_production,
1798 display_refresh_interval);
1799 }
1800
1801 int max_frames_pending =
1802 output_surface->capabilities().max_frames_pending;
1803 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351804 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351805 output_surface->SetMaxFramesPending(max_frames_pending);
1806
[email protected]50644642013-06-20 13:58:551807 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271808 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141809
[email protected]c1bb5af2013-03-13 19:06:271810 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211811
[email protected]c1bb5af2013-03-13 19:06:271812 return true;
[email protected]94f206c12012-08-25 00:09:141813}
1814
[email protected]2b154b22013-06-07 09:03:271815bool LayerTreeHostImpl::DeferredInitialize(
1816 scoped_refptr<ContextProvider> offscreen_context_provider) {
1817 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551818 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091819 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271820
[email protected]50644642013-06-20 13:58:551821 ReleaseTreeResources();
1822 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271823
[email protected]d9ca99e2013-08-28 21:49:481824 bool resource_provider_success = resource_provider_->InitializeGL();
1825
1826 bool success = resource_provider_success;
1827 if (success) {
1828 bool skip_gl_renderer = false;
1829 CreateAndSetRenderer(
1830 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1831 if (!renderer_)
1832 success = false;
1833 }
1834
1835 if (success) {
1836 if (offscreen_context_provider.get() &&
1837 !offscreen_context_provider->BindToCurrentThread())
1838 success = false;
1839 }
1840
[email protected]50644642013-06-20 13:58:551841 if (success) {
1842 EnforceZeroBudget(false);
1843 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481844 } else {
1845 if (offscreen_context_provider.get()) {
1846 if (offscreen_context_provider->BindToCurrentThread())
1847 offscreen_context_provider->VerifyContexts();
1848 offscreen_context_provider = NULL;
1849 }
1850
1851 client_->DidLoseOutputSurfaceOnImplThread();
1852
1853 if (resource_provider_success) {
1854 // If this fails the context provider will be dropped from the output
1855 // surface and destroyed. But the GLRenderer expects the output surface
1856 // to stick around - and hold onto the context3d - as long as it is alive.
1857 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1858 renderer_.reset();
1859
1860 // The resource provider can't stay in GL mode or it tries to clean up GL
1861 // stuff, but the context provider is going away on the output surface
1862 // which contradicts being in GL mode.
1863 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1864 resource_provider_->InitializeSoftware();
1865 }
[email protected]50644642013-06-20 13:58:551866 }
[email protected]b5174d712013-08-28 08:10:431867
1868 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271869 return success;
1870}
1871
[email protected]50af3c22013-07-13 03:50:201872void LayerTreeHostImpl::ReleaseGL() {
1873 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1874 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091875 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201876
1877 ReleaseTreeResources();
1878 renderer_.reset();
1879 tile_manager_.reset();
1880 resource_provider_->InitializeSoftware();
1881
1882 bool skip_gl_renderer = true;
1883 CreateAndSetRenderer(
1884 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1885 DCHECK(renderer_);
1886
1887 EnforceZeroBudget(true);
1888 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011889 NULL,
[email protected]50af3c22013-07-13 03:50:201890 GetRendererCapabilities().using_map_image);
1891 DCHECK(tile_manager_);
1892
[email protected]b5174d712013-08-28 08:10:431893 SetOffscreenContextProvider(NULL);
1894
[email protected]50af3c22013-07-13 03:50:201895 client_->SetNeedsCommitOnImplThread();
1896}
1897
[email protected]18ce59702013-04-09 04:58:401898void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1899 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271900 return;
[email protected]94f206c12012-08-25 00:09:141901
[email protected]54af03522013-09-05 00:43:281902 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271903 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271904
[email protected]c1bb5af2013-03-13 19:06:271905 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141906
[email protected]c1bb5af2013-03-13 19:06:271907 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141908
[email protected]c1bb5af2013-03-13 19:06:271909 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441910 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141911}
1912
[email protected]d9083762013-03-24 01:36:401913void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1914 if (overdraw_bottom_height == overdraw_bottom_height_)
1915 return;
1916 overdraw_bottom_height_ = overdraw_bottom_height;
1917
1918 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441919 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401920}
1921
[email protected]9f4f6a32013-09-04 21:35:121922void LayerTreeHostImpl::SetOverhangUIResource(
1923 UIResourceId overhang_ui_resource_id,
1924 gfx::Size overhang_ui_resource_size) {
1925 overhang_ui_resource_id_ = overhang_ui_resource_id;
1926 overhang_ui_resource_size_ = overhang_ui_resource_size;
1927}
1928
[email protected]c1bb5af2013-03-13 19:06:271929void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1930 if (device_scale_factor == device_scale_factor_)
1931 return;
1932 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271933
[email protected]c1bb5af2013-03-13 19:06:271934 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441935 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141936}
1937
[email protected]54af03522013-09-05 00:43:281938gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1939 return DeviceViewport().size();
1940}
1941
[email protected]f224cc92013-06-06 23:23:321942gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1943 if (external_viewport_.IsEmpty())
1944 return gfx::Rect(device_viewport_size_);
1945
1946 return external_viewport_;
1947}
1948
[email protected]54af03522013-09-05 00:43:281949gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1950 if (external_clip_.IsEmpty())
1951 return DeviceViewport();
1952
1953 return external_clip_;
1954}
1955
1956const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321957 return external_transform_;
1958}
1959
[email protected]c1bb5af2013-03-13 19:06:271960void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1961 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141962}
1963
[email protected]59adb112013-04-09 04:48:441964void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]4e4136d2013-11-29 02:22:441965 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:441966 active_tree_->set_needs_update_draw_properties();
1967 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141968}
1969
[email protected]c1bb5af2013-03-13 19:06:271970bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041971 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341972 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141973}
1974
[email protected]200a9c062013-05-20 04:34:371975void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1976 DCHECK(input_handler_client_ == NULL);
1977 input_handler_client_ = client;
1978}
1979
[email protected]420fdf6e2013-08-26 20:36:381980static LayerImpl* NextScrollLayer(LayerImpl* layer) {
1981 if (LayerImpl* scroll_parent = layer->scroll_parent())
1982 return scroll_parent;
1983 return layer->parent();
1984}
1985
[email protected]f620b0e72013-10-01 21:38:241986LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:471987 const gfx::PointF& device_viewport_point,
1988 InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:431989 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:021990 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:141991
[email protected]c1bb5af2013-03-13 19:06:271992 // Walk up the hierarchy and look for a scrollable layer.
1993 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:381994 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:271995 // The content layer can also block attempts to scroll outside the main
1996 // thread.
1997 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1998 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021999 *scroll_on_main_thread = true;
2000 return NULL;
[email protected]94f206c12012-08-25 00:09:142001 }
2002
[email protected]c1bb5af2013-03-13 19:06:272003 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2004 if (!scroll_layer_impl)
2005 continue;
[email protected]94f206c12012-08-25 00:09:142006
[email protected]c1bb5af2013-03-13 19:06:272007 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272008 // If any layer wants to divert the scroll event to the main thread, abort.
2009 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022010 *scroll_on_main_thread = true;
2011 return NULL;
[email protected]94f206c12012-08-25 00:09:142012 }
2013
[email protected]c1bb5af2013-03-13 19:06:272014 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2015 potentially_scrolling_layer_impl = scroll_layer_impl;
2016 }
2017
[email protected]bf1cfd9a2013-09-26 05:43:022018 return potentially_scrolling_layer_impl;
2019}
2020
2021InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
2022 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
2023 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2024
2025 if (top_controls_manager_)
2026 top_controls_manager_->ScrollBegin();
2027
2028 DCHECK(!CurrentlyScrollingLayer());
2029 ClearCurrentlyScrollingLayer();
2030
2031 if (!EnsureRenderSurfaceLayerList())
2032 return ScrollIgnored;
2033
[email protected]f620b0e72013-10-01 21:38:242034 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2035 device_scale_factor_);
2036 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2037 device_viewport_point,
2038 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022039 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242040 LayerImpl* potentially_scrolling_layer_impl =
2041 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2042 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022043
2044 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022045 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2046 return ScrollOnMainThread;
2047 }
2048
[email protected]d948c112014-01-10 02:13:532049 if (!potentially_scrolling_layer_impl)
[email protected]34d43432013-09-26 20:04:102050 potentially_scrolling_layer_impl = RootScrollLayer();
2051
[email protected]c1bb5af2013-03-13 19:06:272052 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202053 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272054 potentially_scrolling_layer_impl);
2055 should_bubble_scrolls_ = (type != NonBubblingGesture);
2056 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272057 client_->RenewTreePriority();
2058 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2059 return ScrollStarted;
2060 }
2061 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142062}
2063
[email protected]c1bb5af2013-03-13 19:06:272064gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2065 LayerImpl* layer_impl,
2066 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472067 const gfx::PointF& viewport_point,
[email protected]c1bb5af2013-03-13 19:06:272068 gfx::Vector2dF viewport_delta) {
2069 // Layers with non-invertible screen space transforms should not have passed
2070 // the scroll hit test in the first place.
2071 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2072 gfx::Transform inverse_screen_space_transform(
2073 gfx::Transform::kSkipInitialization);
2074 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2075 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352076 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2077 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272078 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142079
[email protected]c1bb5af2013-03-13 19:06:272080 gfx::PointF screen_space_point =
2081 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142082
[email protected]c1bb5af2013-03-13 19:06:272083 gfx::Vector2dF screen_space_delta = viewport_delta;
2084 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2085
2086 // First project the scroll start and end points to local layer space to find
2087 // the scroll delta in layer coordinates.
2088 bool start_clipped, end_clipped;
2089 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2090 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212091 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272092 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212093 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272094 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212095 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272096 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212097 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272098
2099 // In general scroll point coordinates should not get clipped.
2100 DCHECK(!start_clipped);
2101 DCHECK(!end_clipped);
2102 if (start_clipped || end_clipped)
2103 return gfx::Vector2dF();
2104
2105 // local_start_point and local_end_point are in content space but we want to
2106 // move them to layer space for scrolling.
2107 float width_scale = 1.f / layer_impl->contents_scale_x();
2108 float height_scale = 1.f / layer_impl->contents_scale_y();
2109 local_start_point.Scale(width_scale, height_scale);
2110 local_end_point.Scale(width_scale, height_scale);
2111
2112 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472113 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272114 layer_impl->ScrollBy(local_end_point - local_start_point);
2115
2116 // Get the end point in the layer's content space so we can apply its
2117 // ScreenSpaceTransform.
2118 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472119 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272120 previous_delta;
2121 gfx::PointF actual_local_content_end_point =
2122 gfx::ScalePoint(actual_local_end_point,
2123 1.f / width_scale,
2124 1.f / height_scale);
2125
2126 // Calculate the applied scroll delta in viewport space coordinates.
2127 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212128 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272129 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212130 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272131 DCHECK(!end_clipped);
2132 if (end_clipped)
2133 return gfx::Vector2dF();
2134 gfx::PointF actual_viewport_end_point =
2135 gfx::ScalePoint(actual_screen_space_end_point,
2136 1.f / scale_from_viewport_to_screen_space);
2137 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142138}
2139
[email protected]c1bb5af2013-03-13 19:06:272140static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
2141 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:472142 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272143 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472144 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272145}
2146
2147bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
2148 gfx::Vector2dF scroll_delta) {
2149 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2150 if (!CurrentlyScrollingLayer())
2151 return false;
2152
2153 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532154 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292155 bool did_scroll_x = false;
2156 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:422157 bool consume_by_top_controls = top_controls_manager_ &&
[email protected]d948c112014-01-10 02:13:532158 (scroll_delta.y() < 0 ||
2159 (RootScrollLayer() && CurrentlyScrollingLayer() == RootScrollLayer() &&
2160 RootScrollLayer()->max_scroll_offset().y() > 0));
[email protected]a91e4f82013-03-15 06:58:062161
[email protected]c1bb5af2013-03-13 19:06:272162 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2163 layer_impl;
2164 layer_impl = layer_impl->parent()) {
2165 if (!layer_impl->scrollable())
2166 continue;
2167
[email protected]a2b5ded2013-05-20 21:32:532168 if (layer_impl == RootScrollLayer()) {
2169 // Only allow bubble scrolling when the scroll is in the direction to make
2170 // the top controls visible.
[email protected]d948c112014-01-10 02:13:532171 if (consume_by_top_controls) {
[email protected]a2b5ded2013-05-20 21:32:532172 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
2173 UpdateMaxScrollOffset();
2174 }
2175 // Track root layer deltas for reporting overscroll.
2176 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422177 }
2178
[email protected]c1bb5af2013-03-13 19:06:272179 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272180 // Gesture events need to be transformed from viewport coordinates to local
2181 // layer coordinates so that the scrolling contents exactly follow the
2182 // user's finger. In contrast, wheel events represent a fixed amount of
2183 // scrolling so we can just apply them directly.
2184 if (!wheel_scrolling_) {
2185 float scale_from_viewport_to_screen_space = device_scale_factor_;
2186 applied_delta =
2187 ScrollLayerWithViewportSpaceDelta(layer_impl,
2188 scale_from_viewport_to_screen_space,
2189 viewport_point, pending_delta);
2190 } else {
2191 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142192 }
[email protected]94f206c12012-08-25 00:09:142193
[email protected]c1bb5af2013-03-13 19:06:272194 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292195 float move_threshold = 0.1f;
2196 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2197 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2198 did_scroll_x |= did_move_layer_x;
2199 did_scroll_y |= did_move_layer_y;
2200 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]db102612013-12-20 22:04:122201 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
[email protected]c1bb5af2013-03-13 19:06:272202 continue;
[email protected]db102612013-12-20 22:04:122203 else
2204 break;
[email protected]94f206c12012-08-25 00:09:142205 }
[email protected]a2b5ded2013-05-20 21:32:532206
2207 if (layer_impl == RootScrollLayer())
2208 unused_root_delta.Subtract(applied_delta);
2209
[email protected]c1bb5af2013-03-13 19:06:272210 did_lock_scrolling_layer_ = true;
2211 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202212 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272213 break;
[email protected]94f206c12012-08-25 00:09:142214 }
[email protected]94f206c12012-08-25 00:09:142215
[email protected]c1bb5af2013-03-13 19:06:272216 // If the applied delta is within 45 degrees of the input delta, bail out to
2217 // make it easier to scroll just one layer in one direction without
2218 // affecting any of its parents.
2219 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212220 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272221 applied_delta, pending_delta) < angle_threshold) {
2222 pending_delta = gfx::Vector2d();
2223 break;
[email protected]4a23c374c2012-12-08 08:38:552224 }
[email protected]c1bb5af2013-03-13 19:06:272225
2226 // Allow further movement only on an axis perpendicular to the direction in
2227 // which the layer moved.
2228 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212229 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272230
[email protected]a2b5ded2013-05-20 21:32:532231 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272232 break;
2233 }
2234
[email protected]2bd503f2013-07-23 05:35:292235 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272236 if (did_scroll) {
2237 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442238 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272239 client_->RenewTreePriority();
2240 }
[email protected]a2b5ded2013-05-20 21:32:532241
[email protected]2bd503f2013-07-23 05:35:292242 // Scrolling along an axis resets accumulated root overscroll for that axis.
2243 if (did_scroll_x)
2244 accumulated_root_overscroll_.set_x(0);
2245 if (did_scroll_y)
2246 accumulated_root_overscroll_.set_y(0);
2247
[email protected]a2b5ded2013-05-20 21:32:532248 accumulated_root_overscroll_ += unused_root_delta;
2249 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2250 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112251 DidOverscrollParams params;
2252 params.accumulated_overscroll = accumulated_root_overscroll_;
2253 params.latest_overscroll_delta = unused_root_delta;
2254 params.current_fling_velocity = current_fling_velocity_;
2255 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532256 }
2257
[email protected]c1bb5af2013-03-13 19:06:272258 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552259}
2260
[email protected]be782f52013-03-23 21:36:142261// This implements scrolling by page as described here:
2262// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2263// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492264bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2265 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142266 DCHECK(wheel_scrolling_);
2267
2268 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2269 layer_impl;
2270 layer_impl = layer_impl->parent()) {
2271 if (!layer_impl->scrollable())
2272 continue;
2273
2274 if (!layer_impl->vertical_scrollbar_layer())
2275 continue;
2276
2277 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2278
2279 // These magical values match WebKit and are designed to scroll nearly the
2280 // entire visible content height but leave a bit of overlap.
2281 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492282 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142283 page = -page;
2284
2285 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2286
2287 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2288
2289 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142290 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442291 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142292 client_->RenewTreePriority();
2293 return true;
2294 }
2295
2296 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2297 }
2298
2299 return false;
2300}
2301
[email protected]1960a712013-04-30 17:06:472302void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2303 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2304 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2305 active_tree_->SetRootLayerScrollOffsetDelegate(
2306 root_layer_scroll_offset_delegate_);
2307}
2308
2309void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2310 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2311 client_->SetNeedsCommitOnImplThread();
2312}
2313
[email protected]c1bb5af2013-03-13 19:06:272314void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2315 active_tree_->ClearCurrentlyScrollingLayer();
2316 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532317 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202318 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142319}
2320
[email protected]c1bb5af2013-03-13 19:06:272321void LayerTreeHostImpl::ScrollEnd() {
2322 if (top_controls_manager_)
2323 top_controls_manager_->ScrollEnd();
2324 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052325 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142326}
2327
[email protected]5ff3c9782013-04-29 17:35:122328InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462329 if (!active_tree_->CurrentlyScrollingLayer())
2330 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212331
[email protected]75147f52013-08-02 19:11:462332 if (settings_.ignore_root_layer_flings &&
2333 active_tree_->CurrentlyScrollingLayer() ==
2334 active_tree_->RootScrollLayer()) {
2335 ClearCurrentlyScrollingLayer();
2336 return ScrollIgnored;
2337 }
2338
[email protected]db102612013-12-20 22:04:122339 if (!wheel_scrolling_) {
2340 // Allow the fling to lock to the first layer that moves after the initial
2341 // fling |ScrollBy()| event.
2342 did_lock_scrolling_layer_ = false;
2343 should_bubble_scrolls_ = false;
2344 }
[email protected]df0c42342013-10-08 20:52:122345
[email protected]75147f52013-08-02 19:11:462346 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212347}
2348
[email protected]6e921bd2013-04-29 21:10:202349void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2350 current_fling_velocity_ = velocity;
2351}
2352
[email protected]bf1cfd9a2013-09-26 05:43:022353float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472354 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022355 LayerImpl* layer_impl) {
2356 if (!layer_impl)
2357 return std::numeric_limits<float>::max();
2358
2359 gfx::Rect layer_impl_bounds(
2360 layer_impl->content_bounds());
2361
2362 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2363 layer_impl->screen_space_transform(),
2364 layer_impl_bounds);
2365
2366 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2367 device_viewport_point);
2368}
2369
2370void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2371 if (!EnsureRenderSurfaceLayerList())
2372 return;
2373
[email protected]f620b0e72013-10-01 21:38:242374 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2375 device_scale_factor_);
2376
2377 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2378 device_viewport_point,
2379 active_tree_->RenderSurfaceLayerList());
2380 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2381 return;
2382
2383 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2384 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2385 scroll_layer_id_when_mouse_over_scrollbar_);
2386
2387 ScrollbarAnimationController* animation_controller =
2388 scroll_layer_impl->scrollbar_animation_controller();
2389 if (animation_controller) {
2390 animation_controller->DidMouseMoveOffScrollbar(
2391 CurrentPhysicalTimeTicks());
2392 StartScrollbarAnimation();
2393 }
2394 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2395 }
2396
[email protected]bf1cfd9a2013-09-26 05:43:022397 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242398 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2399 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022400 &scroll_on_main_thread);
2401 if (scroll_on_main_thread || !scroll_layer_impl)
2402 return;
2403
2404 ScrollbarAnimationController* animation_controller =
2405 scroll_layer_impl->scrollbar_animation_controller();
2406 if (!animation_controller)
2407 return;
2408
[email protected]bf1cfd9a2013-09-26 05:43:022409 float distance_to_scrollbar = std::min(
2410 DeviceSpaceDistanceToLayer(device_viewport_point,
2411 scroll_layer_impl->horizontal_scrollbar_layer()),
2412 DeviceSpaceDistanceToLayer(device_viewport_point,
2413 scroll_layer_impl->vertical_scrollbar_layer()));
2414
2415 bool should_animate = animation_controller->DidMouseMoveNear(
2416 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202417 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022418 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022419}
2420
[email protected]f620b0e72013-10-01 21:38:242421bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472422 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242423 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2424 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2425 layer_impl = active_tree_->LayerById(scroll_layer_id);
2426 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2427 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202428 bool should_animate =
2429 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2430 CurrentPhysicalTimeTicks(), 0);
2431 if (should_animate)
2432 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242433 } else {
2434 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2435 }
2436
2437 return true;
2438 }
2439
2440 return false;
2441}
2442
[email protected]c1bb5af2013-03-13 19:06:272443void LayerTreeHostImpl::PinchGestureBegin() {
2444 pinch_gesture_active_ = true;
2445 previous_pinch_anchor_ = gfx::Point();
2446 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132447 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]9ec24df2013-08-09 23:32:572448 active_tree_->SetCurrentlyScrollingLayer(RootScrollLayer());
[email protected]a9bda5e2013-10-25 18:43:372449 if (top_controls_manager_)
2450 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142451}
2452
[email protected]c1bb5af2013-03-13 19:06:272453void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2454 gfx::Point anchor) {
2455 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282456
[email protected]c1bb5af2013-03-13 19:06:272457 if (!RootScrollLayer())
2458 return;
[email protected]d3afa112012-12-08 06:24:282459
[email protected]c1bb5af2013-03-13 19:06:272460 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2461 // position over the course of the magnify.
2462 float page_scale_delta = active_tree_->page_scale_delta();
2463 gfx::PointF previous_scale_anchor =
2464 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2465 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2466 page_scale_delta = active_tree_->page_scale_delta();
2467 gfx::PointF new_scale_anchor =
2468 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2469 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2470
2471 previous_pinch_anchor_ = anchor;
2472
2473 move.Scale(1 / active_tree_->page_scale_factor());
2474
2475 RootScrollLayer()->ScrollBy(move);
2476
[email protected]c1bb5af2013-03-13 19:06:272477 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442478 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272479 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282480}
2481
[email protected]c1bb5af2013-03-13 19:06:272482void LayerTreeHostImpl::PinchGestureEnd() {
2483 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132484 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2485 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2486 ClearCurrentlyScrollingLayer();
2487 }
[email protected]a9bda5e2013-10-25 18:43:372488 if (top_controls_manager_)
2489 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272490 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142491}
2492
[email protected]c1bb5af2013-03-13 19:06:272493static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2494 LayerImpl* layer_impl) {
2495 if (!layer_impl)
2496 return;
[email protected]94f206c12012-08-25 00:09:142497
[email protected]c1bb5af2013-03-13 19:06:272498 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472499 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272500 if (!scroll_delta.IsZero()) {
2501 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392502 scroll.layer_id = layer_impl->id();
2503 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272504 scroll_info->scrolls.push_back(scroll);
2505 layer_impl->SetSentScrollDelta(scroll_delta);
2506 }
[email protected]94f206c12012-08-25 00:09:142507
[email protected]c1bb5af2013-03-13 19:06:272508 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2509 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142510}
2511
[email protected]c1bb5af2013-03-13 19:06:272512scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2513 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302514
[email protected]c1bb5af2013-03-13 19:06:272515 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392516 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2517 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302518
[email protected]c1bb5af2013-03-13 19:06:272519 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302520}
2521
[email protected]c1bb5af2013-03-13 19:06:272522void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282523 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102524}
2525
[email protected]c1bb5af2013-03-13 19:06:272526void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2527 if (!page_scale_animation_ || !RootScrollLayer())
2528 return;
2529
2530 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2531 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472532 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272533
[email protected]34e1b512013-09-26 17:32:332534 if (!page_scale_animation_->IsAnimationStarted())
2535 page_scale_animation_->StartAnimation(monotonic_time);
2536
[email protected]c1bb5af2013-03-13 19:06:272537 active_tree_->SetPageScaleDelta(
2538 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2539 active_tree_->page_scale_factor());
2540 gfx::Vector2dF next_scroll =
2541 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2542
2543 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442544 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272545
2546 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2547 page_scale_animation_.reset();
2548 client_->SetNeedsCommitOnImplThread();
2549 client_->RenewTreePriority();
2550 }
[email protected]829ad972013-01-28 23:36:102551}
2552
[email protected]ffb2720f2013-03-15 19:18:372553void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2554 if (!top_controls_manager_ || !RootScrollLayer())
2555 return;
2556 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2557 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172558 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2559 return;
[email protected]ffb2720f2013-03-15 19:18:372560 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2561 scroll, 1.f / active_tree_->total_page_scale_factor()));
2562}
2563
[email protected]c1bb5af2013-03-13 19:06:272564void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2565 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522566 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272567 animation_registrar_->active_animation_controllers().empty() ||
2568 !active_tree_->root_layer())
2569 return;
2570
2571 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2572
2573 last_animation_time_ = wall_clock_time;
2574 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2575
2576 AnimationRegistrar::AnimationControllerMap copy =
2577 animation_registrar_->active_animation_controllers();
2578 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2579 iter != copy.end();
2580 ++iter)
2581 (*iter).second->Animate(monotonic_seconds);
2582
[email protected]4e4136d2013-11-29 02:22:442583 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082584}
2585
[email protected]3d9f7432013-04-06 00:35:182586void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522587 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272588 animation_registrar_->active_animation_controllers().empty() ||
2589 !active_tree_->root_layer())
2590 return;
2591
2592 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2593 scoped_ptr<AnimationEventsVector> events =
2594 make_scoped_ptr(new AnimationEventsVector);
2595 AnimationRegistrar::AnimationControllerMap copy =
2596 animation_registrar_->active_animation_controllers();
2597 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2598 iter != copy.end();
2599 ++iter)
[email protected]3d9f7432013-04-06 00:35:182600 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272601
2602 if (!events->empty()) {
2603 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2604 last_animation_time_);
2605 }
[email protected]131a0c22013-02-12 18:31:082606}
2607
[email protected]c1bb5af2013-03-13 19:06:272608base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2609 return base::TimeDelta::FromSeconds(1);
2610}
2611
[email protected]50644642013-06-20 13:58:552612void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352613 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502614 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352615 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552616 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352617 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552618 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352619 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552620 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352621}
2622
[email protected]b5174d712013-08-28 08:10:432623void LayerTreeHostImpl::SetOffscreenContextProvider(
2624 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2625 if (!offscreen_context_provider.get()) {
2626 offscreen_context_provider_ = NULL;
2627 return;
2628 }
2629
2630 if (!offscreen_context_provider->BindToCurrentThread()) {
2631 offscreen_context_provider_ = NULL;
2632 return;
2633 }
2634
2635 offscreen_context_provider_ = offscreen_context_provider;
2636}
2637
[email protected]c1bb5af2013-03-13 19:06:272638std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2639 std::string str;
2640 if (active_tree_->root_layer()) {
2641 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2642 base::JSONWriter::WriteWithOptions(
2643 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2644 }
2645 return str;
2646}
2647
[email protected]c1bb5af2013-03-13 19:06:272648int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362649 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272650}
2651
[email protected]c1bb5af2013-03-13 19:06:272652void LayerTreeHostImpl::SendManagedMemoryStats(
2653 size_t memory_visible_bytes,
2654 size_t memory_visible_and_nearby_bytes,
2655 size_t memory_use_bytes) {
2656 if (!renderer_)
2657 return;
2658
2659 // Round the numbers being sent up to the next 8MB, to throttle the rate
2660 // at which we spam the GPU process.
2661 static const size_t rounding_step = 8 * 1024 * 1024;
2662 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2663 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2664 rounding_step);
2665 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2666 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2667 last_sent_memory_visible_and_nearby_bytes_ ==
2668 memory_visible_and_nearby_bytes &&
2669 last_sent_memory_use_bytes_ == memory_use_bytes) {
2670 return;
2671 }
2672 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2673 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2674 last_sent_memory_use_bytes_ = memory_use_bytes;
2675
2676 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2677 last_sent_memory_visible_and_nearby_bytes_,
2678 last_sent_memory_use_bytes_);
2679}
2680
2681void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2682 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2683}
2684
2685void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2686 base::TimeTicks time) {
2687 if (!layer)
2688 return;
2689
2690 ScrollbarAnimationController* scrollbar_controller =
2691 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352692 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202693 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422694 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2695 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442696 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202697 }
[email protected]c1bb5af2013-03-13 19:06:272698
2699 for (size_t i = 0; i < layer->children().size(); ++i)
2700 AnimateScrollbarsRecursive(layer->children()[i], time);
2701}
2702
[email protected]21c9dee72013-06-15 01:20:052703void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202704 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052705 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202706}
2707
2708void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2709 base::TimeTicks time) {
2710 if (!layer)
2711 return;
2712
2713 ScrollbarAnimationController* scrollbar_controller =
2714 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352715 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2716 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202717 if (delay > base::TimeDelta())
2718 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352719 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442720 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202721 }
2722
2723 for (size_t i = 0; i < layer->children().size(); ++i)
2724 StartScrollbarAnimationRecursive(layer->children()[i], time);
2725}
2726
[email protected]c1bb5af2013-03-13 19:06:272727void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2728 if (!tile_manager_)
2729 return;
2730
[email protected]1bcced22013-09-24 13:51:192731 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272732 return;
[email protected]1bcced22013-09-24 13:51:192733 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082734 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272735}
2736
[email protected]8347d692013-05-17 23:22:382737void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552738 current_frame_timeticks_ = base::TimeTicks();
2739 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272740}
2741
[email protected]21c9dee72013-06-15 01:20:052742void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2743 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552744 if (ticks->is_null()) {
2745 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052746 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552747 *now = base::Time::Now();
2748 }
2749}
2750
2751base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2752 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2753 return current_frame_timeticks_;
2754}
2755
2756base::Time LayerTreeHostImpl::CurrentFrameTime() {
2757 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272758 return current_frame_time_;
2759}
2760
[email protected]21c9dee72013-06-15 01:20:052761base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462762 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052763}
2764
[email protected]34806722013-08-09 23:51:212765scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2766 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272767 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222768 if (this->pending_tree_)
2769 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272770 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212771 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272772 if (tile_manager_)
2773 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2774 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222775 if (pending_tree_)
2776 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212777 if (frame)
2778 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222779 return state.PassAs<base::Value>();
2780}
2781
2782scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222783 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2784 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472785 if (tile_manager_)
2786 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272787 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082788}
2789
[email protected]6e7fdeb2013-07-09 14:28:382790void LayerTreeHostImpl::SetDebugState(
2791 const LayerTreeDebugState& new_debug_state) {
2792 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2793 return;
2794 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272795 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242796
[email protected]6e7fdeb2013-07-09 14:28:382797 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122798 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382799 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022800}
2801
[email protected]741fba422013-09-20 03:34:142802void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2803 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572804 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572805
[email protected]efa48412013-09-05 15:30:162806 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142807 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162808 case UIResourceBitmap::CLAMP_TO_EDGE:
2809 wrap_mode = GL_CLAMP_TO_EDGE;
2810 break;
2811 case UIResourceBitmap::REPEAT:
2812 wrap_mode = GL_REPEAT;
2813 break;
2814 }
2815
[email protected]c9280762013-08-01 06:28:572816 // Allow for multiple creation requests with the same UIResourceId. The
2817 // previous resource is simply deleted.
2818 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2819 if (id)
2820 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122821
2822 ResourceFormat format = resource_provider_->best_texture_format();
2823 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2824 format = ETC1;
[email protected]c9280762013-08-01 06:28:572825 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142826 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162827 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142828 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122829 format);
[email protected]c9280762013-08-01 06:28:572830
[email protected]efbdb3a2013-10-04 00:35:132831 UIResourceData data;
2832 data.resource_id = id;
2833 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512834 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132835
2836 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032837
2838 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572839 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032840 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142841 gfx::Rect(bitmap.GetSize()),
2842 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572843 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482844 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572845}
2846
2847void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2848 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2849 if (id) {
2850 resource_provider_->DeleteResource(id);
2851 ui_resource_map_.erase(uid);
2852 }
[email protected]127bdc1a2013-09-11 01:44:482853 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572854}
2855
[email protected]127bdc1a2013-09-11 01:44:482856void LayerTreeHostImpl::EvictAllUIResources() {
2857 if (ui_resource_map_.empty())
2858 return;
2859
[email protected]5f4dc4f2013-09-05 14:58:212860 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2861 iter != ui_resource_map_.end();
2862 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482863 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132864 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212865 }
2866 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482867
2868 client_->SetNeedsCommitOnImplThread();
2869 client_->OnCanDrawStateChanged(CanDraw());
2870 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212871}
2872
[email protected]c9280762013-08-01 06:28:572873ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2874 UIResourceId uid) const {
2875 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2876 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132877 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572878 return 0;
2879}
2880
[email protected]709c9542013-10-26 01:43:512881bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2882 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2883 DCHECK(iter != ui_resource_map_.end());
2884 return iter->second.opaque;
2885}
2886
[email protected]127bdc1a2013-09-11 01:44:482887bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2888 return !evicted_ui_resources_.empty();
2889}
2890
2891void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2892 std::set<UIResourceId>::iterator found_in_evicted =
2893 evicted_ui_resources_.find(uid);
2894 if (found_in_evicted == evicted_ui_resources_.end())
2895 return;
2896 evicted_ui_resources_.erase(found_in_evicted);
2897 if (evicted_ui_resources_.empty())
2898 client_->OnCanDrawStateChanged(CanDraw());
2899}
2900
[email protected]5e5648a2013-11-18 00:39:332901void LayerTreeHostImpl::ScheduleMicroBenchmark(
2902 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2903 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2904}
2905
[email protected]6be422b2013-12-08 06:47:312906void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2907 swap_promise_monitor_.insert(monitor);
2908}
2909
2910void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2911 swap_promise_monitor_.erase(monitor);
2912}
2913
2914void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
2915 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
2916 for (; it != swap_promise_monitor_.end(); it++)
2917 (*it)->OnSetNeedsRedrawOnImpl();
2918}
2919
[email protected]d3143c732012-10-05 19:17:592920} // namespace cc