blob: f9076fb9be87def8080895959ff2be042118032b [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)
116 if (context_provider->ContextCapabilities().egl_image_external)
117 return GL_TEXTURE_EXTERNAL_OES;
118 if (context_provider->ContextCapabilities().texture_rectangle)
119 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]f57bbc02012-11-21 07:02:15427}
428
[email protected]c1bb5af2013-03-13 19:06:27429void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
430 bool anchor_point,
431 float page_scale,
[email protected]c1bb5af2013-03-13 19:06:27432 base::TimeDelta duration) {
433 if (!RootScrollLayer())
434 return;
435
436 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47437 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27438 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28439 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27440
[email protected]85d136f782013-04-26 22:04:40441 // Easing constants experimentally determined.
442 scoped_ptr<TimingFunction> timing_function =
443 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
444
[email protected]c1bb5af2013-03-13 19:06:27445 page_scale_animation_ =
446 PageScaleAnimation::Create(scroll_total,
447 active_tree_->total_page_scale_factor(),
448 viewport_size,
449 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40450 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27451
452 if (anchor_point) {
453 gfx::Vector2dF anchor(target_offset);
454 page_scale_animation_->ZoomWithAnchor(anchor,
455 page_scale,
456 duration.InSecondsF());
457 } else {
458 gfx::Vector2dF scaled_target_offset = target_offset;
459 page_scale_animation_->ZoomTo(scaled_target_offset,
460 page_scale,
461 duration.InSecondsF());
462 }
463
[email protected]4e4136d2013-11-29 02:22:44464 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27465 client_->SetNeedsCommitOnImplThread();
466 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15467}
468
[email protected]c1bb5af2013-03-13 19:06:27469void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44470 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15471}
472
[email protected]c1bb5af2013-03-13 19:06:27473bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07474 if (!settings_.touch_hit_testing)
475 return true;
[email protected]c1bb5af2013-03-13 19:06:27476 if (!EnsureRenderSurfaceLayerList())
477 return false;
[email protected]f57bbc02012-11-21 07:02:15478
[email protected]c1bb5af2013-03-13 19:06:27479 gfx::PointF device_viewport_point =
480 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15481
[email protected]c974d5d2013-10-24 01:02:48482 LayerImpl* layer_impl =
483 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
484 device_viewport_point,
485 active_tree_->RenderSurfaceLayerList());
486 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27487}
488
[email protected]6be422b2013-12-08 06:47:31489scoped_ptr<SwapPromiseMonitor>
490LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
491 ui::LatencyInfo* latency) {
492 return scoped_ptr<SwapPromiseMonitor>(
493 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08494}
495
[email protected]c1bb5af2013-03-13 19:06:27496void LayerTreeHostImpl::TrackDamageForAllSurfaces(
497 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28498 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27499 // For now, we use damage tracking to compute a global scissor. To do this, we
500 // must compute all damage tracking before drawing anything, so that we know
501 // the root damage rect. The root damage rect is then used to scissor each
502 // surface.
503
504 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06505 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27506 --surface_index) {
507 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
508 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
509 DCHECK(render_surface);
510 render_surface->damage_tracker()->UpdateDamageTrackingState(
511 render_surface->layer_list(),
512 render_surface_layer->id(),
513 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
514 render_surface->content_rect(),
515 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48516 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27517 }
518}
519
[email protected]34806722013-08-09 23:51:21520scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
521 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
522 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
523 value->SetBoolean("has_no_damage", has_no_damage);
524
525 // Quad data can be quite large, so only dump render passes if we select
526 // cc.debug.quads.
527 bool quads_enabled;
528 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
529 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
530 if (quads_enabled) {
531 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
532 for (size_t i = 0; i < render_passes.size(); ++i)
533 render_pass_list->Append(render_passes[i]->AsValue().release());
534 value->Set("render_passes", render_pass_list.release());
535 }
536 return value.PassAs<base::Value>();
537}
538
[email protected]c1bb5af2013-03-13 19:06:27539void LayerTreeHostImpl::FrameData::AppendRenderPass(
540 scoped_ptr<RenderPass> render_pass) {
541 render_passes_by_id[render_pass->id] = render_pass.get();
542 render_passes.push_back(render_pass.Pass());
543}
544
[email protected]ffbb2212013-06-02 23:47:59545static DrawMode GetDrawMode(OutputSurface* output_surface) {
546 if (output_surface->ForcedDrawToSoftwareDevice()) {
547 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09548 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59549 return DRAW_MODE_HARDWARE;
550 } else {
[email protected]a7f35682013-10-22 23:05:57551 DCHECK_EQ(!output_surface->software_device(),
552 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59553 return DRAW_MODE_SOFTWARE;
554 }
555}
556
[email protected]c1bb5af2013-03-13 19:06:27557static void AppendQuadsForLayer(RenderPass* target_render_pass,
558 LayerImpl* layer,
559 const OcclusionTrackerImpl& occlusion_tracker,
560 AppendQuadsData* append_quads_data) {
561 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49562 QuadCuller quad_culler(&target_render_pass->quad_list,
563 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27564 layer,
565 occlusion_tracker,
566 layer->ShowDebugBorders(),
567 for_surface);
568 layer->AppendQuads(&quad_culler, append_quads_data);
569}
570
571static void AppendQuadsForRenderSurfaceLayer(
572 RenderPass* target_render_pass,
573 LayerImpl* layer,
574 const RenderPass* contributing_render_pass,
575 const OcclusionTrackerImpl& occlusion_tracker,
576 AppendQuadsData* append_quads_data) {
577 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49578 QuadCuller quad_culler(&target_render_pass->quad_list,
579 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27580 layer,
581 occlusion_tracker,
582 layer->ShowDebugBorders(),
583 for_surface);
584
585 bool is_replica = false;
586 layer->render_surface()->AppendQuads(&quad_culler,
587 append_quads_data,
588 is_replica,
589 contributing_render_pass->id);
590
591 // Add replica after the surface so that it appears below the surface.
592 if (layer->has_replica()) {
593 is_replica = true;
594 layer->render_surface()->AppendQuads(&quad_culler,
595 append_quads_data,
596 is_replica,
597 contributing_render_pass->id);
598 }
599}
600
601static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08602 ResourceProvider::ResourceId overhang_resource_id,
603 gfx::SizeF overhang_resource_scaled_size,
604 gfx::Rect root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27605 RenderPass* target_render_pass,
606 LayerImpl* root_layer,
607 SkColor screen_background_color,
608 const OcclusionTrackerImpl& occlusion_tracker) {
609 if (!root_layer || !SkColorGetA(screen_background_color))
610 return;
611
612 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
613 if (fill_region.IsEmpty())
614 return;
615
[email protected]3744e27b2013-11-06 21:44:08616 // Divide the fill region into the part to be filled with the overhang
617 // resource and the part to be filled with the background color.
618 Region screen_background_color_region = fill_region;
619 Region overhang_region;
620 if (overhang_resource_id) {
621 overhang_region = fill_region;
622 overhang_region.Subtract(root_scroll_layer_rect);
623 screen_background_color_region.Intersect(root_scroll_layer_rect);
624 }
625
[email protected]c1bb5af2013-03-13 19:06:27626 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49627 QuadCuller quad_culler(&target_render_pass->quad_list,
628 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27629 root_layer,
630 occlusion_tracker,
631 root_layer->ShowDebugBorders(),
632 for_surface);
633
634 // Manually create the quad state for the gutter quads, as the root layer
635 // doesn't have any bounds and so can't generate this itself.
636 // TODO(danakj): Make the gutter quads generated by the solid color layer
637 // (make it smarter about generating quads to fill unoccluded areas).
638
639 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
640 float opacity = 1.f;
641 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49642 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27643 shared_quad_state->SetAll(root_layer->draw_transform(),
644 root_target_rect.size(),
645 root_target_rect,
646 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01647 false,
[email protected]7bbeaf4e2013-11-26 10:27:22648 opacity,
649 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15650
[email protected]c1bb5af2013-03-13 19:06:27651 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17652
[email protected]c1bb5af2013-03-13 19:06:27653 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
654 bool did_invert = root_layer->screen_space_transform().GetInverse(
655 &transform_to_layer_space);
656 DCHECK(did_invert);
[email protected]3744e27b2013-11-06 21:44:08657 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27658 fill_rects.has_rect();
659 fill_rects.next()) {
660 // The root layer transform is composed of translations and scales only,
661 // no perspective, so mapping is sufficient (as opposed to projecting).
662 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21663 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]3744e27b2013-11-06 21:44:08664 // Skip the quad culler and just append the quads directly to avoid
665 // occlusion checks.
666 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
667 quad->SetNew(
668 shared_quad_state, layer_rect, screen_background_color, false);
669 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
670 }
671 for (Region::Iterator fill_rects(overhang_region);
672 fill_rects.has_rect();
673 fill_rects.next()) {
674 DCHECK(overhang_resource_id);
675 gfx::Rect layer_rect =
676 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
677 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
678 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
679 tex_quad->SetNew(
680 shared_quad_state,
681 layer_rect,
682 layer_rect,
683 overhang_resource_id,
684 false,
685 gfx::PointF(layer_rect.x() / overhang_resource_scaled_size.width(),
686 layer_rect.y() / overhang_resource_scaled_size.height()),
687 gfx::PointF(layer_rect.right() /
688 overhang_resource_scaled_size.width(),
689 layer_rect.bottom() /
690 overhang_resource_scaled_size.height()),
691 screen_background_color,
692 vertex_opacity,
693 false);
694 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27695 }
[email protected]467b3612012-08-28 07:41:16696}
697
[email protected]c1bb5af2013-03-13 19:06:27698bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
699 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14700
[email protected]c1bb5af2013-03-13 19:06:27701 if (!CanDraw() || !active_tree_->root_layer())
702 return false;
[email protected]2d692992012-12-19 01:19:32703
[email protected]c1bb5af2013-03-13 19:06:27704 TrackDamageForAllSurfaces(active_tree_->root_layer(),
705 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14706
[email protected]e0341352013-04-06 05:01:20707 // If the root render surface has no visible damage, then don't generate a
708 // frame at all.
709 RenderSurfaceImpl* root_surface =
710 active_tree_->root_layer()->render_surface();
711 bool root_surface_has_no_visible_damage =
712 !root_surface->damage_tracker()->current_damage_rect().Intersects(
713 root_surface->content_rect());
714 bool root_surface_has_contributing_layers =
715 !root_surface->layer_list().empty();
716 if (root_surface_has_contributing_layers &&
717 root_surface_has_no_visible_damage) {
718 TRACE_EVENT0("cc",
719 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
720 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45721 // A copy request should cause damage, so we should not have any copy
722 // requests in this case.
723 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07724 DCHECK(!output_surface_->capabilities()
725 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20726 return true;
727 }
728
[email protected]c1bb5af2013-03-13 19:06:27729 TRACE_EVENT1("cc",
730 "LayerTreeHostImpl::CalculateRenderPasses",
731 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06732 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14733
[email protected]c1bb5af2013-03-13 19:06:27734 // Create the render passes in dependency order.
735 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06736 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27737 --surface_index) {
738 LayerImpl* render_surface_layer =
739 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45740 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
741
742 bool should_draw_into_render_pass =
743 render_surface_layer->parent() == NULL ||
744 render_surface->contributes_to_drawn_surface() ||
745 render_surface_layer->HasCopyRequest();
746 if (should_draw_into_render_pass)
747 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27748 }
[email protected]94f206c12012-08-25 00:09:14749
[email protected]c1bb5af2013-03-13 19:06:27750 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52751 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27752 base::debug::TraceLog::GetInstance() &&
753 base::debug::TraceLog::GetInstance()->IsEnabled();
754 OcclusionTrackerImpl occlusion_tracker(
755 active_tree_->root_layer()->render_surface()->content_rect(),
756 record_metrics_for_frame);
757 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52758 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14759
[email protected]846f455b2013-03-18 19:07:41760 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27761 occlusion_tracker.set_occluding_screen_space_rects_container(
762 &frame->occluding_screen_space_rects);
763 }
[email protected]846f455b2013-03-18 19:07:41764 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27765 occlusion_tracker.set_non_occluding_screen_space_rects_container(
766 &frame->non_occluding_screen_space_rects);
767 }
[email protected]94f206c12012-08-25 00:09:14768
[email protected]c1bb5af2013-03-13 19:06:27769 // Add quads to the Render passes in FrontToBack order to allow for testing
770 // occlusion and performing culling during the tree walk.
771 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28772 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27773 RenderSurfaceImpl,
774 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14775
[email protected]c1bb5af2013-03-13 19:06:27776 // Typically when we are missing a texture and use a checkerboard quad, we
777 // still draw the frame. However when the layer being checkerboarded is moving
778 // due to an impl-animation, we drop the frame to avoid flashing due to the
779 // texture suddenly appearing in the future.
780 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25781 // When we have a copy request for a layer, we need to draw no matter
782 // what, as the layer may disappear after this frame.
783 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14784
[email protected]372bad5f2013-03-21 16:38:43785 int layers_drawn = 0;
786
[email protected]ffbb2212013-06-02 23:47:59787 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
788
[email protected]c1bb5af2013-03-13 19:06:27789 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09790 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27791 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09792 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27793 it != end;
794 ++it) {
795 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09796 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27797 RenderPass* target_render_pass =
798 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14799
[email protected]e47b0a0a2013-11-18 23:26:22800 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14801
[email protected]30fe19ff2013-07-04 00:54:45802 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30803
[email protected]18a70192013-04-26 16:18:25804 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36805 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25806 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11807 it->TakeCopyRequestsAndTransformToTarget(
808 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25809 }
[email protected]30fe19ff2013-07-04 00:54:45810 } else if (it.represents_contributing_render_surface() &&
811 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27812 RenderPass::Id contributing_render_pass_id =
813 it->render_surface()->RenderPassId();
814 RenderPass* contributing_render_pass =
815 frame->render_passes_by_id[contributing_render_pass_id];
816 AppendQuadsForRenderSurfaceLayer(target_render_pass,
817 *it,
818 contributing_render_pass,
819 occlusion_tracker,
820 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45821 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09822 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27823 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15824 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27825 it->render_target(),
826 it->visible_content_rect(),
827 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45828 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15829 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27830 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59831
[email protected]c1bb5af2013-03-13 19:06:27832 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57833
[email protected]c1bb5af2013-03-13 19:06:27834 if (it->HasContributingDelegatedRenderPasses()) {
835 RenderPass::Id contributing_render_pass_id =
836 it->FirstContributingRenderPassId();
837 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
838 frame->render_passes_by_id.end()) {
839 RenderPass* render_pass =
840 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14841
[email protected]c1bb5af2013-03-13 19:06:27842 AppendQuadsData append_quads_data(render_pass->id);
843 AppendQuadsForLayer(render_pass,
844 *it,
845 occlusion_tracker,
846 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57847
[email protected]c1bb5af2013-03-13 19:06:27848 contributing_render_pass_id =
849 it->NextContributingRenderPassId(contributing_render_pass_id);
850 }
[email protected]94f206c12012-08-25 00:09:14851 }
852
[email protected]c1bb5af2013-03-13 19:06:27853 AppendQuadsForLayer(target_render_pass,
854 *it,
855 occlusion_tracker,
856 &append_quads_data);
857 }
[email protected]89228202012-08-29 03:20:30858
[email protected]372bad5f2013-03-21 16:38:43859 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14860 }
861
[email protected]e1e768f2013-03-26 08:48:09862 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27863 bool layer_has_animating_transform =
864 it->screen_space_transform_is_animating() ||
865 it->draw_transform_is_animating();
866 if (layer_has_animating_transform)
867 draw_frame = false;
868 }
869
[email protected]e1e768f2013-03-26 08:48:09870 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27871 frame->contains_incomplete_tile = true;
872
873 occlusion_tracker.LeaveLayer(it);
874 }
875
[email protected]6133cc232013-07-30 18:47:07876 if (have_copy_request ||
877 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25878 draw_frame = true;
879
[email protected]1d993172012-10-18 18:15:04880#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27881 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
882 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
883 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
884 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
885 != frame->render_passes_by_id.end());
886 }
[email protected]94f206c12012-08-25 00:09:14887#endif
[email protected]c1bb5af2013-03-13 19:06:27888 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14889
[email protected]c1bb5af2013-03-13 19:06:27890 if (!active_tree_->has_transparent_background()) {
891 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08892 AppendQuadsToFillScreen(
893 ResourceIdForUIResource(overhang_ui_resource_id_),
894 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
895 active_tree_->RootScrollLayerDeviceViewportBounds(),
896 frame->render_passes.back(),
897 active_tree_->root_layer(),
898 active_tree_->background_color(),
899 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27900 }
[email protected]94f206c12012-08-25 00:09:14901
[email protected]c1bb5af2013-03-13 19:06:27902 if (draw_frame)
903 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25904 else
905 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14906
[email protected]c1bb5af2013-03-13 19:06:27907 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48908 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14909
[email protected]30fe19ff2013-07-04 00:54:45910 // Any copy requests left in the tree are not going to get serviced, and
911 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11912 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
913 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
914 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
915 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45916 }
[email protected]19334ac2013-12-12 23:59:11917 for (size_t i = 0; i < requests_to_abort.size(); ++i)
918 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45919
[email protected]e0341352013-04-06 05:01:20920 // If we're making a frame to draw, it better have at least one render pass.
921 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48922
923 // Should only have one render pass in resourceless software mode.
924 if (output_surface_->ForcedDrawToSoftwareDevice())
925 DCHECK_EQ(1u, frame->render_passes.size());
926
[email protected]c1bb5af2013-03-13 19:06:27927 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14928}
929
[email protected]200a9c062013-05-20 04:34:37930void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
931 if (input_handler_client_)
932 input_handler_client_->MainThreadHasStoppedFlinging();
933}
934
[email protected]3d9f7432013-04-06 00:35:18935void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
936 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15937 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01938 if (should_background_tick)
939 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15940
[email protected]3d9f7432013-04-06 00:35:18941 bool enabled = should_background_tick &&
942 !animation_registrar_->active_animation_controllers().empty();
943
[email protected]c1bb5af2013-03-13 19:06:27944 // Lazily create the time_source adapter so that we can vary the interval for
945 // testing.
946 if (!time_source_client_adapter_) {
947 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
948 this,
[email protected]810d40b72013-06-20 18:26:15949 DelayBasedTimeSource::Create(
950 LowFrequencyAnimationInterval(),
951 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
952 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27953 }
[email protected]94f206c12012-08-25 00:09:14954
[email protected]c1bb5af2013-03-13 19:06:27955 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14956}
957
[email protected]b8384e22013-12-03 02:20:48958void LayerTreeHostImpl::DidAnimateScrollOffset() {
959 client_->SetNeedsCommitOnImplThread();
960 client_->RenewTreePriority();
961}
962
[email protected]b9d4a362013-04-23 05:36:27963void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02964 viewport_damage_rect_.Union(damage_rect);
965}
966
[email protected]c1bb5af2013-03-13 19:06:27967static inline RenderPass* FindRenderPassById(
968 RenderPass::Id render_pass_id,
969 const LayerTreeHostImpl::FrameData& frame) {
970 RenderPassIdHashMap::const_iterator it =
971 frame.render_passes_by_id.find(render_pass_id);
972 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14973}
974
[email protected]c1bb5af2013-03-13 19:06:27975static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
976 LayerTreeHostImpl::FrameData* frame) {
977 RenderPass* remove_render_pass =
978 FindRenderPassById(remove_render_pass_id, *frame);
979 // The pass was already removed by another quad - probably the original, and
980 // we are the replica.
981 if (!remove_render_pass)
982 return;
983 RenderPassList& render_passes = frame->render_passes;
984 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
985 render_passes.end(),
986 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14987
[email protected]c1bb5af2013-03-13 19:06:27988 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14989
[email protected]c1bb5af2013-03-13 19:06:27990 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
991 frame->render_passes.erase(to_remove);
992 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14993
[email protected]c1bb5af2013-03-13 19:06:27994 // Now follow up for all RenderPass quads and remove their RenderPasses
995 // recursively.
996 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29997 QuadList::ConstBackToFrontIterator quad_list_iterator =
998 quad_list.BackToFrontBegin();
999 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271000 ++quad_list_iterator) {
1001 DrawQuad* current_quad = (*quad_list_iterator);
1002 if (current_quad->material != DrawQuad::RENDER_PASS)
1003 continue;
[email protected]94f206c12012-08-25 00:09:141004
[email protected]c1bb5af2013-03-13 19:06:271005 RenderPass::Id next_remove_render_pass_id =
1006 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1007 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1008 }
[email protected]94f206c12012-08-25 00:09:141009}
1010
[email protected]c1bb5af2013-03-13 19:06:271011bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1012 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1013 const RenderPass* render_pass =
1014 FindRenderPassById(quad.render_pass_id, frame);
1015 if (!render_pass)
1016 return false;
[email protected]94f206c12012-08-25 00:09:141017
[email protected]c1bb5af2013-03-13 19:06:271018 // If any quad or RenderPass draws into this RenderPass, then keep it.
1019 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291020 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1021 quad_list.BackToFrontBegin();
1022 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271023 ++quad_list_iterator) {
1024 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141025
[email protected]c1bb5af2013-03-13 19:06:271026 if (current_quad->material != DrawQuad::RENDER_PASS)
1027 return false;
[email protected]94f206c12012-08-25 00:09:141028
[email protected]c1bb5af2013-03-13 19:06:271029 const RenderPass* contributing_pass = FindRenderPassById(
1030 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1031 if (contributing_pass)
1032 return false;
1033 }
1034 return true;
[email protected]94f206c12012-08-25 00:09:141035}
1036
1037// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271038template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271039 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1040 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141041
1042// static
[email protected]c1bb5af2013-03-13 19:06:271043template <typename RenderPassCuller>
1044void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1045 FrameData* frame) {
1046 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1047 it != culler.RenderPassListEnd(frame->render_passes);
1048 it = culler.RenderPassListNext(it)) {
1049 const RenderPass* current_pass = frame->render_passes[it];
1050 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291051 QuadList::ConstBackToFrontIterator quad_list_iterator =
1052 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141053
[email protected]ed511b8d2013-03-25 03:29:291054 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271055 ++quad_list_iterator) {
1056 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141057
[email protected]c1bb5af2013-03-13 19:06:271058 if (current_quad->material != DrawQuad::RENDER_PASS)
1059 continue;
[email protected]94f206c12012-08-25 00:09:141060
[email protected]c1bb5af2013-03-13 19:06:271061 const RenderPassDrawQuad* render_pass_quad =
1062 RenderPassDrawQuad::MaterialCast(current_quad);
1063 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1064 continue;
[email protected]94f206c12012-08-25 00:09:141065
[email protected]c1bb5af2013-03-13 19:06:271066 // We are changing the vector in the middle of iteration. Because we
1067 // delete render passes that draw into the current pass, we are
1068 // guaranteed that any data from the iterator to the end will not
1069 // change. So, capture the iterator position from the end of the
1070 // list, and restore it after the change.
1071 size_t position_from_end = frame->render_passes.size() - it;
1072 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1073 it = frame->render_passes.size() - position_from_end;
1074 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141075 }
[email protected]c1bb5af2013-03-13 19:06:271076 }
[email protected]94f206c12012-08-25 00:09:141077}
1078
[email protected]e0341352013-04-06 05:01:201079bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
1080 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471081 TRACE_EVENT1("cc",
1082 "LayerTreeHostImpl::PrepareToDraw",
1083 "SourceFrameNumber",
1084 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141085
[email protected]425e5332013-09-21 04:10:031086 if (need_to_update_visible_tiles_before_draw_ &&
1087 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1088 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191089 }
[email protected]425e5332013-09-21 04:10:031090 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191091
[email protected]7d19dc342013-05-02 22:02:041092 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271093
[email protected]c1bb5af2013-03-13 19:06:271094 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1095 frame->render_passes.clear();
1096 frame->render_passes_by_id.clear();
1097 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201098 frame->contains_incomplete_tile = false;
1099 frame->has_no_damage = false;
1100
1101 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021102 device_viewport_damage_rect.Union(viewport_damage_rect_);
1103 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201104
1105 active_tree_->root_layer()->render_surface()->damage_tracker()->
1106 AddDamageNextUpdate(device_viewport_damage_rect);
1107 }
[email protected]94f206c12012-08-25 00:09:141108
[email protected]6133cc232013-07-30 18:47:071109 if (!CalculateRenderPasses(frame)) {
1110 DCHECK(!output_surface_->capabilities()
1111 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:271112 return false;
[email protected]6133cc232013-07-30 18:47:071113 }
[email protected]94f206c12012-08-25 00:09:141114
[email protected]c1bb5af2013-03-13 19:06:271115 // If we return true, then we expect DrawLayers() to be called before this
1116 // function is called again.
1117 return true;
[email protected]94f206c12012-08-25 00:09:141118}
1119
[email protected]0309ba5e02013-06-26 04:11:081120void LayerTreeHostImpl::EvictTexturesForTesting() {
1121 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1122}
1123
[email protected]4f48f6e2013-08-27 06:33:381124void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1125 NOTREACHED();
1126}
1127
[email protected]425e5332013-09-21 04:10:031128void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1129 DidInitializeVisibleTile();
1130}
1131
[email protected]c1bb5af2013-03-13 19:06:271132void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1133 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121134
[email protected]c1bb5af2013-03-13 19:06:271135 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351136 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351137 ManagedMemoryPolicy::PriorityCutoffToValue(
1138 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351139 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271140 if (evicted_resources) {
1141 active_tree_->SetContentsTexturesPurged();
1142 if (pending_tree_)
1143 pending_tree_->SetContentsTexturesPurged();
1144 client_->SetNeedsCommitOnImplThread();
1145 client_->OnCanDrawStateChanged(CanDraw());
1146 client_->RenewTreePriority();
1147 }
1148 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431149
[email protected]8be1a9bf2013-05-01 03:45:191150 UpdateTileManagerMemoryPolicy(policy);
1151}
1152
1153void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1154 const ManagedMemoryPolicy& policy) {
1155 if (!tile_manager_)
1156 return;
1157
[email protected]8be1a9bf2013-05-01 03:45:191158 // TODO(reveman): We should avoid keeping around unused resources if
1159 // possible. crbug.com/224475
[email protected]1bcced22013-09-24 13:51:191160 global_tile_state_.memory_limit_in_bytes =
1161 visible_ ?
[email protected]0b8ef2842013-11-01 09:53:351162 policy.bytes_limit_when_visible : 0;
[email protected]1bcced22013-09-24 13:51:191163 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
1164 (static_cast<int64>(global_tile_state_.memory_limit_in_bytes) *
[email protected]8be1a9bf2013-05-01 03:45:191165 settings_.max_unused_resource_memory_percentage) / 100);
[email protected]1bcced22013-09-24 13:51:191166 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191167 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1168 visible_ ?
1169 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351170 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191171 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081172
[email protected]c48536a52013-09-14 00:02:081173 DidModifyTilePriorities();
1174}
1175
1176void LayerTreeHostImpl::DidModifyTilePriorities() {
1177 DCHECK(settings_.impl_side_painting);
1178 // Mark priorities as dirty and schedule a ManageTiles().
1179 tile_priorities_dirty_ = true;
1180 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141181}
1182
[email protected]86126792013-03-16 20:07:541183void LayerTreeHostImpl::DidInitializeVisibleTile() {
1184 // TODO(reveman): Determine tiles that changed and only damage
1185 // what's necessary.
1186 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031187 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541188 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471189}
1190
[email protected]94bf75c2013-06-12 13:20:041191void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381192 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041193}
1194
[email protected]3f2ff112013-08-03 02:41:071195void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1196 SetManagedMemoryPolicy(policy, zero_budget_);
1197}
1198
[email protected]5f07afc2013-07-19 05:17:181199void LayerTreeHostImpl::SetTreeActivationCallback(
1200 const base::Closure& callback) {
1201 DCHECK(proxy_->IsImplThread());
1202 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261203 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181204}
1205
[email protected]c1bb5af2013-03-13 19:06:271206void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071207 const ManagedMemoryPolicy& policy, bool zero_budget) {
1208 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271209 return;
[email protected]61de5812012-11-08 07:03:441210
[email protected]3f2ff112013-08-03 02:41:071211 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1212
1213 cached_managed_memory_policy_ = policy;
1214 zero_budget_ = zero_budget;
1215 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1216
1217 if (old_policy == actual_policy)
1218 return;
1219
1220 if (!proxy_->HasImplThread()) {
1221 // In single-thread mode, this can be called on the main thread by
1222 // GLRenderer::OnMemoryAllocationChanged.
1223 DebugScopedSetImplThread impl_thread(proxy_);
1224 EnforceManagedMemoryPolicy(actual_policy);
1225 } else {
1226 DCHECK(proxy_->IsImplThread());
1227 EnforceManagedMemoryPolicy(actual_policy);
1228 }
[email protected]50644642013-06-20 13:58:551229
[email protected]d7626ffd2013-03-29 00:17:421230 // If there is already enough memory to draw everything imaginable and the
1231 // new memory limit does not change this, then do not re-commit. Don't bother
1232 // skipping commits if this is not visible (commits don't happen when not
1233 // visible, there will almost always be a commit when this becomes visible).
1234 bool needs_commit = true;
1235 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071236 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1237 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1238 actual_policy.priority_cutoff_when_visible ==
1239 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421240 needs_commit = false;
1241 }
1242
[email protected]d7626ffd2013-03-29 00:17:421243 if (needs_commit)
1244 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141245}
1246
[email protected]f224cc92013-06-06 23:23:321247void LayerTreeHostImpl::SetExternalDrawConstraints(
1248 const gfx::Transform& transform,
[email protected]54af03522013-09-05 00:43:281249 gfx::Rect viewport,
1250 gfx::Rect clip,
1251 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321252 external_transform_ = transform;
1253 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281254 external_clip_ = clip;
1255 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321256}
1257
[email protected]1cd9f5552013-04-26 04:22:031258void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
[email protected]6be422b2013-12-08 06:47:311259 if (damage_rect.IsEmpty())
1260 return;
1261 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031262 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1263}
1264
[email protected]daea3d42013-10-23 17:04:501265void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1266 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451267}
1268
[email protected]4d7e46a2013-11-08 05:33:401269void LayerTreeHostImpl::DidSwapBuffers() {
1270 client_->DidSwapBuffersOnImplThread();
1271}
1272
[email protected]a7335e0b2013-09-18 09:34:511273void LayerTreeHostImpl::OnSwapBuffersComplete() {
1274 client_->OnSwapBuffersCompleteOnImplThread();
1275}
1276
1277void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271278 // TODO(piman): We may need to do some validation on this ack before
1279 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511280 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291281 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161282}
1283
[email protected]c1bb5af2013-03-13 19:06:271284void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1285 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291286}
1287
[email protected]c1bb5af2013-03-13 19:06:271288CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1289 CompositorFrameMetadata metadata;
1290 metadata.device_scale_factor = device_scale_factor_;
1291 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1292 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1293 metadata.root_layer_size = active_tree_->ScrollableSize();
1294 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1295 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1296 if (top_controls_manager_) {
1297 metadata.location_bar_offset =
1298 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1299 metadata.location_bar_content_translation =
1300 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011301 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271302 }
[email protected]bf189f62012-12-18 03:42:111303
[email protected]c1bb5af2013-03-13 19:06:271304 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111305 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271306
[email protected]ffb2720f2013-03-15 19:18:371307 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271308
1309 return metadata;
[email protected]bf189f62012-12-18 03:42:111310}
1311
[email protected]6f50b8fa2013-07-01 19:51:491312static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1313 layer->DidBeginTracing();
1314}
[email protected]37349bc2013-06-04 01:31:521315
[email protected]f0c2a242013-03-15 19:34:521316void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1317 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351318 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271319 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201320
[email protected]3519b872013-07-30 07:17:501321 if (frame->has_no_damage) {
1322 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071323 DCHECK(!output_surface_->capabilities()
1324 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201325 return;
[email protected]3519b872013-07-30 07:17:501326 }
[email protected]e0341352013-04-06 05:01:201327
[email protected]c1bb5af2013-03-13 19:06:271328 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141329
[email protected]213fcfc2013-08-24 14:59:271330 fps_counter_->SaveTimeStamp(frame_begin_time,
1331 !output_surface_->context_provider());
[email protected]94f206c12012-08-25 00:09:141332
[email protected]c1e6cc062013-08-24 03:35:351333 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091334 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351335 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431336
[email protected]c1bb5af2013-03-13 19:06:271337 if (tile_manager_) {
1338 memory_history_->SaveEntry(
1339 tile_manager_->memory_stats_from_last_assign());
1340 }
[email protected]1191d9d2013-02-02 06:00:331341
[email protected]846f455b2013-03-18 19:07:411342 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021343 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271344 active_tree_->root_layer(),
1345 *frame->render_surface_layer_list,
1346 frame->occluding_screen_space_rects,
1347 frame->non_occluding_screen_space_rects,
1348 debug_state_);
1349 }
[email protected]94f206c12012-08-25 00:09:141350
[email protected]a848c102013-03-26 08:59:091351 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1352 const RenderingStats& stats =
1353 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181354 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091355 }
1356
[email protected]37349bc2013-06-04 01:31:521357 bool is_new_trace;
1358 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1359 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491360 if (pending_tree_) {
1361 LayerTreeHostCommon::CallFunctionForSubtree(
1362 pending_tree_->root_layer(),
1363 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1364 }
1365 LayerTreeHostCommon::CallFunctionForSubtree(
1366 active_tree_->root_layer(),
1367 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521368 }
1369
[email protected]9197dbcb2013-05-15 20:28:511370 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211371 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1372 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1373 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081374
[email protected]c1bb5af2013-03-13 19:06:271375 // Because the contents of the HUD depend on everything else in the frame, the
1376 // contents of its texture are updated as the last thing before the frame is
1377 // drawn.
[email protected]671f6672013-10-01 07:31:071378 if (active_tree_->hud_layer()) {
1379 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531380 active_tree_->hud_layer()->UpdateHudTexture(
1381 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071382 }
[email protected]94f206c12012-08-25 00:09:141383
[email protected]b09c1942013-05-10 00:06:371384 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321385 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261386 bool disable_picture_quad_image_filtering =
1387 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321388
[email protected]b09c1942013-05-10 00:06:371389 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321390 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301391 temp_software_renderer->DrawFrame(&frame->render_passes,
1392 NULL,
1393 device_scale_factor_,
1394 DeviceViewport(),
1395 DeviceClip(),
1396 allow_partial_swap,
1397 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371398 } else {
[email protected]573e8e32013-09-09 17:24:321399 // We don't track damage on the HUD layer (it interacts with damage tracking
1400 // visualizations), so disable partial swaps to make the HUD layer display
1401 // properly.
1402 bool allow_partial_swap = !debug_state_.ShowHudRects();
1403
[email protected]b5174d712013-08-28 08:10:431404 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321405 offscreen_context_provider_.get(),
1406 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301407 DeviceViewport(),
1408 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261409 allow_partial_swap,
1410 false);
[email protected]b09c1942013-05-10 00:06:371411 }
[email protected]c1bb5af2013-03-13 19:06:271412 // The render passes should be consumed by the renderer.
1413 DCHECK(frame->render_passes.empty());
1414 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141415
[email protected]c1bb5af2013-03-13 19:06:271416 // The next frame should start by assuming nothing has changed, and changes
1417 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321418 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1419 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541420 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271421 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1422 DidDrawDamagedArea();
1423 }
1424 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351425
[email protected]adbe30f2013-10-11 21:12:331426 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1427 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181428 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141429}
1430
[email protected]c1bb5af2013-03-13 19:06:271431void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1432 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1433 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521434
[email protected]c1bb5af2013-03-13 19:06:271435 // Once all layers have been drawn, pending texture uploads should no
1436 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151437 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141438}
1439
[email protected]c1bb5af2013-03-13 19:06:271440void LayerTreeHostImpl::FinishAllRendering() {
1441 if (renderer_)
1442 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141443}
1444
[email protected]c1bb5af2013-03-13 19:06:271445bool LayerTreeHostImpl::IsContextLost() {
1446 DCHECK(proxy_->IsImplThread());
1447 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141448}
1449
[email protected]c1bb5af2013-03-13 19:06:271450const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1451 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141452}
1453
[email protected]e0341352013-04-06 05:01:201454bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]b69c1db2013-11-27 00:05:191455 if (frame.has_no_damage) {
1456 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201457 return false;
[email protected]b69c1db2013-11-27 00:05:191458 }
[email protected]4a28a612013-11-27 02:06:331459 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551460 active_tree()->FinishSwapPromises(&metadata);
[email protected]4a28a612013-11-27 02:06:331461 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161462 return true;
[email protected]94f206c12012-08-25 00:09:141463}
1464
[email protected]daea3d42013-10-23 17:04:501465void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451466 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501467 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451468}
1469
[email protected]54af03522013-09-05 00:43:281470gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341471 // Use the root container layer bounds if it clips to them, otherwise, the
1472 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441473 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341474 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441475 DCHECK(!top_controls_manager_);
1476 DCHECK_EQ(0, overdraw_bottom_height_);
1477 return container_layer->bounds();
1478 }
1479
[email protected]ffb2720f2013-03-15 19:18:371480 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281481 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371482
[email protected]ed511b8d2013-03-25 03:29:291483 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371484 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401485 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291486 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371487}
1488
[email protected]c1bb5af2013-03-13 19:06:271489void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191490 if (resource_provider_)
1491 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011492 // TODO(jamesr): The renderer_ check is needed to make some of the
1493 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1494 // important) in production. We should adjust the test to not need this.
1495 if (renderer_)
1496 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]ce2e8112013-11-28 07:44:361497#ifndef NDEBUG
1498 did_lose_called_ = true;
1499#endif
[email protected]94f206c12012-08-25 00:09:141500}
1501
[email protected]c1bb5af2013-03-13 19:06:271502void LayerTreeHostImpl::Readback(void* pixels,
1503 gfx::Rect rect_in_device_viewport) {
1504 DCHECK(renderer_);
1505 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141506}
1507
[email protected]59adb112013-04-09 04:48:441508bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341509 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011510}
1511
[email protected]c1bb5af2013-03-13 19:06:271512LayerImpl* LayerTreeHostImpl::RootLayer() const {
1513 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081514}
1515
[email protected]c1bb5af2013-03-13 19:06:271516LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1517 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081518}
1519
[email protected]c1bb5af2013-03-13 19:06:271520LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1521 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081522}
1523
[email protected]251699b2013-10-09 00:21:261524bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1525 return CurrentlyScrollingLayer() ||
1526 (RootScrollLayer() && RootScrollLayer()->IsExternalFlingActive());
1527}
1528
[email protected]94f206c12012-08-25 00:09:141529// Content layers can be either directly scrollable or contained in an outer
1530// scrolling layer which applies the scroll transform. Given a content layer,
1531// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271532static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1533 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021534 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271535
1536 if (layer_impl->scrollable())
1537 return layer_impl;
1538
1539 if (layer_impl->DrawsContent() &&
1540 layer_impl->parent() &&
1541 layer_impl->parent()->scrollable())
1542 return layer_impl->parent();
1543
[email protected]bf1cfd9a2013-09-26 05:43:021544 return NULL;
[email protected]94f206c12012-08-25 00:09:141545}
1546
[email protected]c1bb5af2013-03-13 19:06:271547void LayerTreeHostImpl::CreatePendingTree() {
1548 CHECK(!pending_tree_);
1549 if (recycle_tree_)
1550 recycle_tree_.swap(pending_tree_);
1551 else
1552 pending_tree_ = LayerTreeImpl::create(this);
1553 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111554 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271555}
1556
[email protected]8bb6cfa2013-07-23 00:11:191557void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191558 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1559 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191560 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481561}
1562
[email protected]c1bb5af2013-03-13 19:06:271563void LayerTreeHostImpl::ActivatePendingTree() {
1564 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111565 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351566
[email protected]4f48f6e2013-08-27 06:33:381567 need_to_update_visible_tiles_before_draw_ = true;
1568
[email protected]1960a712013-04-30 17:06:471569 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271570 active_tree_->PushPersistedState(pending_tree_.get());
1571 if (pending_tree_->needs_full_tree_sync()) {
1572 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331573 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271574 active_tree_->DetachLayerTree(),
1575 active_tree_.get()));
1576 }
[email protected]b5651c22013-03-14 15:06:331577 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271578 active_tree_->root_layer());
1579 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511580
[email protected]c9280762013-08-01 06:28:571581 // Process any requests in the UI resource queue. The request queue is given
1582 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1583 // the swap.
1584 pending_tree_->ProcessUIResourceRequestQueue();
1585
[email protected]c1bb5af2013-03-13 19:06:271586 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511587
[email protected]c1bb5af2013-03-13 19:06:271588 // Now that we've synced everything from the pending tree to the active
1589 // tree, rename the pending tree the recycle tree so we can reuse it on the
1590 // next sync.
1591 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511592
[email protected]7d1b07e2013-10-01 17:31:301593 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471594 active_tree_->SetRootLayerScrollOffsetDelegate(
1595 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221596
[email protected]c1bb5af2013-03-13 19:06:271597 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441598 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271599 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241600
[email protected]a848c102013-03-26 08:59:091601 if (debug_state_.continuous_painting) {
1602 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431603 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091604 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1605 stats.main_stats.record_time +
1606 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271607 }
[email protected]2a61ad52013-05-13 14:01:291608
1609 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261610 if (!tree_activation_callback_.is_null())
1611 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011612
1613 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1614 DCHECK(active_tree_->root_layer());
[email protected]dbe759a2013-12-02 19:23:021615 devtools_instrumentation::didActivateLayerTree(id_,
1616 active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271617}
1618
[email protected]c1bb5af2013-03-13 19:06:271619void LayerTreeHostImpl::SetVisible(bool visible) {
1620 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141621
[email protected]c1bb5af2013-03-13 19:06:271622 if (visible_ == visible)
1623 return;
1624 visible_ = visible;
1625 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121626 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141627
[email protected]127bdc1a2013-09-11 01:44:481628 if (!visible_)
1629 EvictAllUIResources();
1630
[email protected]0fa42e02013-08-20 01:16:051631 // Evict tiles immediately if invisible since this tab may never get another
1632 // draw or timer tick.
1633 if (!visible_)
1634 ManageTiles();
1635
[email protected]c1bb5af2013-03-13 19:06:271636 if (!renderer_)
1637 return;
[email protected]94f206c12012-08-25 00:09:141638
[email protected]c1bb5af2013-03-13 19:06:271639 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141640}
1641
[email protected]6be422b2013-12-08 06:47:311642void LayerTreeHostImpl::SetNeedsRedraw() {
1643 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1644 client_->SetNeedsRedrawOnImplThread();
1645}
1646
[email protected]206a3922013-05-17 06:34:121647ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071648 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1649 if (debug_state_.rasterize_only_visible_content) {
[email protected]3f2ff112013-08-03 02:41:071650 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291651 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071652 }
[email protected]206a3922013-05-17 06:34:121653
[email protected]3f2ff112013-08-03 02:41:071654 if (zero_budget_) {
1655 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071656 }
1657
[email protected]206a3922013-05-17 06:34:121658 return actual;
1659}
1660
[email protected]3f2ff112013-08-03 02:41:071661size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1662 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1663}
1664
[email protected]990e050a2013-09-23 18:50:211665int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1666 return ManagedMemoryPolicy::PriorityCutoffToValue(
1667 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1668}
1669
[email protected]50644642013-06-20 13:58:551670void LayerTreeHostImpl::ReleaseTreeResources() {
1671 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551672 SendReleaseResourcesRecursive(active_tree_->root_layer());
1673 if (pending_tree_ && pending_tree_->root_layer())
1674 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1675 if (recycle_tree_ && recycle_tree_->root_layer())
1676 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571677
[email protected]127bdc1a2013-09-11 01:44:481678 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551679}
1680
1681void LayerTreeHostImpl::CreateAndSetRenderer(
1682 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201683 ResourceProvider* resource_provider,
1684 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551685 DCHECK(!renderer_);
1686 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321687 renderer_ = DelegatingRenderer::Create(
1688 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091689 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551690 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321691 &settings_,
[email protected]50644642013-06-20 13:58:551692 output_surface,
1693 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111694 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241695 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551696 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321697 renderer_ = SoftwareRenderer::Create(
1698 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551699 }
1700
[email protected]84975832013-06-29 00:24:111701 if (renderer_) {
[email protected]50644642013-06-20 13:58:551702 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111703 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561704
1705 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1706 // initialized to get max texture size. Also, after releasing resources,
1707 // trees need another update to generate new ones.
1708 active_tree_->set_needs_update_draw_properties();
1709 if (pending_tree_)
1710 pending_tree_->set_needs_update_draw_properties();
[email protected]84975832013-06-29 00:24:111711 }
[email protected]50644642013-06-20 13:58:551712}
1713
[email protected]50af3c22013-07-13 03:50:201714void LayerTreeHostImpl::CreateAndSetTileManager(
1715 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011716 ContextProvider* context_provider,
[email protected]50af3c22013-07-13 03:50:201717 bool using_map_image) {
1718 DCHECK(settings_.impl_side_painting);
1719 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011720 tile_manager_ =
1721 TileManager::Create(this,
1722 resource_provider,
1723 settings_.num_raster_threads,
1724 rendering_stats_instrumentation_,
1725 using_map_image,
[email protected]b4664142013-11-08 00:50:311726 GetMaxTransferBufferUsageBytes(context_provider),
[email protected]8d7aa012013-11-23 21:19:121727 GetMaxRasterTasksUsageBytes(context_provider),
1728 GetMapImageTextureTarget(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011729
[email protected]50af3c22013-07-13 03:50:201730 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191731 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201732}
1733
[email protected]50644642013-06-20 13:58:551734void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071735 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551736}
1737
[email protected]c1bb5af2013-03-13 19:06:271738bool LayerTreeHostImpl::InitializeRenderer(
1739 scoped_ptr<OutputSurface> output_surface) {
[email protected]ce2e8112013-11-28 07:44:361740#ifndef NDEBUG
1741 DCHECK(!renderer_ || did_lose_called_);
1742#endif
1743
[email protected]c1bb5af2013-03-13 19:06:271744 // Since we will create a new resource provider, we cannot continue to use
1745 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1746 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551747 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401748
[email protected]c1bb5af2013-03-13 19:06:271749 // Note: order is important here.
1750 renderer_.reset();
1751 tile_manager_.reset();
1752 resource_provider_.reset();
1753 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141754
[email protected]c1bb5af2013-03-13 19:06:271755 if (!output_surface->BindToClient(this))
1756 return false;
[email protected]be3181652012-09-25 13:02:131757
[email protected]a7f35682013-10-22 23:05:571758 scoped_ptr<ResourceProvider> resource_provider =
1759 ResourceProvider::Create(output_surface.get(),
1760 shared_bitmap_manager_,
1761 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341762 settings_.use_rgba_4444_textures,
1763 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551764 if (!resource_provider)
1765 return false;
[email protected]2b154b22013-06-07 09:03:271766
[email protected]50644642013-06-20 13:58:551767 if (output_surface->capabilities().deferred_gl_initialization)
1768 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131769
[email protected]50af3c22013-07-13 03:50:201770 bool skip_gl_renderer = false;
1771 CreateAndSetRenderer(
1772 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131773
[email protected]50644642013-06-20 13:58:551774 if (!renderer_)
1775 return false;
[email protected]6123930c2013-05-23 06:48:371776
[email protected]50644642013-06-20 13:58:551777 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201778 CreateAndSetTileManager(resource_provider.get(),
[email protected]b6eb8e332013-09-10 00:51:011779 output_surface->context_provider().get(),
[email protected]50af3c22013-07-13 03:50:201780 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241781 }
1782
[email protected]daea3d42013-10-23 17:04:501783 // Setup BeginImplFrameEmulation if it's not supported natively
1784 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351785 const base::TimeDelta display_refresh_interval =
1786 base::TimeDelta::FromMicroseconds(
1787 base::Time::kMicrosecondsPerSecond /
1788 settings_.refresh_rate);
1789
[email protected]daea3d42013-10-23 17:04:501790 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151791 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351792 settings_.throttle_frame_production,
1793 display_refresh_interval);
1794 }
1795
1796 int max_frames_pending =
1797 output_surface->capabilities().max_frames_pending;
1798 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351799 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351800 output_surface->SetMaxFramesPending(max_frames_pending);
1801
[email protected]50644642013-06-20 13:58:551802 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271803 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141804
[email protected]c1bb5af2013-03-13 19:06:271805 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211806
[email protected]c1bb5af2013-03-13 19:06:271807 return true;
[email protected]94f206c12012-08-25 00:09:141808}
1809
[email protected]2b154b22013-06-07 09:03:271810bool LayerTreeHostImpl::DeferredInitialize(
1811 scoped_refptr<ContextProvider> offscreen_context_provider) {
1812 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551813 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091814 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271815
[email protected]50644642013-06-20 13:58:551816 ReleaseTreeResources();
1817 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271818
[email protected]d9ca99e2013-08-28 21:49:481819 bool resource_provider_success = resource_provider_->InitializeGL();
1820
1821 bool success = resource_provider_success;
1822 if (success) {
1823 bool skip_gl_renderer = false;
1824 CreateAndSetRenderer(
1825 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1826 if (!renderer_)
1827 success = false;
1828 }
1829
1830 if (success) {
1831 if (offscreen_context_provider.get() &&
1832 !offscreen_context_provider->BindToCurrentThread())
1833 success = false;
1834 }
1835
[email protected]50644642013-06-20 13:58:551836 if (success) {
1837 EnforceZeroBudget(false);
1838 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481839 } else {
1840 if (offscreen_context_provider.get()) {
1841 if (offscreen_context_provider->BindToCurrentThread())
1842 offscreen_context_provider->VerifyContexts();
1843 offscreen_context_provider = NULL;
1844 }
1845
1846 client_->DidLoseOutputSurfaceOnImplThread();
1847
1848 if (resource_provider_success) {
1849 // If this fails the context provider will be dropped from the output
1850 // surface and destroyed. But the GLRenderer expects the output surface
1851 // to stick around - and hold onto the context3d - as long as it is alive.
1852 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1853 renderer_.reset();
1854
1855 // The resource provider can't stay in GL mode or it tries to clean up GL
1856 // stuff, but the context provider is going away on the output surface
1857 // which contradicts being in GL mode.
1858 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1859 resource_provider_->InitializeSoftware();
1860 }
[email protected]50644642013-06-20 13:58:551861 }
[email protected]b5174d712013-08-28 08:10:431862
1863 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271864 return success;
1865}
1866
[email protected]50af3c22013-07-13 03:50:201867void LayerTreeHostImpl::ReleaseGL() {
1868 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1869 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091870 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201871
1872 ReleaseTreeResources();
1873 renderer_.reset();
1874 tile_manager_.reset();
1875 resource_provider_->InitializeSoftware();
1876
1877 bool skip_gl_renderer = true;
1878 CreateAndSetRenderer(
1879 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1880 DCHECK(renderer_);
1881
1882 EnforceZeroBudget(true);
1883 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011884 NULL,
[email protected]50af3c22013-07-13 03:50:201885 GetRendererCapabilities().using_map_image);
1886 DCHECK(tile_manager_);
1887
[email protected]b5174d712013-08-28 08:10:431888 SetOffscreenContextProvider(NULL);
1889
[email protected]50af3c22013-07-13 03:50:201890 client_->SetNeedsCommitOnImplThread();
1891}
1892
[email protected]18ce59702013-04-09 04:58:401893void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1894 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271895 return;
[email protected]94f206c12012-08-25 00:09:141896
[email protected]54af03522013-09-05 00:43:281897 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271898 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271899
[email protected]c1bb5af2013-03-13 19:06:271900 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141901
[email protected]c1bb5af2013-03-13 19:06:271902 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141903
[email protected]c1bb5af2013-03-13 19:06:271904 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441905 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141906}
1907
[email protected]d9083762013-03-24 01:36:401908void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1909 if (overdraw_bottom_height == overdraw_bottom_height_)
1910 return;
1911 overdraw_bottom_height_ = overdraw_bottom_height;
1912
1913 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441914 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401915}
1916
[email protected]9f4f6a32013-09-04 21:35:121917void LayerTreeHostImpl::SetOverhangUIResource(
1918 UIResourceId overhang_ui_resource_id,
1919 gfx::Size overhang_ui_resource_size) {
1920 overhang_ui_resource_id_ = overhang_ui_resource_id;
1921 overhang_ui_resource_size_ = overhang_ui_resource_size;
1922}
1923
[email protected]c1bb5af2013-03-13 19:06:271924void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1925 if (device_scale_factor == device_scale_factor_)
1926 return;
1927 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271928
[email protected]c1bb5af2013-03-13 19:06:271929 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441930 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141931}
1932
[email protected]54af03522013-09-05 00:43:281933gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1934 return DeviceViewport().size();
1935}
1936
[email protected]f224cc92013-06-06 23:23:321937gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1938 if (external_viewport_.IsEmpty())
1939 return gfx::Rect(device_viewport_size_);
1940
1941 return external_viewport_;
1942}
1943
[email protected]54af03522013-09-05 00:43:281944gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1945 if (external_clip_.IsEmpty())
1946 return DeviceViewport();
1947
1948 return external_clip_;
1949}
1950
1951const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321952 return external_transform_;
1953}
1954
[email protected]c1bb5af2013-03-13 19:06:271955void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1956 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141957}
1958
[email protected]59adb112013-04-09 04:48:441959void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]4e4136d2013-11-29 02:22:441960 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:441961 active_tree_->set_needs_update_draw_properties();
1962 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141963}
1964
[email protected]c1bb5af2013-03-13 19:06:271965bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041966 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341967 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141968}
1969
[email protected]200a9c062013-05-20 04:34:371970void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1971 DCHECK(input_handler_client_ == NULL);
1972 input_handler_client_ = client;
1973}
1974
[email protected]420fdf6e2013-08-26 20:36:381975static LayerImpl* NextScrollLayer(LayerImpl* layer) {
1976 if (LayerImpl* scroll_parent = layer->scroll_parent())
1977 return scroll_parent;
1978 return layer->parent();
1979}
1980
[email protected]f620b0e72013-10-01 21:38:241981LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
1982 gfx::PointF device_viewport_point, InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:431983 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:021984 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:141985
[email protected]c1bb5af2013-03-13 19:06:271986 // Walk up the hierarchy and look for a scrollable layer.
1987 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:381988 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:271989 // The content layer can also block attempts to scroll outside the main
1990 // thread.
1991 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1992 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021993 *scroll_on_main_thread = true;
1994 return NULL;
[email protected]94f206c12012-08-25 00:09:141995 }
1996
[email protected]c1bb5af2013-03-13 19:06:271997 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1998 if (!scroll_layer_impl)
1999 continue;
[email protected]94f206c12012-08-25 00:09:142000
[email protected]c1bb5af2013-03-13 19:06:272001 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272002 // If any layer wants to divert the scroll event to the main thread, abort.
2003 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022004 *scroll_on_main_thread = true;
2005 return NULL;
[email protected]94f206c12012-08-25 00:09:142006 }
2007
[email protected]c1bb5af2013-03-13 19:06:272008 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2009 potentially_scrolling_layer_impl = scroll_layer_impl;
2010 }
2011
2012 // When hiding top controls is enabled and the controls are hidden or
2013 // overlaying the content, force scrolls to be enabled on the root layer to
2014 // allow bringing the top controls back into view.
2015 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
2016 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:522017 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:272018 potentially_scrolling_layer_impl = RootScrollLayer();
2019 }
2020
[email protected]bf1cfd9a2013-09-26 05:43:022021 return potentially_scrolling_layer_impl;
2022}
2023
2024InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
2025 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
2026 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2027
2028 if (top_controls_manager_)
2029 top_controls_manager_->ScrollBegin();
2030
2031 DCHECK(!CurrentlyScrollingLayer());
2032 ClearCurrentlyScrollingLayer();
2033
2034 if (!EnsureRenderSurfaceLayerList())
2035 return ScrollIgnored;
2036
[email protected]f620b0e72013-10-01 21:38:242037 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2038 device_scale_factor_);
2039 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2040 device_viewport_point,
2041 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022042 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242043 LayerImpl* potentially_scrolling_layer_impl =
2044 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
2045 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:022046
2047 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022048 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2049 return ScrollOnMainThread;
2050 }
2051
[email protected]34d43432013-09-26 20:04:102052 // If we want to send a DidOverscroll for this scroll it can't be ignored.
2053 if (!potentially_scrolling_layer_impl && settings_.always_overscroll)
2054 potentially_scrolling_layer_impl = RootScrollLayer();
2055
[email protected]c1bb5af2013-03-13 19:06:272056 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202057 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272058 potentially_scrolling_layer_impl);
2059 should_bubble_scrolls_ = (type != NonBubblingGesture);
2060 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272061 client_->RenewTreePriority();
2062 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2063 return ScrollStarted;
2064 }
2065 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142066}
2067
[email protected]c1bb5af2013-03-13 19:06:272068gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2069 LayerImpl* layer_impl,
2070 float scale_from_viewport_to_screen_space,
2071 gfx::PointF viewport_point,
2072 gfx::Vector2dF viewport_delta) {
2073 // Layers with non-invertible screen space transforms should not have passed
2074 // the scroll hit test in the first place.
2075 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2076 gfx::Transform inverse_screen_space_transform(
2077 gfx::Transform::kSkipInitialization);
2078 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2079 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352080 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2081 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272082 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142083
[email protected]c1bb5af2013-03-13 19:06:272084 gfx::PointF screen_space_point =
2085 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142086
[email protected]c1bb5af2013-03-13 19:06:272087 gfx::Vector2dF screen_space_delta = viewport_delta;
2088 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2089
2090 // First project the scroll start and end points to local layer space to find
2091 // the scroll delta in layer coordinates.
2092 bool start_clipped, end_clipped;
2093 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2094 gfx::PointF local_start_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_point,
[email protected]fa816c62013-03-18 04:24:212097 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272098 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212099 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272100 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212101 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272102
2103 // In general scroll point coordinates should not get clipped.
2104 DCHECK(!start_clipped);
2105 DCHECK(!end_clipped);
2106 if (start_clipped || end_clipped)
2107 return gfx::Vector2dF();
2108
2109 // local_start_point and local_end_point are in content space but we want to
2110 // move them to layer space for scrolling.
2111 float width_scale = 1.f / layer_impl->contents_scale_x();
2112 float height_scale = 1.f / layer_impl->contents_scale_y();
2113 local_start_point.Scale(width_scale, height_scale);
2114 local_end_point.Scale(width_scale, height_scale);
2115
2116 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472117 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272118 layer_impl->ScrollBy(local_end_point - local_start_point);
2119
2120 // Get the end point in the layer's content space so we can apply its
2121 // ScreenSpaceTransform.
2122 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472123 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272124 previous_delta;
2125 gfx::PointF actual_local_content_end_point =
2126 gfx::ScalePoint(actual_local_end_point,
2127 1.f / width_scale,
2128 1.f / height_scale);
2129
2130 // Calculate the applied scroll delta in viewport space coordinates.
2131 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212132 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272133 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212134 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272135 DCHECK(!end_clipped);
2136 if (end_clipped)
2137 return gfx::Vector2dF();
2138 gfx::PointF actual_viewport_end_point =
2139 gfx::ScalePoint(actual_screen_space_end_point,
2140 1.f / scale_from_viewport_to_screen_space);
2141 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142142}
2143
[email protected]c1bb5af2013-03-13 19:06:272144static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
2145 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:472146 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272147 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472148 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272149}
2150
2151bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
2152 gfx::Vector2dF scroll_delta) {
2153 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2154 if (!CurrentlyScrollingLayer())
2155 return false;
2156
2157 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532158 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292159 bool did_scroll_x = false;
2160 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:422161 bool consume_by_top_controls = top_controls_manager_ &&
2162 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062163
[email protected]c1bb5af2013-03-13 19:06:272164 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2165 layer_impl;
2166 layer_impl = layer_impl->parent()) {
2167 if (!layer_impl->scrollable())
2168 continue;
2169
[email protected]a2b5ded2013-05-20 21:32:532170 if (layer_impl == RootScrollLayer()) {
2171 // Only allow bubble scrolling when the scroll is in the direction to make
2172 // the top controls visible.
2173 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
2174 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
2175 UpdateMaxScrollOffset();
2176 }
2177 // Track root layer deltas for reporting overscroll.
2178 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422179 }
2180
[email protected]c1bb5af2013-03-13 19:06:272181 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272182 // Gesture events need to be transformed from viewport coordinates to local
2183 // layer coordinates so that the scrolling contents exactly follow the
2184 // user's finger. In contrast, wheel events represent a fixed amount of
2185 // scrolling so we can just apply them directly.
2186 if (!wheel_scrolling_) {
2187 float scale_from_viewport_to_screen_space = device_scale_factor_;
2188 applied_delta =
2189 ScrollLayerWithViewportSpaceDelta(layer_impl,
2190 scale_from_viewport_to_screen_space,
2191 viewport_point, pending_delta);
2192 } else {
2193 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142194 }
[email protected]94f206c12012-08-25 00:09:142195
[email protected]c1bb5af2013-03-13 19:06:272196 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292197 float move_threshold = 0.1f;
2198 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2199 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2200 did_scroll_x |= did_move_layer_x;
2201 did_scroll_y |= did_move_layer_y;
2202 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]db102612013-12-20 22:04:122203 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_)
[email protected]c1bb5af2013-03-13 19:06:272204 continue;
[email protected]db102612013-12-20 22:04:122205 else
2206 break;
[email protected]94f206c12012-08-25 00:09:142207 }
[email protected]a2b5ded2013-05-20 21:32:532208
2209 if (layer_impl == RootScrollLayer())
2210 unused_root_delta.Subtract(applied_delta);
2211
[email protected]c1bb5af2013-03-13 19:06:272212 did_lock_scrolling_layer_ = true;
2213 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202214 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272215 break;
[email protected]94f206c12012-08-25 00:09:142216 }
[email protected]94f206c12012-08-25 00:09:142217
[email protected]c1bb5af2013-03-13 19:06:272218 // If the applied delta is within 45 degrees of the input delta, bail out to
2219 // make it easier to scroll just one layer in one direction without
2220 // affecting any of its parents.
2221 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212222 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272223 applied_delta, pending_delta) < angle_threshold) {
2224 pending_delta = gfx::Vector2d();
2225 break;
[email protected]4a23c374c2012-12-08 08:38:552226 }
[email protected]c1bb5af2013-03-13 19:06:272227
2228 // Allow further movement only on an axis perpendicular to the direction in
2229 // which the layer moved.
2230 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212231 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272232
[email protected]a2b5ded2013-05-20 21:32:532233 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272234 break;
2235 }
2236
[email protected]2bd503f2013-07-23 05:35:292237 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272238 if (did_scroll) {
2239 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442240 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272241 client_->RenewTreePriority();
2242 }
[email protected]a2b5ded2013-05-20 21:32:532243
[email protected]2bd503f2013-07-23 05:35:292244 // Scrolling along an axis resets accumulated root overscroll for that axis.
2245 if (did_scroll_x)
2246 accumulated_root_overscroll_.set_x(0);
2247 if (did_scroll_y)
2248 accumulated_root_overscroll_.set_y(0);
2249
[email protected]a2b5ded2013-05-20 21:32:532250 accumulated_root_overscroll_ += unused_root_delta;
2251 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2252 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112253 DidOverscrollParams params;
2254 params.accumulated_overscroll = accumulated_root_overscroll_;
2255 params.latest_overscroll_delta = unused_root_delta;
2256 params.current_fling_velocity = current_fling_velocity_;
2257 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532258 }
2259
[email protected]c1bb5af2013-03-13 19:06:272260 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552261}
2262
[email protected]be782f52013-03-23 21:36:142263// This implements scrolling by page as described here:
2264// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2265// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492266bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2267 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142268 DCHECK(wheel_scrolling_);
2269
2270 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2271 layer_impl;
2272 layer_impl = layer_impl->parent()) {
2273 if (!layer_impl->scrollable())
2274 continue;
2275
2276 if (!layer_impl->vertical_scrollbar_layer())
2277 continue;
2278
2279 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2280
2281 // These magical values match WebKit and are designed to scroll nearly the
2282 // entire visible content height but leave a bit of overlap.
2283 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492284 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142285 page = -page;
2286
2287 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2288
2289 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2290
2291 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142292 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442293 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142294 client_->RenewTreePriority();
2295 return true;
2296 }
2297
2298 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2299 }
2300
2301 return false;
2302}
2303
[email protected]1960a712013-04-30 17:06:472304void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2305 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2306 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2307 active_tree_->SetRootLayerScrollOffsetDelegate(
2308 root_layer_scroll_offset_delegate_);
2309}
2310
2311void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2312 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2313 client_->SetNeedsCommitOnImplThread();
2314}
2315
[email protected]c1bb5af2013-03-13 19:06:272316void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2317 active_tree_->ClearCurrentlyScrollingLayer();
2318 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532319 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202320 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142321}
2322
[email protected]c1bb5af2013-03-13 19:06:272323void LayerTreeHostImpl::ScrollEnd() {
2324 if (top_controls_manager_)
2325 top_controls_manager_->ScrollEnd();
2326 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052327 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142328}
2329
[email protected]5ff3c9782013-04-29 17:35:122330InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462331 if (!active_tree_->CurrentlyScrollingLayer())
2332 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212333
[email protected]75147f52013-08-02 19:11:462334 if (settings_.ignore_root_layer_flings &&
2335 active_tree_->CurrentlyScrollingLayer() ==
2336 active_tree_->RootScrollLayer()) {
2337 ClearCurrentlyScrollingLayer();
2338 return ScrollIgnored;
2339 }
2340
[email protected]db102612013-12-20 22:04:122341 if (!wheel_scrolling_) {
2342 // Allow the fling to lock to the first layer that moves after the initial
2343 // fling |ScrollBy()| event.
2344 did_lock_scrolling_layer_ = false;
2345 should_bubble_scrolls_ = false;
2346 }
[email protected]df0c42342013-10-08 20:52:122347
[email protected]75147f52013-08-02 19:11:462348 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212349}
2350
[email protected]6e921bd2013-04-29 21:10:202351void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2352 current_fling_velocity_ = velocity;
2353}
2354
[email protected]bf1cfd9a2013-09-26 05:43:022355float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
2356 gfx::PointF device_viewport_point,
2357 LayerImpl* layer_impl) {
2358 if (!layer_impl)
2359 return std::numeric_limits<float>::max();
2360
2361 gfx::Rect layer_impl_bounds(
2362 layer_impl->content_bounds());
2363
2364 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2365 layer_impl->screen_space_transform(),
2366 layer_impl_bounds);
2367
2368 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2369 device_viewport_point);
2370}
2371
2372void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2373 if (!EnsureRenderSurfaceLayerList())
2374 return;
2375
[email protected]f620b0e72013-10-01 21:38:242376 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2377 device_scale_factor_);
2378
2379 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2380 device_viewport_point,
2381 active_tree_->RenderSurfaceLayerList());
2382 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2383 return;
2384
2385 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2386 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2387 scroll_layer_id_when_mouse_over_scrollbar_);
2388
2389 ScrollbarAnimationController* animation_controller =
2390 scroll_layer_impl->scrollbar_animation_controller();
2391 if (animation_controller) {
2392 animation_controller->DidMouseMoveOffScrollbar(
2393 CurrentPhysicalTimeTicks());
2394 StartScrollbarAnimation();
2395 }
2396 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2397 }
2398
[email protected]bf1cfd9a2013-09-26 05:43:022399 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242400 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2401 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022402 &scroll_on_main_thread);
2403 if (scroll_on_main_thread || !scroll_layer_impl)
2404 return;
2405
2406 ScrollbarAnimationController* animation_controller =
2407 scroll_layer_impl->scrollbar_animation_controller();
2408 if (!animation_controller)
2409 return;
2410
[email protected]bf1cfd9a2013-09-26 05:43:022411 float distance_to_scrollbar = std::min(
2412 DeviceSpaceDistanceToLayer(device_viewport_point,
2413 scroll_layer_impl->horizontal_scrollbar_layer()),
2414 DeviceSpaceDistanceToLayer(device_viewport_point,
2415 scroll_layer_impl->vertical_scrollbar_layer()));
2416
2417 bool should_animate = animation_controller->DidMouseMoveNear(
2418 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202419 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022420 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022421}
2422
[email protected]f620b0e72013-10-01 21:38:242423bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
2424 gfx::PointF device_viewport_point) {
2425 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2426 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2427 layer_impl = active_tree_->LayerById(scroll_layer_id);
2428 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2429 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202430 bool should_animate =
2431 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2432 CurrentPhysicalTimeTicks(), 0);
2433 if (should_animate)
2434 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242435 } else {
2436 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2437 }
2438
2439 return true;
2440 }
2441
2442 return false;
2443}
2444
[email protected]c1bb5af2013-03-13 19:06:272445void LayerTreeHostImpl::PinchGestureBegin() {
2446 pinch_gesture_active_ = true;
2447 previous_pinch_anchor_ = gfx::Point();
2448 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132449 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]9ec24df2013-08-09 23:32:572450 active_tree_->SetCurrentlyScrollingLayer(RootScrollLayer());
[email protected]a9bda5e2013-10-25 18:43:372451 if (top_controls_manager_)
2452 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142453}
2454
[email protected]c1bb5af2013-03-13 19:06:272455void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2456 gfx::Point anchor) {
2457 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282458
[email protected]c1bb5af2013-03-13 19:06:272459 if (!RootScrollLayer())
2460 return;
[email protected]d3afa112012-12-08 06:24:282461
[email protected]c1bb5af2013-03-13 19:06:272462 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2463 // position over the course of the magnify.
2464 float page_scale_delta = active_tree_->page_scale_delta();
2465 gfx::PointF previous_scale_anchor =
2466 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2467 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2468 page_scale_delta = active_tree_->page_scale_delta();
2469 gfx::PointF new_scale_anchor =
2470 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2471 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2472
2473 previous_pinch_anchor_ = anchor;
2474
2475 move.Scale(1 / active_tree_->page_scale_factor());
2476
2477 RootScrollLayer()->ScrollBy(move);
2478
[email protected]c1bb5af2013-03-13 19:06:272479 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442480 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272481 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282482}
2483
[email protected]c1bb5af2013-03-13 19:06:272484void LayerTreeHostImpl::PinchGestureEnd() {
2485 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132486 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2487 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2488 ClearCurrentlyScrollingLayer();
2489 }
[email protected]a9bda5e2013-10-25 18:43:372490 if (top_controls_manager_)
2491 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272492 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142493}
2494
[email protected]c1bb5af2013-03-13 19:06:272495static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2496 LayerImpl* layer_impl) {
2497 if (!layer_impl)
2498 return;
[email protected]94f206c12012-08-25 00:09:142499
[email protected]c1bb5af2013-03-13 19:06:272500 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472501 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272502 if (!scroll_delta.IsZero()) {
2503 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392504 scroll.layer_id = layer_impl->id();
2505 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272506 scroll_info->scrolls.push_back(scroll);
2507 layer_impl->SetSentScrollDelta(scroll_delta);
2508 }
[email protected]94f206c12012-08-25 00:09:142509
[email protected]c1bb5af2013-03-13 19:06:272510 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2511 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142512}
2513
[email protected]c1bb5af2013-03-13 19:06:272514scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2515 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302516
[email protected]c1bb5af2013-03-13 19:06:272517 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392518 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2519 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302520
[email protected]c1bb5af2013-03-13 19:06:272521 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302522}
2523
[email protected]c1bb5af2013-03-13 19:06:272524void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282525 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102526}
2527
[email protected]c1bb5af2013-03-13 19:06:272528void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2529 if (!page_scale_animation_ || !RootScrollLayer())
2530 return;
2531
2532 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2533 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472534 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272535
[email protected]34e1b512013-09-26 17:32:332536 if (!page_scale_animation_->IsAnimationStarted())
2537 page_scale_animation_->StartAnimation(monotonic_time);
2538
[email protected]c1bb5af2013-03-13 19:06:272539 active_tree_->SetPageScaleDelta(
2540 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2541 active_tree_->page_scale_factor());
2542 gfx::Vector2dF next_scroll =
2543 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2544
2545 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442546 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272547
2548 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2549 page_scale_animation_.reset();
2550 client_->SetNeedsCommitOnImplThread();
2551 client_->RenewTreePriority();
2552 }
[email protected]829ad972013-01-28 23:36:102553}
2554
[email protected]ffb2720f2013-03-15 19:18:372555void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2556 if (!top_controls_manager_ || !RootScrollLayer())
2557 return;
2558 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2559 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172560 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2561 return;
[email protected]ffb2720f2013-03-15 19:18:372562 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2563 scroll, 1.f / active_tree_->total_page_scale_factor()));
2564}
2565
[email protected]c1bb5af2013-03-13 19:06:272566void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2567 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522568 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272569 animation_registrar_->active_animation_controllers().empty() ||
2570 !active_tree_->root_layer())
2571 return;
2572
2573 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2574
2575 last_animation_time_ = wall_clock_time;
2576 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2577
2578 AnimationRegistrar::AnimationControllerMap copy =
2579 animation_registrar_->active_animation_controllers();
2580 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2581 iter != copy.end();
2582 ++iter)
2583 (*iter).second->Animate(monotonic_seconds);
2584
[email protected]4e4136d2013-11-29 02:22:442585 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082586}
2587
[email protected]3d9f7432013-04-06 00:35:182588void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522589 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272590 animation_registrar_->active_animation_controllers().empty() ||
2591 !active_tree_->root_layer())
2592 return;
2593
2594 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2595 scoped_ptr<AnimationEventsVector> events =
2596 make_scoped_ptr(new AnimationEventsVector);
2597 AnimationRegistrar::AnimationControllerMap copy =
2598 animation_registrar_->active_animation_controllers();
2599 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2600 iter != copy.end();
2601 ++iter)
[email protected]3d9f7432013-04-06 00:35:182602 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272603
2604 if (!events->empty()) {
2605 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2606 last_animation_time_);
2607 }
[email protected]131a0c22013-02-12 18:31:082608}
2609
[email protected]c1bb5af2013-03-13 19:06:272610base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2611 return base::TimeDelta::FromSeconds(1);
2612}
2613
[email protected]50644642013-06-20 13:58:552614void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352615 DCHECK(current);
[email protected]50644642013-06-20 13:58:552616 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352617 current->DidLoseOutputSurface();
2618 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552619 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352620 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552621 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352622 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552623 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352624}
2625
[email protected]b5174d712013-08-28 08:10:432626void LayerTreeHostImpl::SetOffscreenContextProvider(
2627 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2628 if (!offscreen_context_provider.get()) {
2629 offscreen_context_provider_ = NULL;
2630 return;
2631 }
2632
2633 if (!offscreen_context_provider->BindToCurrentThread()) {
2634 offscreen_context_provider_ = NULL;
2635 return;
2636 }
2637
2638 offscreen_context_provider_ = offscreen_context_provider;
2639}
2640
[email protected]c1bb5af2013-03-13 19:06:272641std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2642 std::string str;
2643 if (active_tree_->root_layer()) {
2644 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2645 base::JSONWriter::WriteWithOptions(
2646 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2647 }
2648 return str;
2649}
2650
[email protected]c1bb5af2013-03-13 19:06:272651int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362652 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272653}
2654
[email protected]c1bb5af2013-03-13 19:06:272655void LayerTreeHostImpl::SendManagedMemoryStats(
2656 size_t memory_visible_bytes,
2657 size_t memory_visible_and_nearby_bytes,
2658 size_t memory_use_bytes) {
2659 if (!renderer_)
2660 return;
2661
2662 // Round the numbers being sent up to the next 8MB, to throttle the rate
2663 // at which we spam the GPU process.
2664 static const size_t rounding_step = 8 * 1024 * 1024;
2665 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2666 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2667 rounding_step);
2668 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2669 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2670 last_sent_memory_visible_and_nearby_bytes_ ==
2671 memory_visible_and_nearby_bytes &&
2672 last_sent_memory_use_bytes_ == memory_use_bytes) {
2673 return;
2674 }
2675 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2676 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2677 last_sent_memory_use_bytes_ = memory_use_bytes;
2678
2679 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2680 last_sent_memory_visible_and_nearby_bytes_,
2681 last_sent_memory_use_bytes_);
2682}
2683
2684void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2685 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2686}
2687
2688void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2689 base::TimeTicks time) {
2690 if (!layer)
2691 return;
2692
2693 ScrollbarAnimationController* scrollbar_controller =
2694 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352695 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202696 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422697 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2698 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442699 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202700 }
[email protected]c1bb5af2013-03-13 19:06:272701
2702 for (size_t i = 0; i < layer->children().size(); ++i)
2703 AnimateScrollbarsRecursive(layer->children()[i], time);
2704}
2705
[email protected]21c9dee72013-06-15 01:20:052706void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202707 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052708 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202709}
2710
2711void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2712 base::TimeTicks time) {
2713 if (!layer)
2714 return;
2715
2716 ScrollbarAnimationController* scrollbar_controller =
2717 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352718 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2719 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202720 if (delay > base::TimeDelta())
2721 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352722 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442723 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202724 }
2725
2726 for (size_t i = 0; i < layer->children().size(); ++i)
2727 StartScrollbarAnimationRecursive(layer->children()[i], time);
2728}
2729
[email protected]c1bb5af2013-03-13 19:06:272730void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2731 if (!tile_manager_)
2732 return;
2733
[email protected]1bcced22013-09-24 13:51:192734 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272735 return;
[email protected]1bcced22013-09-24 13:51:192736 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082737 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272738}
2739
[email protected]8347d692013-05-17 23:22:382740void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552741 current_frame_timeticks_ = base::TimeTicks();
2742 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272743}
2744
[email protected]21c9dee72013-06-15 01:20:052745void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2746 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552747 if (ticks->is_null()) {
2748 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052749 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552750 *now = base::Time::Now();
2751 }
2752}
2753
2754base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2755 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2756 return current_frame_timeticks_;
2757}
2758
2759base::Time LayerTreeHostImpl::CurrentFrameTime() {
2760 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272761 return current_frame_time_;
2762}
2763
[email protected]21c9dee72013-06-15 01:20:052764base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462765 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052766}
2767
[email protected]34806722013-08-09 23:51:212768scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2769 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272770 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222771 if (this->pending_tree_)
2772 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272773 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212774 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272775 if (tile_manager_)
2776 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2777 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222778 if (pending_tree_)
2779 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212780 if (frame)
2781 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222782 return state.PassAs<base::Value>();
2783}
2784
2785scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222786 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2787 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472788 if (tile_manager_)
2789 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272790 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082791}
2792
[email protected]6e7fdeb2013-07-09 14:28:382793void LayerTreeHostImpl::SetDebugState(
2794 const LayerTreeDebugState& new_debug_state) {
2795 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2796 return;
2797 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272798 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242799
[email protected]6e7fdeb2013-07-09 14:28:382800 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122801 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382802 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022803}
2804
[email protected]741fba422013-09-20 03:34:142805void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2806 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572807 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572808
[email protected]efa48412013-09-05 15:30:162809 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142810 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162811 case UIResourceBitmap::CLAMP_TO_EDGE:
2812 wrap_mode = GL_CLAMP_TO_EDGE;
2813 break;
2814 case UIResourceBitmap::REPEAT:
2815 wrap_mode = GL_REPEAT;
2816 break;
2817 }
2818
[email protected]c9280762013-08-01 06:28:572819 // Allow for multiple creation requests with the same UIResourceId. The
2820 // previous resource is simply deleted.
2821 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2822 if (id)
2823 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122824
2825 ResourceFormat format = resource_provider_->best_texture_format();
2826 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2827 format = ETC1;
[email protected]c9280762013-08-01 06:28:572828 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142829 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162830 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142831 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122832 format);
[email protected]c9280762013-08-01 06:28:572833
[email protected]efbdb3a2013-10-04 00:35:132834 UIResourceData data;
2835 data.resource_id = id;
2836 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512837 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132838
2839 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032840
2841 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572842 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032843 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142844 gfx::Rect(bitmap.GetSize()),
2845 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572846 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482847 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572848}
2849
2850void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2851 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2852 if (id) {
2853 resource_provider_->DeleteResource(id);
2854 ui_resource_map_.erase(uid);
2855 }
[email protected]127bdc1a2013-09-11 01:44:482856 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572857}
2858
[email protected]127bdc1a2013-09-11 01:44:482859void LayerTreeHostImpl::EvictAllUIResources() {
2860 if (ui_resource_map_.empty())
2861 return;
2862
[email protected]5f4dc4f2013-09-05 14:58:212863 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2864 iter != ui_resource_map_.end();
2865 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482866 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132867 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212868 }
2869 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482870
2871 client_->SetNeedsCommitOnImplThread();
2872 client_->OnCanDrawStateChanged(CanDraw());
2873 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212874}
2875
[email protected]c9280762013-08-01 06:28:572876ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2877 UIResourceId uid) const {
2878 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2879 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132880 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572881 return 0;
2882}
2883
[email protected]709c9542013-10-26 01:43:512884bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2885 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2886 DCHECK(iter != ui_resource_map_.end());
2887 return iter->second.opaque;
2888}
2889
[email protected]127bdc1a2013-09-11 01:44:482890bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2891 return !evicted_ui_resources_.empty();
2892}
2893
2894void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2895 std::set<UIResourceId>::iterator found_in_evicted =
2896 evicted_ui_resources_.find(uid);
2897 if (found_in_evicted == evicted_ui_resources_.end())
2898 return;
2899 evicted_ui_resources_.erase(found_in_evicted);
2900 if (evicted_ui_resources_.empty())
2901 client_->OnCanDrawStateChanged(CanDraw());
2902}
2903
[email protected]5e5648a2013-11-18 00:39:332904void LayerTreeHostImpl::ScheduleMicroBenchmark(
2905 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2906 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2907}
2908
[email protected]6be422b2013-12-08 06:47:312909void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2910 swap_promise_monitor_.insert(monitor);
2911}
2912
2913void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
2914 swap_promise_monitor_.erase(monitor);
2915}
2916
2917void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
2918 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
2919 for (; it != swap_promise_monitor_.end(); it++)
2920 (*it)->OnSetNeedsRedrawOnImpl();
2921}
2922
[email protected]d3143c732012-10-05 19:17:592923} // namespace cc