blob: 6f267cc51b0417ac65f35299e15a4be158db4da9 [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]681ccff2013-03-18 06:13:5218#include "cc/base/math_util.h"
19#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3320#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2721#include "cc/debug/debug_rect_history.h"
22#include "cc/debug/frame_rate_counter.h"
23#include "cc/debug/overdraw_metrics.h"
24#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4325#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2226#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2127#include "cc/input/page_scale_animation.h"
28#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5229#include "cc/layers/append_quads_data.h"
30#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2831#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5232#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1733#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2834#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0235#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3036#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4537#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3038#include "cc/output/delegating_renderer.h"
39#include "cc/output/gl_renderer.h"
40#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5641#include "cc/quads/render_pass_draw_quad.h"
42#include "cc/quads/shared_quad_state.h"
43#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1244#include "cc/quads/texture_draw_quad.h"
[email protected]e12dd0e2013-03-18 08:24:4045#include "cc/resources/memory_history.h"
46#include "cc/resources/picture_layer_tiling.h"
47#include "cc/resources/prioritized_resource_manager.h"
[email protected]ea468c6c2013-09-10 08:25:1148#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5749#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3150#include "cc/scheduler/delay_based_time_source.h"
51#include "cc/scheduler/texture_uploader.h"
[email protected]556fd292013-03-18 08:03:0452#include "cc/trees/damage_tracker.h"
53#include "cc/trees/layer_tree_host.h"
54#include "cc/trees/layer_tree_host_common.h"
55#include "cc/trees/layer_tree_impl.h"
56#include "cc/trees/quad_culler.h"
57#include "cc/trees/single_thread_proxy.h"
58#include "cc/trees/tree_synchronizer.h"
[email protected]de2cf8c2013-10-25 19:46:4659#include "ui/gfx/frame_time.h"
[email protected]d455d552012-11-02 00:19:0660#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1361#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1462
[email protected]94f206c12012-08-25 00:09:1463namespace {
64
[email protected]c1bb5af2013-03-13 19:06:2765void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
66 if (visible) {
67 TRACE_EVENT_ASYNC_BEGIN1("webkit",
68 "LayerTreeHostImpl::SetVisible",
69 id,
70 "LayerTreeHostImpl",
71 id);
72 return;
73 }
[email protected]94f206c12012-08-25 00:09:1474
[email protected]c1bb5af2013-03-13 19:06:2775 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1476}
77
[email protected]b6eb8e332013-09-10 00:51:0178size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
[email protected]b4664142013-11-08 00:50:3179 // Software compositing should not use this value in production. Just use a
80 // default value when testing uploads with the software compositor.
81 if (!context_provider)
[email protected]b6eb8e332013-09-10 00:51:0182 return std::numeric_limits<size_t>::max();
[email protected]b4664142013-11-08 00:50:3183
84 // We want to make sure the default transfer buffer size is equal to the
85 // amount of data that can be uploaded by the compositor to avoid stalling
86 // the pipeline.
87 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
88 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
89 // Assuming a two frame deep pipeline between CPU and GPU and we are
90 // drawing 60 frames per second which would require us to draw one
91 // frame in 16 milliseconds.
92 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
93 return std::min(
94 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
95 kMaxTransferBufferUsageBytes);
96}
97
98size_t GetMaxRasterTasksUsageBytes(cc::ContextProvider* context_provider) {
99 // Transfer-buffer/raster-tasks limits are different but related. We make
100 // equal here, as this is ideal when using transfer buffers. When not using
101 // transfer buffers we should still limit raster to something similar, to
102 // preserve caching behavior (and limit memory waste when priorities change).
103 return GetMaxTransferBufferUsageBytes(context_provider);
[email protected]b6eb8e332013-09-10 00:51:01104}
105
[email protected]c1bb5af2013-03-13 19:06:27106} // namespace
[email protected]94f206c12012-08-25 00:09:14107
[email protected]9c88e562012-09-14 22:21:30108namespace cc {
[email protected]94f206c12012-08-25 00:09:14109
[email protected]96baf3e2012-10-22 23:09:55110class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27111 public:
112 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
113 LayerTreeHostImpl* layer_tree_host_impl,
114 scoped_refptr<DelayBasedTimeSource> time_source) {
115 return make_scoped_ptr(
116 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
117 time_source));
118 }
119 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26120 time_source_->SetClient(NULL);
121 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27122 }
123
[email protected]6d0e69d2013-03-20 14:53:26124 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27125 // In single threaded mode we attempt to simulate changing the current
126 // thread by maintaining a fake thread id. When we switch from one
127 // thread to another, we construct DebugScopedSetXXXThread objects that
128 // update the thread id. This lets DCHECKS that ensure we're on the
129 // right thread to work correctly in single threaded mode. The problem
130 // here is that the timer tasks are run via the message loop, and when
131 // they run, we've had no chance to construct a DebugScopedSetXXXThread
132 // object. The result is that we report that we're running on the main
133 // thread. In multi-threaded mode, this timer is run on the compositor
134 // thread, so to keep this consistent in single-threaded mode, we'll
135 // construct a DebugScopedSetImplThread object. There is no need to do
136 // this in multi-threaded mode since the real thread id's will be
137 // correct. In fact, setting fake thread id's interferes with the real
138 // thread id's and causes breakage.
139 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
140 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
141 set_impl_thread.reset(
142 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14143 }
144
[email protected]94bf75c2013-06-12 13:20:04145 // TODO(enne): This should probably happen post-animate.
146 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38147 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
148 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04149 }
150
[email protected]fb7425a2013-04-22 16:28:55151 layer_tree_host_impl_->Animate(
152 layer_tree_host_impl_->CurrentFrameTimeTicks(),
153 layer_tree_host_impl_->CurrentFrameTime());
[email protected]3d9f7432013-04-06 00:35:18154 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
155 bool start_ready_animations = true;
156 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38157 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27158 }
[email protected]373974232013-01-10 22:20:50159
[email protected]c1bb5af2013-03-13 19:06:27160 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26161 if (active != time_source_->Active())
162 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27163 }
[email protected]94f206c12012-08-25 00:09:14164
[email protected]d9fce6722013-08-30 01:10:01165 bool Active() const { return time_source_->Active(); }
166
[email protected]c1bb5af2013-03-13 19:06:27167 private:
168 LayerTreeHostImplTimeSourceAdapter(
169 LayerTreeHostImpl* layer_tree_host_impl,
170 scoped_refptr<DelayBasedTimeSource> time_source)
171 : layer_tree_host_impl_(layer_tree_host_impl),
172 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26173 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27174 }
[email protected]94f206c12012-08-25 00:09:14175
[email protected]c1bb5af2013-03-13 19:06:27176 LayerTreeHostImpl* layer_tree_host_impl_;
177 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14178
[email protected]c1bb5af2013-03-13 19:06:27179 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14180};
181
[email protected]96baf3e2012-10-22 23:09:55182LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20183 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27184
185LayerTreeHostImpl::FrameData::~FrameData() {}
186
187scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
188 const LayerTreeSettings& settings,
189 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43190 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57191 RenderingStatsInstrumentation* rendering_stats_instrumentation,
192 SharedBitmapManager* manager) {
193 return make_scoped_ptr(new LayerTreeHostImpl(
194 settings, client, proxy, rendering_stats_instrumentation, manager));
[email protected]493067512012-09-19 23:34:10195}
196
[email protected]372bad5f2013-03-21 16:38:43197LayerTreeHostImpl::LayerTreeHostImpl(
198 const LayerTreeSettings& settings,
199 LayerTreeHostImplClient* client,
200 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57201 RenderingStatsInstrumentation* rendering_stats_instrumentation,
202 SharedBitmapManager* manager)
[email protected]c1bb5af2013-03-13 19:06:27203 : client_(client),
204 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37205 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27206 did_lock_scrolling_layer_(false),
207 should_bubble_scrolls_(false),
[email protected]d41d7cb92013-10-15 16:08:09208 last_scroll_did_bubble_(false),
[email protected]c1bb5af2013-03-13 19:06:27209 wheel_scrolling_(false),
[email protected]f620b0e72013-10-01 21:38:24210 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08211 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47212 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27213 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27214 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07215 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34216 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29217 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07218 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27219 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13220 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36221 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29222 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27223 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02224 debug_rect_history_(DebugRectHistory::Create()),
[email protected]ea468c6c2013-09-10 08:25:11225 texture_mailbox_deleter_(new TextureMailboxDeleter),
[email protected]d7626ffd2013-03-29 00:17:42226 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27227 last_sent_memory_visible_bytes_(0),
228 last_sent_memory_visible_and_nearby_bytes_(0),
229 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55230 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32231 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12232 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32233 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28234 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17235 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43236 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19237 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33238 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57239 need_to_update_visible_tiles_before_draw_(false),
240 shared_bitmap_manager_(manager) {
[email protected]c1bb5af2013-03-13 19:06:27241 DCHECK(proxy_->IsImplThread());
242 DidVisibilityChange(this, visible_);
243
[email protected]8e0176d2013-03-21 03:14:52244 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27245
[email protected]8e0176d2013-03-21 03:14:52246 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27247 top_controls_manager_ =
248 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52249 settings.top_controls_height,
250 settings.top_controls_show_threshold,
251 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27252 }
253
[email protected]8e0176d2013-03-21 03:14:52254 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27255
256 // LTHI always has an active tree.
257 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51258 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
259 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10260}
261
[email protected]c1bb5af2013-03-13 19:06:27262LayerTreeHostImpl::~LayerTreeHostImpl() {
263 DCHECK(proxy_->IsImplThread());
264 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51265 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
266 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27267
[email protected]200a9c062013-05-20 04:34:37268 if (input_handler_client_) {
269 input_handler_client_->WillShutdown();
270 input_handler_client_ = NULL;
271 }
272
[email protected]75deb742013-06-24 20:19:18273 // The layer trees must be destroyed before the layer tree host. We've
274 // made a contract with our animation controllers that the registrar
275 // will outlive them, and we must make good.
276 recycle_tree_.reset();
277 pending_tree_.reset();
278 active_tree_.reset();
[email protected]94f206c12012-08-25 00:09:14279}
280
[email protected]c1bb5af2013-03-13 19:06:27281void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29282
[email protected]c1bb5af2013-03-13 19:06:27283void LayerTreeHostImpl::CommitComplete() {
284 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08285
[email protected]8e0176d2013-03-21 03:14:52286 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25287 // Impl-side painting needs an update immediately post-commit to have the
288 // opportunity to create tilings. Other paths can call UpdateDrawProperties
289 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50290 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27291 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04292 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26293 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08294 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38295 NotifyReadyToActivate();
296 else
297 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27298 } else {
299 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01300 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
301 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27302 }
[email protected]3ba4cae2013-01-16 03:58:38303
[email protected]c1bb5af2013-03-13 19:06:27304 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33305
306 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14307}
308
[email protected]6133cc232013-07-30 18:47:07309bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27310 // Note: If you are changing this function or any other function that might
311 // affect the result of CanDraw, make sure to call
312 // client_->OnCanDrawStateChanged in the proper places and update the
313 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14314
[email protected]6133cc232013-07-30 18:47:07315 if (!renderer_) {
316 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
317 TRACE_EVENT_SCOPE_THREAD);
318 return false;
319 }
320
321 // Must have an OutputSurface if |renderer_| is not NULL.
322 DCHECK(output_surface_);
323
324 // TODO(boliu): Make draws without root_layer work and move this below
325 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27326 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42327 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
328 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22329 return false;
[email protected]c1bb5af2013-03-13 19:06:27330 }
[email protected]6133cc232013-07-30 18:47:07331
332 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
333 return true;
334
[email protected]54af03522013-09-05 00:43:28335 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42336 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
337 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27338 return false;
339 }
340 if (active_tree_->ViewportSizeInvalid()) {
341 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42342 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
343 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27344 return false;
345 }
[email protected]c1bb5af2013-03-13 19:06:27346 if (active_tree_->ContentsTexturesPurged()) {
347 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42348 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
349 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27350 return false;
351 }
[email protected]127bdc1a2013-09-11 01:44:48352 if (EvictedUIResourcesExist()) {
353 TRACE_EVENT_INSTANT0(
354 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
355 TRACE_EVENT_SCOPE_THREAD);
356 return false;
357 }
[email protected]c1bb5af2013-03-13 19:06:27358 return true;
[email protected]2f1acc262012-11-16 21:42:22359}
360
[email protected]c1bb5af2013-03-13 19:06:27361void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time,
362 base::Time wall_clock_time) {
[email protected]200a9c062013-05-20 04:34:37363 if (input_handler_client_)
364 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27365 AnimatePageScale(monotonic_time);
366 AnimateLayers(monotonic_time, wall_clock_time);
367 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37368 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14369}
370
[email protected]c1bb5af2013-03-13 19:06:27371void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26372 if (!tile_manager_)
373 return;
[email protected]c48536a52013-09-14 00:02:08374 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26375 return;
[email protected]54af03522013-09-05 00:43:28376 if (!device_viewport_valid_for_tile_management_)
377 return;
378
[email protected]c48536a52013-09-14 00:02:08379 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19380 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27381
382 size_t memory_required_bytes;
383 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03384 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27385 size_t memory_used_bytes;
386 tile_manager_->GetMemoryStats(&memory_required_bytes,
387 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03388 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27389 &memory_used_bytes);
390 SendManagedMemoryStats(memory_required_bytes,
391 memory_nice_to_have_bytes,
392 memory_used_bytes);
[email protected]f57bbc02012-11-21 07:02:15393}
394
[email protected]c1bb5af2013-03-13 19:06:27395void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset,
396 bool anchor_point,
397 float page_scale,
[email protected]c1bb5af2013-03-13 19:06:27398 base::TimeDelta duration) {
399 if (!RootScrollLayer())
400 return;
401
402 gfx::Vector2dF scroll_total =
[email protected]1960a712013-04-30 17:06:47403 RootScrollLayer()->scroll_offset() + RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:27404 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28405 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27406
[email protected]85d136f782013-04-26 22:04:40407 // Easing constants experimentally determined.
408 scoped_ptr<TimingFunction> timing_function =
409 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
410
[email protected]c1bb5af2013-03-13 19:06:27411 page_scale_animation_ =
412 PageScaleAnimation::Create(scroll_total,
413 active_tree_->total_page_scale_factor(),
414 viewport_size,
415 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40416 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27417
418 if (anchor_point) {
419 gfx::Vector2dF anchor(target_offset);
420 page_scale_animation_->ZoomWithAnchor(anchor,
421 page_scale,
422 duration.InSecondsF());
423 } else {
424 gfx::Vector2dF scaled_target_offset = target_offset;
425 page_scale_animation_->ZoomTo(scaled_target_offset,
426 page_scale,
427 duration.InSecondsF());
428 }
429
430 client_->SetNeedsRedrawOnImplThread();
431 client_->SetNeedsCommitOnImplThread();
432 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15433}
434
[email protected]c1bb5af2013-03-13 19:06:27435void LayerTreeHostImpl::ScheduleAnimation() {
436 client_->SetNeedsRedrawOnImplThread();
[email protected]f57bbc02012-11-21 07:02:15437}
438
[email protected]c1bb5af2013-03-13 19:06:27439bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07440 if (!settings_.touch_hit_testing)
441 return true;
[email protected]c1bb5af2013-03-13 19:06:27442 if (!EnsureRenderSurfaceLayerList())
443 return false;
[email protected]f57bbc02012-11-21 07:02:15444
[email protected]c1bb5af2013-03-13 19:06:27445 gfx::PointF device_viewport_point =
446 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15447
[email protected]c974d5d2013-10-24 01:02:48448 LayerImpl* layer_impl =
449 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
450 device_viewport_point,
451 active_tree_->RenderSurfaceLayerList());
452 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27453}
454
[email protected]b76329d2013-06-11 04:15:08455void LayerTreeHostImpl::SetLatencyInfoForInputEvent(
456 const ui::LatencyInfo& latency_info) {
457 active_tree()->SetLatencyInfo(latency_info);
458}
459
[email protected]c1bb5af2013-03-13 19:06:27460void LayerTreeHostImpl::TrackDamageForAllSurfaces(
461 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28462 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27463 // For now, we use damage tracking to compute a global scissor. To do this, we
464 // must compute all damage tracking before drawing anything, so that we know
465 // the root damage rect. The root damage rect is then used to scissor each
466 // surface.
467
468 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06469 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27470 --surface_index) {
471 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
472 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
473 DCHECK(render_surface);
474 render_surface->damage_tracker()->UpdateDamageTrackingState(
475 render_surface->layer_list(),
476 render_surface_layer->id(),
477 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
478 render_surface->content_rect(),
479 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48480 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27481 }
482}
483
[email protected]34806722013-08-09 23:51:21484scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
485 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
486 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
487 value->SetBoolean("has_no_damage", has_no_damage);
488
489 // Quad data can be quite large, so only dump render passes if we select
490 // cc.debug.quads.
491 bool quads_enabled;
492 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
493 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
494 if (quads_enabled) {
495 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
496 for (size_t i = 0; i < render_passes.size(); ++i)
497 render_pass_list->Append(render_passes[i]->AsValue().release());
498 value->Set("render_passes", render_pass_list.release());
499 }
500 return value.PassAs<base::Value>();
501}
502
[email protected]c1bb5af2013-03-13 19:06:27503void LayerTreeHostImpl::FrameData::AppendRenderPass(
504 scoped_ptr<RenderPass> render_pass) {
505 render_passes_by_id[render_pass->id] = render_pass.get();
506 render_passes.push_back(render_pass.Pass());
507}
508
[email protected]ffbb2212013-06-02 23:47:59509static DrawMode GetDrawMode(OutputSurface* output_surface) {
510 if (output_surface->ForcedDrawToSoftwareDevice()) {
511 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09512 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59513 return DRAW_MODE_HARDWARE;
514 } else {
[email protected]a7f35682013-10-22 23:05:57515 DCHECK_EQ(!output_surface->software_device(),
516 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59517 return DRAW_MODE_SOFTWARE;
518 }
519}
520
[email protected]c1bb5af2013-03-13 19:06:27521static void AppendQuadsForLayer(RenderPass* target_render_pass,
522 LayerImpl* layer,
523 const OcclusionTrackerImpl& occlusion_tracker,
524 AppendQuadsData* append_quads_data) {
525 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49526 QuadCuller quad_culler(&target_render_pass->quad_list,
527 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27528 layer,
529 occlusion_tracker,
530 layer->ShowDebugBorders(),
531 for_surface);
532 layer->AppendQuads(&quad_culler, append_quads_data);
533}
534
535static void AppendQuadsForRenderSurfaceLayer(
536 RenderPass* target_render_pass,
537 LayerImpl* layer,
538 const RenderPass* contributing_render_pass,
539 const OcclusionTrackerImpl& occlusion_tracker,
540 AppendQuadsData* append_quads_data) {
541 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49542 QuadCuller quad_culler(&target_render_pass->quad_list,
543 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27544 layer,
545 occlusion_tracker,
546 layer->ShowDebugBorders(),
547 for_surface);
548
549 bool is_replica = false;
550 layer->render_surface()->AppendQuads(&quad_culler,
551 append_quads_data,
552 is_replica,
553 contributing_render_pass->id);
554
555 // Add replica after the surface so that it appears below the surface.
556 if (layer->has_replica()) {
557 is_replica = true;
558 layer->render_surface()->AppendQuads(&quad_culler,
559 append_quads_data,
560 is_replica,
561 contributing_render_pass->id);
562 }
563}
564
565static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08566 ResourceProvider::ResourceId overhang_resource_id,
567 gfx::SizeF overhang_resource_scaled_size,
568 gfx::Rect root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27569 RenderPass* target_render_pass,
570 LayerImpl* root_layer,
571 SkColor screen_background_color,
572 const OcclusionTrackerImpl& occlusion_tracker) {
573 if (!root_layer || !SkColorGetA(screen_background_color))
574 return;
575
576 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
577 if (fill_region.IsEmpty())
578 return;
579
[email protected]3744e27b2013-11-06 21:44:08580 // Divide the fill region into the part to be filled with the overhang
581 // resource and the part to be filled with the background color.
582 Region screen_background_color_region = fill_region;
583 Region overhang_region;
584 if (overhang_resource_id) {
585 overhang_region = fill_region;
586 overhang_region.Subtract(root_scroll_layer_rect);
587 screen_background_color_region.Intersect(root_scroll_layer_rect);
588 }
589
[email protected]c1bb5af2013-03-13 19:06:27590 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49591 QuadCuller quad_culler(&target_render_pass->quad_list,
592 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27593 root_layer,
594 occlusion_tracker,
595 root_layer->ShowDebugBorders(),
596 for_surface);
597
598 // Manually create the quad state for the gutter quads, as the root layer
599 // doesn't have any bounds and so can't generate this itself.
600 // TODO(danakj): Make the gutter quads generated by the solid color layer
601 // (make it smarter about generating quads to fill unoccluded areas).
602
603 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
604 float opacity = 1.f;
605 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49606 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]c1bb5af2013-03-13 19:06:27607 shared_quad_state->SetAll(root_layer->draw_transform(),
608 root_target_rect.size(),
609 root_target_rect,
610 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01611 false,
[email protected]f57bbc02012-11-21 07:02:15612 opacity);
613
[email protected]c1bb5af2013-03-13 19:06:27614 AppendQuadsData append_quads_data;
[email protected]bda41962013-01-07 18:46:17615
[email protected]c1bb5af2013-03-13 19:06:27616 gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization);
617 bool did_invert = root_layer->screen_space_transform().GetInverse(
618 &transform_to_layer_space);
619 DCHECK(did_invert);
[email protected]3744e27b2013-11-06 21:44:08620 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27621 fill_rects.has_rect();
622 fill_rects.next()) {
623 // The root layer transform is composed of translations and scales only,
624 // no perspective, so mapping is sufficient (as opposed to projecting).
625 gfx::Rect layer_rect =
[email protected]fa816c62013-03-18 04:24:21626 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
[email protected]3744e27b2013-11-06 21:44:08627 // Skip the quad culler and just append the quads directly to avoid
628 // occlusion checks.
629 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
630 quad->SetNew(
631 shared_quad_state, layer_rect, screen_background_color, false);
632 quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data);
633 }
634 for (Region::Iterator fill_rects(overhang_region);
635 fill_rects.has_rect();
636 fill_rects.next()) {
637 DCHECK(overhang_resource_id);
638 gfx::Rect layer_rect =
639 MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect());
640 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
641 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
642 tex_quad->SetNew(
643 shared_quad_state,
644 layer_rect,
645 layer_rect,
646 overhang_resource_id,
647 false,
648 gfx::PointF(layer_rect.x() / overhang_resource_scaled_size.width(),
649 layer_rect.y() / overhang_resource_scaled_size.height()),
650 gfx::PointF(layer_rect.right() /
651 overhang_resource_scaled_size.width(),
652 layer_rect.bottom() /
653 overhang_resource_scaled_size.height()),
654 screen_background_color,
655 vertex_opacity,
656 false);
657 quad_culler.Append(tex_quad.PassAs<DrawQuad>(), &append_quads_data);
[email protected]c1bb5af2013-03-13 19:06:27658 }
[email protected]467b3612012-08-28 07:41:16659}
660
[email protected]c1bb5af2013-03-13 19:06:27661bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) {
662 DCHECK(frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:14663
[email protected]c1bb5af2013-03-13 19:06:27664 if (!CanDraw() || !active_tree_->root_layer())
665 return false;
[email protected]2d692992012-12-19 01:19:32666
[email protected]c1bb5af2013-03-13 19:06:27667 TrackDamageForAllSurfaces(active_tree_->root_layer(),
668 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14669
[email protected]e0341352013-04-06 05:01:20670 // If the root render surface has no visible damage, then don't generate a
671 // frame at all.
672 RenderSurfaceImpl* root_surface =
673 active_tree_->root_layer()->render_surface();
674 bool root_surface_has_no_visible_damage =
675 !root_surface->damage_tracker()->current_damage_rect().Intersects(
676 root_surface->content_rect());
677 bool root_surface_has_contributing_layers =
678 !root_surface->layer_list().empty();
679 if (root_surface_has_contributing_layers &&
680 root_surface_has_no_visible_damage) {
681 TRACE_EVENT0("cc",
682 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
683 frame->has_no_damage = true;
[email protected]30fe19ff2013-07-04 00:54:45684 // A copy request should cause damage, so we should not have any copy
685 // requests in this case.
686 DCHECK_EQ(0u, active_tree_->LayersWithCopyOutputRequest().size());
[email protected]6133cc232013-07-30 18:47:07687 DCHECK(!output_surface_->capabilities()
688 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:20689 return true;
690 }
691
[email protected]c1bb5af2013-03-13 19:06:27692 TRACE_EVENT1("cc",
693 "LayerTreeHostImpl::CalculateRenderPasses",
694 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06695 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14696
[email protected]c1bb5af2013-03-13 19:06:27697 // Create the render passes in dependency order.
698 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06699 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27700 --surface_index) {
701 LayerImpl* render_surface_layer =
702 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45703 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
704
705 bool should_draw_into_render_pass =
706 render_surface_layer->parent() == NULL ||
707 render_surface->contributes_to_drawn_surface() ||
708 render_surface_layer->HasCopyRequest();
709 if (should_draw_into_render_pass)
710 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27711 }
[email protected]94f206c12012-08-25 00:09:14712
[email protected]c1bb5af2013-03-13 19:06:27713 bool record_metrics_for_frame =
[email protected]8e0176d2013-03-21 03:14:52714 settings_.show_overdraw_in_tracing &&
[email protected]c1bb5af2013-03-13 19:06:27715 base::debug::TraceLog::GetInstance() &&
716 base::debug::TraceLog::GetInstance()->IsEnabled();
717 OcclusionTrackerImpl occlusion_tracker(
718 active_tree_->root_layer()->render_surface()->content_rect(),
719 record_metrics_for_frame);
720 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52721 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14722
[email protected]846f455b2013-03-18 19:07:41723 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27724 occlusion_tracker.set_occluding_screen_space_rects_container(
725 &frame->occluding_screen_space_rects);
726 }
[email protected]846f455b2013-03-18 19:07:41727 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27728 occlusion_tracker.set_non_occluding_screen_space_rects_container(
729 &frame->non_occluding_screen_space_rects);
730 }
[email protected]94f206c12012-08-25 00:09:14731
[email protected]c1bb5af2013-03-13 19:06:27732 // Add quads to the Render passes in FrontToBack order to allow for testing
733 // occlusion and performing culling during the tree walk.
734 typedef LayerIterator<LayerImpl,
[email protected]50761e92013-03-29 20:51:28735 LayerImplList,
[email protected]c1bb5af2013-03-13 19:06:27736 RenderSurfaceImpl,
737 LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14738
[email protected]c1bb5af2013-03-13 19:06:27739 // Typically when we are missing a texture and use a checkerboard quad, we
740 // still draw the frame. However when the layer being checkerboarded is moving
741 // due to an impl-animation, we drop the frame to avoid flashing due to the
742 // texture suddenly appearing in the future.
743 bool draw_frame = true;
[email protected]18a70192013-04-26 16:18:25744 // When we have a copy request for a layer, we need to draw no matter
745 // what, as the layer may disappear after this frame.
746 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14747
[email protected]372bad5f2013-03-21 16:38:43748 int layers_drawn = 0;
749
[email protected]ffbb2212013-06-02 23:47:59750 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
751
[email protected]c1bb5af2013-03-13 19:06:27752 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09753 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27754 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09755 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27756 it != end;
757 ++it) {
758 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09759 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27760 RenderPass* target_render_pass =
761 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14762
[email protected]e47b0a0a2013-11-18 23:26:22763 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14764
[email protected]30fe19ff2013-07-04 00:54:45765 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30766
[email protected]18a70192013-04-26 16:18:25767 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36768 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25769 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11770 it->TakeCopyRequestsAndTransformToTarget(
771 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25772 }
[email protected]30fe19ff2013-07-04 00:54:45773 } else if (it.represents_contributing_render_surface() &&
774 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27775 RenderPass::Id contributing_render_pass_id =
776 it->render_surface()->RenderPassId();
777 RenderPass* contributing_render_pass =
778 frame->render_passes_by_id[contributing_render_pass_id];
779 AppendQuadsForRenderSurfaceLayer(target_render_pass,
780 *it,
781 contributing_render_pass,
782 occlusion_tracker,
783 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45784 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09785 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27786 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15787 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27788 it->render_target(),
789 it->visible_content_rect(),
790 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45791 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15792 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27793 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59794
[email protected]c1bb5af2013-03-13 19:06:27795 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57796
[email protected]c1bb5af2013-03-13 19:06:27797 if (it->HasContributingDelegatedRenderPasses()) {
798 RenderPass::Id contributing_render_pass_id =
799 it->FirstContributingRenderPassId();
800 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
801 frame->render_passes_by_id.end()) {
802 RenderPass* render_pass =
803 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14804
[email protected]c1bb5af2013-03-13 19:06:27805 AppendQuadsData append_quads_data(render_pass->id);
806 AppendQuadsForLayer(render_pass,
807 *it,
808 occlusion_tracker,
809 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57810
[email protected]c1bb5af2013-03-13 19:06:27811 contributing_render_pass_id =
812 it->NextContributingRenderPassId(contributing_render_pass_id);
813 }
[email protected]94f206c12012-08-25 00:09:14814 }
815
[email protected]c1bb5af2013-03-13 19:06:27816 AppendQuadsForLayer(target_render_pass,
817 *it,
818 occlusion_tracker,
819 &append_quads_data);
820 }
[email protected]89228202012-08-29 03:20:30821
[email protected]372bad5f2013-03-21 16:38:43822 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14823 }
824
[email protected]e1e768f2013-03-26 08:48:09825 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27826 bool layer_has_animating_transform =
827 it->screen_space_transform_is_animating() ||
828 it->draw_transform_is_animating();
829 if (layer_has_animating_transform)
830 draw_frame = false;
831 }
832
[email protected]e1e768f2013-03-26 08:48:09833 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27834 frame->contains_incomplete_tile = true;
835
836 occlusion_tracker.LeaveLayer(it);
837 }
838
[email protected]6133cc232013-07-30 18:47:07839 if (have_copy_request ||
840 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25841 draw_frame = true;
842
[email protected]1d993172012-10-18 18:15:04843#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27844 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
845 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
846 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
847 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
848 != frame->render_passes_by_id.end());
849 }
[email protected]94f206c12012-08-25 00:09:14850#endif
[email protected]c1bb5af2013-03-13 19:06:27851 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14852
[email protected]c1bb5af2013-03-13 19:06:27853 if (!active_tree_->has_transparent_background()) {
854 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08855 AppendQuadsToFillScreen(
856 ResourceIdForUIResource(overhang_ui_resource_id_),
857 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
858 active_tree_->RootScrollLayerDeviceViewportBounds(),
859 frame->render_passes.back(),
860 active_tree_->root_layer(),
861 active_tree_->background_color(),
862 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27863 }
[email protected]94f206c12012-08-25 00:09:14864
[email protected]c1bb5af2013-03-13 19:06:27865 if (draw_frame)
866 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25867 else
868 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14869
[email protected]c1bb5af2013-03-13 19:06:27870 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48871 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14872
[email protected]30fe19ff2013-07-04 00:54:45873 // Any copy requests left in the tree are not going to get serviced, and
874 // should be aborted.
875 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
876 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
877 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
878 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
879 }
880 for (size_t i = 0; i < requests_to_abort.size(); ++i)
881 requests_to_abort[i]->SendEmptyResult();
882
[email protected]e0341352013-04-06 05:01:20883 // If we're making a frame to draw, it better have at least one render pass.
884 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48885
886 // Should only have one render pass in resourceless software mode.
887 if (output_surface_->ForcedDrawToSoftwareDevice())
888 DCHECK_EQ(1u, frame->render_passes.size());
889
[email protected]c1bb5af2013-03-13 19:06:27890 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14891}
892
[email protected]200a9c062013-05-20 04:34:37893void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
894 if (input_handler_client_)
895 input_handler_client_->MainThreadHasStoppedFlinging();
896}
897
[email protected]3d9f7432013-04-06 00:35:18898void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
899 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15900 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01901 if (should_background_tick)
902 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15903
[email protected]3d9f7432013-04-06 00:35:18904 bool enabled = should_background_tick &&
905 !animation_registrar_->active_animation_controllers().empty();
906
[email protected]c1bb5af2013-03-13 19:06:27907 // Lazily create the time_source adapter so that we can vary the interval for
908 // testing.
909 if (!time_source_client_adapter_) {
910 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
911 this,
[email protected]810d40b72013-06-20 18:26:15912 DelayBasedTimeSource::Create(
913 LowFrequencyAnimationInterval(),
914 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
915 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27916 }
[email protected]94f206c12012-08-25 00:09:14917
[email protected]c1bb5af2013-03-13 19:06:27918 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14919}
920
[email protected]b9d4a362013-04-23 05:36:27921void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02922 viewport_damage_rect_.Union(damage_rect);
923}
924
[email protected]c1bb5af2013-03-13 19:06:27925static inline RenderPass* FindRenderPassById(
926 RenderPass::Id render_pass_id,
927 const LayerTreeHostImpl::FrameData& frame) {
928 RenderPassIdHashMap::const_iterator it =
929 frame.render_passes_by_id.find(render_pass_id);
930 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14931}
932
[email protected]c1bb5af2013-03-13 19:06:27933static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
934 LayerTreeHostImpl::FrameData* frame) {
935 RenderPass* remove_render_pass =
936 FindRenderPassById(remove_render_pass_id, *frame);
937 // The pass was already removed by another quad - probably the original, and
938 // we are the replica.
939 if (!remove_render_pass)
940 return;
941 RenderPassList& render_passes = frame->render_passes;
942 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
943 render_passes.end(),
944 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14945
[email protected]c1bb5af2013-03-13 19:06:27946 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14947
[email protected]c1bb5af2013-03-13 19:06:27948 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
949 frame->render_passes.erase(to_remove);
950 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14951
[email protected]c1bb5af2013-03-13 19:06:27952 // Now follow up for all RenderPass quads and remove their RenderPasses
953 // recursively.
954 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29955 QuadList::ConstBackToFrontIterator quad_list_iterator =
956 quad_list.BackToFrontBegin();
957 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27958 ++quad_list_iterator) {
959 DrawQuad* current_quad = (*quad_list_iterator);
960 if (current_quad->material != DrawQuad::RENDER_PASS)
961 continue;
[email protected]94f206c12012-08-25 00:09:14962
[email protected]c1bb5af2013-03-13 19:06:27963 RenderPass::Id next_remove_render_pass_id =
964 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
965 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
966 }
[email protected]94f206c12012-08-25 00:09:14967}
968
[email protected]c1bb5af2013-03-13 19:06:27969bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
970 const RenderPassDrawQuad& quad, const FrameData& frame) const {
971 const RenderPass* render_pass =
972 FindRenderPassById(quad.render_pass_id, frame);
973 if (!render_pass)
974 return false;
[email protected]94f206c12012-08-25 00:09:14975
[email protected]c1bb5af2013-03-13 19:06:27976 // If any quad or RenderPass draws into this RenderPass, then keep it.
977 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29978 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
979 quad_list.BackToFrontBegin();
980 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27981 ++quad_list_iterator) {
982 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14983
[email protected]c1bb5af2013-03-13 19:06:27984 if (current_quad->material != DrawQuad::RENDER_PASS)
985 return false;
[email protected]94f206c12012-08-25 00:09:14986
[email protected]c1bb5af2013-03-13 19:06:27987 const RenderPass* contributing_pass = FindRenderPassById(
988 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
989 if (contributing_pass)
990 return false;
991 }
992 return true;
[email protected]94f206c12012-08-25 00:09:14993}
994
995// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27996template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:27997 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
998 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:14999
1000// static
[email protected]c1bb5af2013-03-13 19:06:271001template <typename RenderPassCuller>
1002void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1003 FrameData* frame) {
1004 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1005 it != culler.RenderPassListEnd(frame->render_passes);
1006 it = culler.RenderPassListNext(it)) {
1007 const RenderPass* current_pass = frame->render_passes[it];
1008 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291009 QuadList::ConstBackToFrontIterator quad_list_iterator =
1010 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141011
[email protected]ed511b8d2013-03-25 03:29:291012 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271013 ++quad_list_iterator) {
1014 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141015
[email protected]c1bb5af2013-03-13 19:06:271016 if (current_quad->material != DrawQuad::RENDER_PASS)
1017 continue;
[email protected]94f206c12012-08-25 00:09:141018
[email protected]c1bb5af2013-03-13 19:06:271019 const RenderPassDrawQuad* render_pass_quad =
1020 RenderPassDrawQuad::MaterialCast(current_quad);
1021 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1022 continue;
[email protected]94f206c12012-08-25 00:09:141023
[email protected]c1bb5af2013-03-13 19:06:271024 // We are changing the vector in the middle of iteration. Because we
1025 // delete render passes that draw into the current pass, we are
1026 // guaranteed that any data from the iterator to the end will not
1027 // change. So, capture the iterator position from the end of the
1028 // list, and restore it after the change.
1029 size_t position_from_end = frame->render_passes.size() - it;
1030 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1031 it = frame->render_passes.size() - position_from_end;
1032 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141033 }
[email protected]c1bb5af2013-03-13 19:06:271034 }
[email protected]94f206c12012-08-25 00:09:141035}
1036
[email protected]e0341352013-04-06 05:01:201037bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
1038 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471039 TRACE_EVENT1("cc",
1040 "LayerTreeHostImpl::PrepareToDraw",
1041 "SourceFrameNumber",
1042 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141043
[email protected]425e5332013-09-21 04:10:031044 if (need_to_update_visible_tiles_before_draw_ &&
1045 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1046 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191047 }
[email protected]425e5332013-09-21 04:10:031048 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191049
[email protected]7d19dc342013-05-02 22:02:041050 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271051
[email protected]c1bb5af2013-03-13 19:06:271052 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1053 frame->render_passes.clear();
1054 frame->render_passes_by_id.clear();
1055 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201056 frame->contains_incomplete_tile = false;
1057 frame->has_no_damage = false;
1058
1059 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021060 device_viewport_damage_rect.Union(viewport_damage_rect_);
1061 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201062
1063 active_tree_->root_layer()->render_surface()->damage_tracker()->
1064 AddDamageNextUpdate(device_viewport_damage_rect);
1065 }
[email protected]94f206c12012-08-25 00:09:141066
[email protected]6133cc232013-07-30 18:47:071067 if (!CalculateRenderPasses(frame)) {
1068 DCHECK(!output_surface_->capabilities()
1069 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:271070 return false;
[email protected]6133cc232013-07-30 18:47:071071 }
[email protected]94f206c12012-08-25 00:09:141072
[email protected]c1bb5af2013-03-13 19:06:271073 // If we return true, then we expect DrawLayers() to be called before this
1074 // function is called again.
1075 return true;
[email protected]94f206c12012-08-25 00:09:141076}
1077
[email protected]0309ba5e02013-06-26 04:11:081078void LayerTreeHostImpl::EvictTexturesForTesting() {
1079 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1080}
1081
[email protected]4f48f6e2013-08-27 06:33:381082void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1083 NOTREACHED();
1084}
1085
[email protected]425e5332013-09-21 04:10:031086void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1087 DidInitializeVisibleTile();
1088}
1089
[email protected]c1bb5af2013-03-13 19:06:271090void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1091 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121092
[email protected]c1bb5af2013-03-13 19:06:271093 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351094 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351095 ManagedMemoryPolicy::PriorityCutoffToValue(
1096 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351097 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271098 if (evicted_resources) {
1099 active_tree_->SetContentsTexturesPurged();
1100 if (pending_tree_)
1101 pending_tree_->SetContentsTexturesPurged();
1102 client_->SetNeedsCommitOnImplThread();
1103 client_->OnCanDrawStateChanged(CanDraw());
1104 client_->RenewTreePriority();
1105 }
1106 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431107
[email protected]8be1a9bf2013-05-01 03:45:191108 UpdateTileManagerMemoryPolicy(policy);
1109}
1110
1111void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1112 const ManagedMemoryPolicy& policy) {
1113 if (!tile_manager_)
1114 return;
1115
[email protected]8be1a9bf2013-05-01 03:45:191116 // TODO(reveman): We should avoid keeping around unused resources if
1117 // possible. crbug.com/224475
[email protected]1bcced22013-09-24 13:51:191118 global_tile_state_.memory_limit_in_bytes =
1119 visible_ ?
[email protected]0b8ef2842013-11-01 09:53:351120 policy.bytes_limit_when_visible : 0;
[email protected]1bcced22013-09-24 13:51:191121 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
1122 (static_cast<int64>(global_tile_state_.memory_limit_in_bytes) *
[email protected]8be1a9bf2013-05-01 03:45:191123 settings_.max_unused_resource_memory_percentage) / 100);
[email protected]1bcced22013-09-24 13:51:191124 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191125 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1126 visible_ ?
1127 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351128 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191129 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081130
[email protected]c48536a52013-09-14 00:02:081131 DidModifyTilePriorities();
1132}
1133
1134void LayerTreeHostImpl::DidModifyTilePriorities() {
1135 DCHECK(settings_.impl_side_painting);
1136 // Mark priorities as dirty and schedule a ManageTiles().
1137 tile_priorities_dirty_ = true;
1138 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141139}
1140
[email protected]86126792013-03-16 20:07:541141void LayerTreeHostImpl::DidInitializeVisibleTile() {
1142 // TODO(reveman): Determine tiles that changed and only damage
1143 // what's necessary.
1144 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031145 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541146 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471147}
1148
[email protected]94bf75c2013-06-12 13:20:041149void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381150 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041151}
1152
[email protected]3f2ff112013-08-03 02:41:071153void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1154 SetManagedMemoryPolicy(policy, zero_budget_);
1155}
1156
[email protected]5f07afc2013-07-19 05:17:181157void LayerTreeHostImpl::SetTreeActivationCallback(
1158 const base::Closure& callback) {
1159 DCHECK(proxy_->IsImplThread());
1160 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261161 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181162}
1163
[email protected]c1bb5af2013-03-13 19:06:271164void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071165 const ManagedMemoryPolicy& policy, bool zero_budget) {
1166 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271167 return;
[email protected]61de5812012-11-08 07:03:441168
[email protected]3f2ff112013-08-03 02:41:071169 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1170
1171 cached_managed_memory_policy_ = policy;
1172 zero_budget_ = zero_budget;
1173 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1174
1175 if (old_policy == actual_policy)
1176 return;
1177
1178 if (!proxy_->HasImplThread()) {
1179 // In single-thread mode, this can be called on the main thread by
1180 // GLRenderer::OnMemoryAllocationChanged.
1181 DebugScopedSetImplThread impl_thread(proxy_);
1182 EnforceManagedMemoryPolicy(actual_policy);
1183 } else {
1184 DCHECK(proxy_->IsImplThread());
1185 EnforceManagedMemoryPolicy(actual_policy);
1186 }
[email protected]50644642013-06-20 13:58:551187
[email protected]d7626ffd2013-03-29 00:17:421188 // If there is already enough memory to draw everything imaginable and the
1189 // new memory limit does not change this, then do not re-commit. Don't bother
1190 // skipping commits if this is not visible (commits don't happen when not
1191 // visible, there will almost always be a commit when this becomes visible).
1192 bool needs_commit = true;
1193 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071194 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1195 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1196 actual_policy.priority_cutoff_when_visible ==
1197 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421198 needs_commit = false;
1199 }
1200
[email protected]d7626ffd2013-03-29 00:17:421201 if (needs_commit)
1202 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141203}
1204
[email protected]f224cc92013-06-06 23:23:321205void LayerTreeHostImpl::SetExternalDrawConstraints(
1206 const gfx::Transform& transform,
[email protected]54af03522013-09-05 00:43:281207 gfx::Rect viewport,
1208 gfx::Rect clip,
1209 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321210 external_transform_ = transform;
1211 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281212 external_clip_ = clip;
1213 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321214}
1215
[email protected]1cd9f5552013-04-26 04:22:031216void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1217 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1218}
1219
[email protected]daea3d42013-10-23 17:04:501220void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1221 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451222}
1223
[email protected]4d7e46a2013-11-08 05:33:401224void LayerTreeHostImpl::DidSwapBuffers() {
1225 client_->DidSwapBuffersOnImplThread();
1226}
1227
[email protected]a7335e0b2013-09-18 09:34:511228void LayerTreeHostImpl::OnSwapBuffersComplete() {
1229 client_->OnSwapBuffersCompleteOnImplThread();
1230}
1231
1232void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271233 // TODO(piman): We may need to do some validation on this ack before
1234 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511235 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291236 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161237}
1238
[email protected]c1bb5af2013-03-13 19:06:271239void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1240 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291241}
1242
[email protected]c1bb5af2013-03-13 19:06:271243CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1244 CompositorFrameMetadata metadata;
1245 metadata.device_scale_factor = device_scale_factor_;
1246 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1247 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1248 metadata.root_layer_size = active_tree_->ScrollableSize();
1249 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1250 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501251 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271252 if (top_controls_manager_) {
1253 metadata.location_bar_offset =
1254 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1255 metadata.location_bar_content_translation =
1256 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011257 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271258 }
[email protected]bf189f62012-12-18 03:42:111259
[email protected]c1bb5af2013-03-13 19:06:271260 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111261 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271262
[email protected]ffb2720f2013-03-15 19:18:371263 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271264
1265 return metadata;
[email protected]bf189f62012-12-18 03:42:111266}
1267
[email protected]6f50b8fa2013-07-01 19:51:491268static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1269 layer->DidBeginTracing();
1270}
[email protected]37349bc2013-06-04 01:31:521271
[email protected]f0c2a242013-03-15 19:34:521272void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1273 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351274 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271275 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201276
[email protected]3519b872013-07-30 07:17:501277 if (frame->has_no_damage) {
1278 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071279 DCHECK(!output_surface_->capabilities()
1280 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201281 return;
[email protected]3519b872013-07-30 07:17:501282 }
[email protected]e0341352013-04-06 05:01:201283
[email protected]c1bb5af2013-03-13 19:06:271284 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141285
[email protected]213fcfc2013-08-24 14:59:271286 fps_counter_->SaveTimeStamp(frame_begin_time,
1287 !output_surface_->context_provider());
[email protected]94f206c12012-08-25 00:09:141288
[email protected]c1e6cc062013-08-24 03:35:351289 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091290 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351291 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431292
[email protected]c1bb5af2013-03-13 19:06:271293 if (tile_manager_) {
1294 memory_history_->SaveEntry(
1295 tile_manager_->memory_stats_from_last_assign());
1296 }
[email protected]1191d9d2013-02-02 06:00:331297
[email protected]846f455b2013-03-18 19:07:411298 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021299 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271300 active_tree_->root_layer(),
1301 *frame->render_surface_layer_list,
1302 frame->occluding_screen_space_rects,
1303 frame->non_occluding_screen_space_rects,
1304 debug_state_);
1305 }
[email protected]94f206c12012-08-25 00:09:141306
[email protected]a848c102013-03-26 08:59:091307 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1308 const RenderingStats& stats =
1309 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181310 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091311 }
1312
[email protected]37349bc2013-06-04 01:31:521313 bool is_new_trace;
1314 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1315 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491316 if (pending_tree_) {
1317 LayerTreeHostCommon::CallFunctionForSubtree(
1318 pending_tree_->root_layer(),
1319 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1320 }
1321 LayerTreeHostCommon::CallFunctionForSubtree(
1322 active_tree_->root_layer(),
1323 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521324 }
1325
[email protected]9197dbcb2013-05-15 20:28:511326 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211327 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1328 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1329 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081330
[email protected]c1bb5af2013-03-13 19:06:271331 // Because the contents of the HUD depend on everything else in the frame, the
1332 // contents of its texture are updated as the last thing before the frame is
1333 // drawn.
[email protected]671f6672013-10-01 07:31:071334 if (active_tree_->hud_layer()) {
1335 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531336 active_tree_->hud_layer()->UpdateHudTexture(
1337 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071338 }
[email protected]94f206c12012-08-25 00:09:141339
[email protected]b09c1942013-05-10 00:06:371340 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321341 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261342 bool disable_picture_quad_image_filtering =
1343 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321344
[email protected]b09c1942013-05-10 00:06:371345 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321346 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
1347 temp_software_renderer->DrawFrame(
[email protected]251699b2013-10-09 00:21:261348 &frame->render_passes, NULL, device_scale_factor_, allow_partial_swap,
1349 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371350 } else {
[email protected]573e8e32013-09-09 17:24:321351 // We don't track damage on the HUD layer (it interacts with damage tracking
1352 // visualizations), so disable partial swaps to make the HUD layer display
1353 // properly.
1354 bool allow_partial_swap = !debug_state_.ShowHudRects();
1355
[email protected]b5174d712013-08-28 08:10:431356 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321357 offscreen_context_provider_.get(),
1358 device_scale_factor_,
[email protected]251699b2013-10-09 00:21:261359 allow_partial_swap,
1360 false);
[email protected]b09c1942013-05-10 00:06:371361 }
[email protected]c1bb5af2013-03-13 19:06:271362 // The render passes should be consumed by the renderer.
1363 DCHECK(frame->render_passes.empty());
1364 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141365
[email protected]c1bb5af2013-03-13 19:06:271366 // The next frame should start by assuming nothing has changed, and changes
1367 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321368 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1369 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541370 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271371 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1372 DidDrawDamagedArea();
1373 }
1374 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351375
[email protected]adbe30f2013-10-11 21:12:331376 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1377 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181378 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141379}
1380
[email protected]c1bb5af2013-03-13 19:06:271381void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1382 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1383 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521384
[email protected]c1bb5af2013-03-13 19:06:271385 // Once all layers have been drawn, pending texture uploads should no
1386 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151387 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141388}
1389
[email protected]c1bb5af2013-03-13 19:06:271390void LayerTreeHostImpl::FinishAllRendering() {
1391 if (renderer_)
1392 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141393}
1394
[email protected]c1bb5af2013-03-13 19:06:271395bool LayerTreeHostImpl::IsContextLost() {
1396 DCHECK(proxy_->IsImplThread());
1397 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141398}
1399
[email protected]c1bb5af2013-03-13 19:06:271400const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1401 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141402}
1403
[email protected]e0341352013-04-06 05:01:201404bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1405 if (frame.has_no_damage)
1406 return false;
[email protected]36e5ff12013-06-11 12:19:291407 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161408 active_tree_->ClearLatencyInfo();
1409 return true;
[email protected]94f206c12012-08-25 00:09:141410}
1411
[email protected]daea3d42013-10-23 17:04:501412void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451413 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501414 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451415}
1416
[email protected]54af03522013-09-05 00:43:281417gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341418 // Use the root container layer bounds if it clips to them, otherwise, the
1419 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441420 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341421 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441422 DCHECK(!top_controls_manager_);
1423 DCHECK_EQ(0, overdraw_bottom_height_);
1424 return container_layer->bounds();
1425 }
1426
[email protected]ffb2720f2013-03-15 19:18:371427 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281428 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371429
[email protected]ed511b8d2013-03-25 03:29:291430 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371431 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401432 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291433 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371434}
1435
[email protected]c1bb5af2013-03-13 19:06:271436void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011437 // TODO(jamesr): The renderer_ check is needed to make some of the
1438 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1439 // important) in production. We should adjust the test to not need this.
1440 if (renderer_)
1441 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141442}
1443
[email protected]c1bb5af2013-03-13 19:06:271444void LayerTreeHostImpl::Readback(void* pixels,
1445 gfx::Rect rect_in_device_viewport) {
1446 DCHECK(renderer_);
1447 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141448}
1449
[email protected]59adb112013-04-09 04:48:441450bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341451 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011452}
1453
[email protected]c1bb5af2013-03-13 19:06:271454LayerImpl* LayerTreeHostImpl::RootLayer() const {
1455 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081456}
1457
[email protected]c1bb5af2013-03-13 19:06:271458LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1459 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081460}
1461
[email protected]c1bb5af2013-03-13 19:06:271462LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1463 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081464}
1465
[email protected]251699b2013-10-09 00:21:261466bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1467 return CurrentlyScrollingLayer() ||
1468 (RootScrollLayer() && RootScrollLayer()->IsExternalFlingActive());
1469}
1470
[email protected]94f206c12012-08-25 00:09:141471// Content layers can be either directly scrollable or contained in an outer
1472// scrolling layer which applies the scroll transform. Given a content layer,
1473// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271474static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1475 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021476 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271477
1478 if (layer_impl->scrollable())
1479 return layer_impl;
1480
1481 if (layer_impl->DrawsContent() &&
1482 layer_impl->parent() &&
1483 layer_impl->parent()->scrollable())
1484 return layer_impl->parent();
1485
[email protected]bf1cfd9a2013-09-26 05:43:021486 return NULL;
[email protected]94f206c12012-08-25 00:09:141487}
1488
[email protected]c1bb5af2013-03-13 19:06:271489void LayerTreeHostImpl::CreatePendingTree() {
1490 CHECK(!pending_tree_);
1491 if (recycle_tree_)
1492 recycle_tree_.swap(pending_tree_);
1493 else
1494 pending_tree_ = LayerTreeImpl::create(this);
1495 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111496 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271497}
1498
[email protected]8bb6cfa2013-07-23 00:11:191499void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191500 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1501 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191502 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481503}
1504
[email protected]c1bb5af2013-03-13 19:06:271505void LayerTreeHostImpl::ActivatePendingTree() {
1506 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111507 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351508
[email protected]4f48f6e2013-08-27 06:33:381509 need_to_update_visible_tiles_before_draw_ = true;
1510
[email protected]1960a712013-04-30 17:06:471511 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271512 active_tree_->PushPersistedState(pending_tree_.get());
1513 if (pending_tree_->needs_full_tree_sync()) {
1514 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331515 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271516 active_tree_->DetachLayerTree(),
1517 active_tree_.get()));
1518 }
[email protected]b5651c22013-03-14 15:06:331519 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271520 active_tree_->root_layer());
1521 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511522
[email protected]c9280762013-08-01 06:28:571523 // Process any requests in the UI resource queue. The request queue is given
1524 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1525 // the swap.
1526 pending_tree_->ProcessUIResourceRequestQueue();
1527
[email protected]c1bb5af2013-03-13 19:06:271528 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511529
[email protected]c1bb5af2013-03-13 19:06:271530 // Now that we've synced everything from the pending tree to the active
1531 // tree, rename the pending tree the recycle tree so we can reuse it on the
1532 // next sync.
1533 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511534
[email protected]7d1b07e2013-10-01 17:31:301535 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471536 active_tree_->SetRootLayerScrollOffsetDelegate(
1537 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221538
[email protected]c1bb5af2013-03-13 19:06:271539 client_->OnCanDrawStateChanged(CanDraw());
[email protected]c1bb5af2013-03-13 19:06:271540 client_->SetNeedsRedrawOnImplThread();
1541 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241542
[email protected]a848c102013-03-26 08:59:091543 if (debug_state_.continuous_painting) {
1544 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431545 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091546 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1547 stats.main_stats.record_time +
1548 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271549 }
[email protected]2a61ad52013-05-13 14:01:291550
1551 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261552 if (!tree_activation_callback_.is_null())
1553 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011554
1555 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1556 DCHECK(active_tree_->root_layer());
[email protected]2e7ca422012-12-20 02:57:271557}
1558
[email protected]c1bb5af2013-03-13 19:06:271559void LayerTreeHostImpl::SetVisible(bool visible) {
1560 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141561
[email protected]c1bb5af2013-03-13 19:06:271562 if (visible_ == visible)
1563 return;
1564 visible_ = visible;
1565 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121566 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141567
[email protected]127bdc1a2013-09-11 01:44:481568 if (!visible_)
1569 EvictAllUIResources();
1570
[email protected]0fa42e02013-08-20 01:16:051571 // Evict tiles immediately if invisible since this tab may never get another
1572 // draw or timer tick.
1573 if (!visible_)
1574 ManageTiles();
1575
[email protected]c1bb5af2013-03-13 19:06:271576 if (!renderer_)
1577 return;
[email protected]94f206c12012-08-25 00:09:141578
[email protected]c1bb5af2013-03-13 19:06:271579 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141580}
1581
[email protected]206a3922013-05-17 06:34:121582ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071583 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1584 if (debug_state_.rasterize_only_visible_content) {
[email protected]3f2ff112013-08-03 02:41:071585 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291586 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071587 }
[email protected]206a3922013-05-17 06:34:121588
[email protected]3f2ff112013-08-03 02:41:071589 if (zero_budget_) {
1590 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071591 }
1592
[email protected]206a3922013-05-17 06:34:121593 return actual;
1594}
1595
[email protected]3f2ff112013-08-03 02:41:071596size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1597 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1598}
1599
[email protected]990e050a2013-09-23 18:50:211600int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1601 return ManagedMemoryPolicy::PriorityCutoffToValue(
1602 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1603}
1604
[email protected]50644642013-06-20 13:58:551605void LayerTreeHostImpl::ReleaseTreeResources() {
1606 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551607 SendReleaseResourcesRecursive(active_tree_->root_layer());
1608 if (pending_tree_ && pending_tree_->root_layer())
1609 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1610 if (recycle_tree_ && recycle_tree_->root_layer())
1611 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571612
[email protected]127bdc1a2013-09-11 01:44:481613 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551614}
1615
1616void LayerTreeHostImpl::CreateAndSetRenderer(
1617 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201618 ResourceProvider* resource_provider,
1619 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551620 DCHECK(!renderer_);
1621 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321622 renderer_ = DelegatingRenderer::Create(
1623 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091624 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551625 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321626 &settings_,
[email protected]50644642013-06-20 13:58:551627 output_surface,
1628 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111629 texture_mailbox_deleter_.get(),
[email protected]50644642013-06-20 13:58:551630 settings_.highp_threshold_min,
1631 settings_.force_direct_layer_drawing);
1632 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321633 renderer_ = SoftwareRenderer::Create(
1634 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551635 }
1636
[email protected]84975832013-06-29 00:24:111637 if (renderer_) {
[email protected]50644642013-06-20 13:58:551638 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111639 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561640
1641 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1642 // initialized to get max texture size. Also, after releasing resources,
1643 // trees need another update to generate new ones.
1644 active_tree_->set_needs_update_draw_properties();
1645 if (pending_tree_)
1646 pending_tree_->set_needs_update_draw_properties();
[email protected]84975832013-06-29 00:24:111647 }
[email protected]50644642013-06-20 13:58:551648}
1649
[email protected]50af3c22013-07-13 03:50:201650void LayerTreeHostImpl::CreateAndSetTileManager(
1651 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011652 ContextProvider* context_provider,
[email protected]50af3c22013-07-13 03:50:201653 bool using_map_image) {
1654 DCHECK(settings_.impl_side_painting);
1655 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011656 tile_manager_ =
1657 TileManager::Create(this,
1658 resource_provider,
1659 settings_.num_raster_threads,
1660 rendering_stats_instrumentation_,
1661 using_map_image,
[email protected]b4664142013-11-08 00:50:311662 GetMaxTransferBufferUsageBytes(context_provider),
1663 GetMaxRasterTasksUsageBytes(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011664
[email protected]50af3c22013-07-13 03:50:201665 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191666 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201667}
1668
[email protected]50644642013-06-20 13:58:551669void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071670 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551671}
1672
[email protected]c1bb5af2013-03-13 19:06:271673bool LayerTreeHostImpl::InitializeRenderer(
1674 scoped_ptr<OutputSurface> output_surface) {
1675 // Since we will create a new resource provider, we cannot continue to use
1676 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1677 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551678 ReleaseTreeResources();
[email protected]7ba3ca72013-04-11 06:37:251679 if (resource_provider_)
1680 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401681
[email protected]c1bb5af2013-03-13 19:06:271682 // Note: order is important here.
1683 renderer_.reset();
1684 tile_manager_.reset();
1685 resource_provider_.reset();
1686 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141687
[email protected]c1bb5af2013-03-13 19:06:271688 if (!output_surface->BindToClient(this))
1689 return false;
[email protected]be3181652012-09-25 13:02:131690
[email protected]a7f35682013-10-22 23:05:571691 scoped_ptr<ResourceProvider> resource_provider =
1692 ResourceProvider::Create(output_surface.get(),
1693 shared_bitmap_manager_,
1694 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341695 settings_.use_rgba_4444_textures,
1696 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551697 if (!resource_provider)
1698 return false;
[email protected]2b154b22013-06-07 09:03:271699
[email protected]50644642013-06-20 13:58:551700 if (output_surface->capabilities().deferred_gl_initialization)
1701 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131702
[email protected]50af3c22013-07-13 03:50:201703 bool skip_gl_renderer = false;
1704 CreateAndSetRenderer(
1705 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131706
[email protected]50644642013-06-20 13:58:551707 if (!renderer_)
1708 return false;
[email protected]6123930c2013-05-23 06:48:371709
[email protected]50644642013-06-20 13:58:551710 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201711 CreateAndSetTileManager(resource_provider.get(),
[email protected]b6eb8e332013-09-10 00:51:011712 output_surface->context_provider().get(),
[email protected]50af3c22013-07-13 03:50:201713 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241714 }
1715
[email protected]daea3d42013-10-23 17:04:501716 // Setup BeginImplFrameEmulation if it's not supported natively
1717 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351718 const base::TimeDelta display_refresh_interval =
1719 base::TimeDelta::FromMicroseconds(
1720 base::Time::kMicrosecondsPerSecond /
1721 settings_.refresh_rate);
1722
[email protected]daea3d42013-10-23 17:04:501723 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151724 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351725 settings_.throttle_frame_production,
1726 display_refresh_interval);
1727 }
1728
1729 int max_frames_pending =
1730 output_surface->capabilities().max_frames_pending;
1731 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351732 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351733 output_surface->SetMaxFramesPending(max_frames_pending);
1734
[email protected]50644642013-06-20 13:58:551735 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271736 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141737
[email protected]c1bb5af2013-03-13 19:06:271738 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211739
[email protected]c1bb5af2013-03-13 19:06:271740 return true;
[email protected]94f206c12012-08-25 00:09:141741}
1742
[email protected]2b154b22013-06-07 09:03:271743bool LayerTreeHostImpl::DeferredInitialize(
1744 scoped_refptr<ContextProvider> offscreen_context_provider) {
1745 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551746 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091747 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271748
[email protected]50644642013-06-20 13:58:551749 ReleaseTreeResources();
1750 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271751
[email protected]d9ca99e2013-08-28 21:49:481752 bool resource_provider_success = resource_provider_->InitializeGL();
1753
1754 bool success = resource_provider_success;
1755 if (success) {
1756 bool skip_gl_renderer = false;
1757 CreateAndSetRenderer(
1758 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1759 if (!renderer_)
1760 success = false;
1761 }
1762
1763 if (success) {
1764 if (offscreen_context_provider.get() &&
1765 !offscreen_context_provider->BindToCurrentThread())
1766 success = false;
1767 }
1768
[email protected]50644642013-06-20 13:58:551769 if (success) {
1770 EnforceZeroBudget(false);
1771 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481772 } else {
1773 if (offscreen_context_provider.get()) {
1774 if (offscreen_context_provider->BindToCurrentThread())
1775 offscreen_context_provider->VerifyContexts();
1776 offscreen_context_provider = NULL;
1777 }
1778
1779 client_->DidLoseOutputSurfaceOnImplThread();
1780
1781 if (resource_provider_success) {
1782 // If this fails the context provider will be dropped from the output
1783 // surface and destroyed. But the GLRenderer expects the output surface
1784 // to stick around - and hold onto the context3d - as long as it is alive.
1785 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1786 renderer_.reset();
1787
1788 // The resource provider can't stay in GL mode or it tries to clean up GL
1789 // stuff, but the context provider is going away on the output surface
1790 // which contradicts being in GL mode.
1791 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1792 resource_provider_->InitializeSoftware();
1793 }
[email protected]50644642013-06-20 13:58:551794 }
[email protected]b5174d712013-08-28 08:10:431795
1796 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271797 return success;
1798}
1799
[email protected]50af3c22013-07-13 03:50:201800void LayerTreeHostImpl::ReleaseGL() {
1801 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1802 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091803 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201804
1805 ReleaseTreeResources();
1806 renderer_.reset();
1807 tile_manager_.reset();
1808 resource_provider_->InitializeSoftware();
1809
1810 bool skip_gl_renderer = true;
1811 CreateAndSetRenderer(
1812 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1813 DCHECK(renderer_);
1814
1815 EnforceZeroBudget(true);
1816 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011817 NULL,
[email protected]50af3c22013-07-13 03:50:201818 GetRendererCapabilities().using_map_image);
1819 DCHECK(tile_manager_);
1820
[email protected]b5174d712013-08-28 08:10:431821 SetOffscreenContextProvider(NULL);
1822
[email protected]50af3c22013-07-13 03:50:201823 client_->SetNeedsCommitOnImplThread();
1824}
1825
[email protected]18ce59702013-04-09 04:58:401826void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1827 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271828 return;
[email protected]94f206c12012-08-25 00:09:141829
[email protected]54af03522013-09-05 00:43:281830 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271831 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271832
[email protected]c1bb5af2013-03-13 19:06:271833 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141834
[email protected]c1bb5af2013-03-13 19:06:271835 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141836
[email protected]c1bb5af2013-03-13 19:06:271837 if (renderer_)
1838 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211839
[email protected]c1bb5af2013-03-13 19:06:271840 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441841 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141842}
1843
[email protected]d9083762013-03-24 01:36:401844void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1845 if (overdraw_bottom_height == overdraw_bottom_height_)
1846 return;
1847 overdraw_bottom_height_ = overdraw_bottom_height;
1848
1849 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441850 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401851}
1852
[email protected]9f4f6a32013-09-04 21:35:121853void LayerTreeHostImpl::SetOverhangUIResource(
1854 UIResourceId overhang_ui_resource_id,
1855 gfx::Size overhang_ui_resource_size) {
1856 overhang_ui_resource_id_ = overhang_ui_resource_id;
1857 overhang_ui_resource_size_ = overhang_ui_resource_size;
1858}
1859
[email protected]c1bb5af2013-03-13 19:06:271860void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1861 if (device_scale_factor == device_scale_factor_)
1862 return;
1863 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271864
[email protected]94c40e6292013-05-24 22:01:501865 if (renderer_)
1866 renderer_->ViewportChanged();
1867
[email protected]c1bb5af2013-03-13 19:06:271868 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441869 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141870}
1871
[email protected]54af03522013-09-05 00:43:281872gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1873 return DeviceViewport().size();
1874}
1875
[email protected]f224cc92013-06-06 23:23:321876gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1877 if (external_viewport_.IsEmpty())
1878 return gfx::Rect(device_viewport_size_);
1879
1880 return external_viewport_;
1881}
1882
[email protected]54af03522013-09-05 00:43:281883gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1884 if (external_clip_.IsEmpty())
1885 return DeviceViewport();
1886
1887 return external_clip_;
1888}
1889
1890const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321891 return external_transform_;
1892}
1893
[email protected]c1bb5af2013-03-13 19:06:271894void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1895 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141896}
1897
[email protected]59adb112013-04-09 04:48:441898void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271899 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441900 active_tree_->set_needs_update_draw_properties();
1901 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141902}
1903
[email protected]c1bb5af2013-03-13 19:06:271904bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041905 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341906 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141907}
1908
[email protected]200a9c062013-05-20 04:34:371909void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1910 DCHECK(input_handler_client_ == NULL);
1911 input_handler_client_ = client;
1912}
1913
[email protected]420fdf6e2013-08-26 20:36:381914static LayerImpl* NextScrollLayer(LayerImpl* layer) {
1915 if (LayerImpl* scroll_parent = layer->scroll_parent())
1916 return scroll_parent;
1917 return layer->parent();
1918}
1919
[email protected]f620b0e72013-10-01 21:38:241920LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
1921 gfx::PointF device_viewport_point, InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:431922 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:021923 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:141924
[email protected]c1bb5af2013-03-13 19:06:271925 // Walk up the hierarchy and look for a scrollable layer.
1926 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:381927 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:271928 // The content layer can also block attempts to scroll outside the main
1929 // thread.
1930 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1931 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021932 *scroll_on_main_thread = true;
1933 return NULL;
[email protected]94f206c12012-08-25 00:09:141934 }
1935
[email protected]c1bb5af2013-03-13 19:06:271936 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1937 if (!scroll_layer_impl)
1938 continue;
[email protected]94f206c12012-08-25 00:09:141939
[email protected]c1bb5af2013-03-13 19:06:271940 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:271941 // If any layer wants to divert the scroll event to the main thread, abort.
1942 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021943 *scroll_on_main_thread = true;
1944 return NULL;
[email protected]94f206c12012-08-25 00:09:141945 }
1946
[email protected]c1bb5af2013-03-13 19:06:271947 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1948 potentially_scrolling_layer_impl = scroll_layer_impl;
1949 }
1950
1951 // When hiding top controls is enabled and the controls are hidden or
1952 // overlaying the content, force scrolls to be enabled on the root layer to
1953 // allow bringing the top controls back into view.
1954 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1955 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521956 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271957 potentially_scrolling_layer_impl = RootScrollLayer();
1958 }
1959
[email protected]bf1cfd9a2013-09-26 05:43:021960 return potentially_scrolling_layer_impl;
1961}
1962
1963InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1964 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
1965 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
1966
1967 if (top_controls_manager_)
1968 top_controls_manager_->ScrollBegin();
1969
1970 DCHECK(!CurrentlyScrollingLayer());
1971 ClearCurrentlyScrollingLayer();
1972
1973 if (!EnsureRenderSurfaceLayerList())
1974 return ScrollIgnored;
1975
[email protected]f620b0e72013-10-01 21:38:241976 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1977 device_scale_factor_);
1978 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
1979 device_viewport_point,
1980 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:021981 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:241982 LayerImpl* potentially_scrolling_layer_impl =
1983 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
1984 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:021985
1986 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:021987 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1988 return ScrollOnMainThread;
1989 }
1990
[email protected]34d43432013-09-26 20:04:101991 // If we want to send a DidOverscroll for this scroll it can't be ignored.
1992 if (!potentially_scrolling_layer_impl && settings_.always_overscroll)
1993 potentially_scrolling_layer_impl = RootScrollLayer();
1994
[email protected]c1bb5af2013-03-13 19:06:271995 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201996 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271997 potentially_scrolling_layer_impl);
1998 should_bubble_scrolls_ = (type != NonBubblingGesture);
[email protected]d41d7cb92013-10-15 16:08:091999 last_scroll_did_bubble_ = false;
[email protected]c1bb5af2013-03-13 19:06:272000 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272001 client_->RenewTreePriority();
2002 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2003 return ScrollStarted;
2004 }
2005 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142006}
2007
[email protected]c1bb5af2013-03-13 19:06:272008gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2009 LayerImpl* layer_impl,
2010 float scale_from_viewport_to_screen_space,
2011 gfx::PointF viewport_point,
2012 gfx::Vector2dF viewport_delta) {
2013 // Layers with non-invertible screen space transforms should not have passed
2014 // the scroll hit test in the first place.
2015 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2016 gfx::Transform inverse_screen_space_transform(
2017 gfx::Transform::kSkipInitialization);
2018 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2019 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352020 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2021 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272022 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142023
[email protected]c1bb5af2013-03-13 19:06:272024 gfx::PointF screen_space_point =
2025 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142026
[email protected]c1bb5af2013-03-13 19:06:272027 gfx::Vector2dF screen_space_delta = viewport_delta;
2028 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2029
2030 // First project the scroll start and end points to local layer space to find
2031 // the scroll delta in layer coordinates.
2032 bool start_clipped, end_clipped;
2033 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2034 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212035 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272036 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212037 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272038 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212039 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272040 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212041 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272042
2043 // In general scroll point coordinates should not get clipped.
2044 DCHECK(!start_clipped);
2045 DCHECK(!end_clipped);
2046 if (start_clipped || end_clipped)
2047 return gfx::Vector2dF();
2048
2049 // local_start_point and local_end_point are in content space but we want to
2050 // move them to layer space for scrolling.
2051 float width_scale = 1.f / layer_impl->contents_scale_x();
2052 float height_scale = 1.f / layer_impl->contents_scale_y();
2053 local_start_point.Scale(width_scale, height_scale);
2054 local_end_point.Scale(width_scale, height_scale);
2055
2056 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472057 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272058 layer_impl->ScrollBy(local_end_point - local_start_point);
2059
2060 // Get the end point in the layer's content space so we can apply its
2061 // ScreenSpaceTransform.
2062 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472063 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272064 previous_delta;
2065 gfx::PointF actual_local_content_end_point =
2066 gfx::ScalePoint(actual_local_end_point,
2067 1.f / width_scale,
2068 1.f / height_scale);
2069
2070 // Calculate the applied scroll delta in viewport space coordinates.
2071 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212072 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272073 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212074 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272075 DCHECK(!end_clipped);
2076 if (end_clipped)
2077 return gfx::Vector2dF();
2078 gfx::PointF actual_viewport_end_point =
2079 gfx::ScalePoint(actual_screen_space_end_point,
2080 1.f / scale_from_viewport_to_screen_space);
2081 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142082}
2083
[email protected]c1bb5af2013-03-13 19:06:272084static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
2085 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:472086 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272087 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472088 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272089}
2090
2091bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
2092 gfx::Vector2dF scroll_delta) {
2093 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2094 if (!CurrentlyScrollingLayer())
2095 return false;
2096
2097 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532098 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292099 bool did_scroll_x = false;
2100 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:422101 bool consume_by_top_controls = top_controls_manager_ &&
2102 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]d41d7cb92013-10-15 16:08:092103 last_scroll_did_bubble_ = false;
[email protected]a91e4f82013-03-15 06:58:062104
[email protected]c1bb5af2013-03-13 19:06:272105 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2106 layer_impl;
2107 layer_impl = layer_impl->parent()) {
2108 if (!layer_impl->scrollable())
2109 continue;
2110
[email protected]a2b5ded2013-05-20 21:32:532111 if (layer_impl == RootScrollLayer()) {
2112 // Only allow bubble scrolling when the scroll is in the direction to make
2113 // the top controls visible.
2114 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
2115 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
2116 UpdateMaxScrollOffset();
2117 }
2118 // Track root layer deltas for reporting overscroll.
2119 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422120 }
2121
[email protected]c1bb5af2013-03-13 19:06:272122 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272123 // Gesture events need to be transformed from viewport coordinates to local
2124 // layer coordinates so that the scrolling contents exactly follow the
2125 // user's finger. In contrast, wheel events represent a fixed amount of
2126 // scrolling so we can just apply them directly.
2127 if (!wheel_scrolling_) {
2128 float scale_from_viewport_to_screen_space = device_scale_factor_;
2129 applied_delta =
2130 ScrollLayerWithViewportSpaceDelta(layer_impl,
2131 scale_from_viewport_to_screen_space,
2132 viewport_point, pending_delta);
2133 } else {
2134 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142135 }
[email protected]94f206c12012-08-25 00:09:142136
[email protected]c1bb5af2013-03-13 19:06:272137 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292138 float move_threshold = 0.1f;
2139 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2140 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2141 did_scroll_x |= did_move_layer_x;
2142 did_scroll_y |= did_move_layer_y;
2143 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]d41d7cb92013-10-15 16:08:092144 if (!did_lock_scrolling_layer_)
[email protected]c1bb5af2013-03-13 19:06:272145 continue;
[email protected]d41d7cb92013-10-15 16:08:092146
2147 if (should_bubble_scrolls_) {
2148 last_scroll_did_bubble_ = true;
2149 continue;
2150 }
2151
2152 break;
[email protected]94f206c12012-08-25 00:09:142153 }
[email protected]a2b5ded2013-05-20 21:32:532154
2155 if (layer_impl == RootScrollLayer())
2156 unused_root_delta.Subtract(applied_delta);
2157
[email protected]c1bb5af2013-03-13 19:06:272158 did_lock_scrolling_layer_ = true;
2159 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202160 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272161 break;
[email protected]94f206c12012-08-25 00:09:142162 }
[email protected]94f206c12012-08-25 00:09:142163
[email protected]c1bb5af2013-03-13 19:06:272164 // If the applied delta is within 45 degrees of the input delta, bail out to
2165 // make it easier to scroll just one layer in one direction without
2166 // affecting any of its parents.
2167 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212168 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272169 applied_delta, pending_delta) < angle_threshold) {
2170 pending_delta = gfx::Vector2d();
2171 break;
[email protected]4a23c374c2012-12-08 08:38:552172 }
[email protected]c1bb5af2013-03-13 19:06:272173
2174 // Allow further movement only on an axis perpendicular to the direction in
2175 // which the layer moved.
2176 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212177 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272178
[email protected]a2b5ded2013-05-20 21:32:532179 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272180 break;
2181 }
2182
[email protected]2bd503f2013-07-23 05:35:292183 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272184 if (did_scroll) {
2185 client_->SetNeedsCommitOnImplThread();
2186 client_->SetNeedsRedrawOnImplThread();
2187 client_->RenewTreePriority();
2188 }
[email protected]a2b5ded2013-05-20 21:32:532189
[email protected]2bd503f2013-07-23 05:35:292190 // Scrolling along an axis resets accumulated root overscroll for that axis.
2191 if (did_scroll_x)
2192 accumulated_root_overscroll_.set_x(0);
2193 if (did_scroll_y)
2194 accumulated_root_overscroll_.set_y(0);
2195
[email protected]a2b5ded2013-05-20 21:32:532196 accumulated_root_overscroll_ += unused_root_delta;
2197 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2198 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112199 DidOverscrollParams params;
2200 params.accumulated_overscroll = accumulated_root_overscroll_;
2201 params.latest_overscroll_delta = unused_root_delta;
2202 params.current_fling_velocity = current_fling_velocity_;
2203 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532204 }
2205
[email protected]c1bb5af2013-03-13 19:06:272206 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552207}
2208
[email protected]be782f52013-03-23 21:36:142209// This implements scrolling by page as described here:
2210// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2211// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492212bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2213 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142214 DCHECK(wheel_scrolling_);
2215
2216 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2217 layer_impl;
2218 layer_impl = layer_impl->parent()) {
2219 if (!layer_impl->scrollable())
2220 continue;
2221
2222 if (!layer_impl->vertical_scrollbar_layer())
2223 continue;
2224
2225 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2226
2227 // These magical values match WebKit and are designed to scroll nearly the
2228 // entire visible content height but leave a bit of overlap.
2229 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492230 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142231 page = -page;
2232
2233 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2234
2235 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2236
2237 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142238 client_->SetNeedsCommitOnImplThread();
2239 client_->SetNeedsRedrawOnImplThread();
2240 client_->RenewTreePriority();
2241 return true;
2242 }
2243
2244 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2245 }
2246
2247 return false;
2248}
2249
[email protected]1960a712013-04-30 17:06:472250void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2251 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2252 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2253 active_tree_->SetRootLayerScrollOffsetDelegate(
2254 root_layer_scroll_offset_delegate_);
2255}
2256
2257void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2258 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2259 client_->SetNeedsCommitOnImplThread();
2260}
2261
[email protected]c1bb5af2013-03-13 19:06:272262void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2263 active_tree_->ClearCurrentlyScrollingLayer();
2264 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532265 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202266 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142267}
2268
[email protected]c1bb5af2013-03-13 19:06:272269void LayerTreeHostImpl::ScrollEnd() {
2270 if (top_controls_manager_)
2271 top_controls_manager_->ScrollEnd();
2272 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052273 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142274}
2275
[email protected]5ff3c9782013-04-29 17:35:122276InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462277 if (!active_tree_->CurrentlyScrollingLayer())
2278 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212279
[email protected]75147f52013-08-02 19:11:462280 if (settings_.ignore_root_layer_flings &&
2281 active_tree_->CurrentlyScrollingLayer() ==
2282 active_tree_->RootScrollLayer()) {
2283 ClearCurrentlyScrollingLayer();
2284 return ScrollIgnored;
2285 }
2286
[email protected]df0c42342013-10-08 20:52:122287 if (!wheel_scrolling_)
[email protected]d41d7cb92013-10-15 16:08:092288 should_bubble_scrolls_ = last_scroll_did_bubble_;
[email protected]df0c42342013-10-08 20:52:122289
[email protected]75147f52013-08-02 19:11:462290 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212291}
2292
[email protected]6e921bd2013-04-29 21:10:202293void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2294 current_fling_velocity_ = velocity;
2295}
2296
[email protected]bf1cfd9a2013-09-26 05:43:022297float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
2298 gfx::PointF device_viewport_point,
2299 LayerImpl* layer_impl) {
2300 if (!layer_impl)
2301 return std::numeric_limits<float>::max();
2302
2303 gfx::Rect layer_impl_bounds(
2304 layer_impl->content_bounds());
2305
2306 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2307 layer_impl->screen_space_transform(),
2308 layer_impl_bounds);
2309
2310 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2311 device_viewport_point);
2312}
2313
2314void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2315 if (!EnsureRenderSurfaceLayerList())
2316 return;
2317
[email protected]f620b0e72013-10-01 21:38:242318 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2319 device_scale_factor_);
2320
2321 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2322 device_viewport_point,
2323 active_tree_->RenderSurfaceLayerList());
2324 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2325 return;
2326
2327 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2328 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2329 scroll_layer_id_when_mouse_over_scrollbar_);
2330
2331 ScrollbarAnimationController* animation_controller =
2332 scroll_layer_impl->scrollbar_animation_controller();
2333 if (animation_controller) {
2334 animation_controller->DidMouseMoveOffScrollbar(
2335 CurrentPhysicalTimeTicks());
2336 StartScrollbarAnimation();
2337 }
2338 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2339 }
2340
[email protected]bf1cfd9a2013-09-26 05:43:022341 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242342 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2343 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022344 &scroll_on_main_thread);
2345 if (scroll_on_main_thread || !scroll_layer_impl)
2346 return;
2347
2348 ScrollbarAnimationController* animation_controller =
2349 scroll_layer_impl->scrollbar_animation_controller();
2350 if (!animation_controller)
2351 return;
2352
[email protected]bf1cfd9a2013-09-26 05:43:022353 float distance_to_scrollbar = std::min(
2354 DeviceSpaceDistanceToLayer(device_viewport_point,
2355 scroll_layer_impl->horizontal_scrollbar_layer()),
2356 DeviceSpaceDistanceToLayer(device_viewport_point,
2357 scroll_layer_impl->vertical_scrollbar_layer()));
2358
2359 bool should_animate = animation_controller->DidMouseMoveNear(
2360 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202361 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022362 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022363}
2364
[email protected]f620b0e72013-10-01 21:38:242365bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
2366 gfx::PointF device_viewport_point) {
2367 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2368 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2369 layer_impl = active_tree_->LayerById(scroll_layer_id);
2370 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2371 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202372 bool should_animate =
2373 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2374 CurrentPhysicalTimeTicks(), 0);
2375 if (should_animate)
2376 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242377 } else {
2378 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2379 }
2380
2381 return true;
2382 }
2383
2384 return false;
2385}
2386
[email protected]c1bb5af2013-03-13 19:06:272387void LayerTreeHostImpl::PinchGestureBegin() {
2388 pinch_gesture_active_ = true;
2389 previous_pinch_anchor_ = gfx::Point();
2390 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132391 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]9ec24df2013-08-09 23:32:572392 active_tree_->SetCurrentlyScrollingLayer(RootScrollLayer());
[email protected]a9bda5e2013-10-25 18:43:372393 if (top_controls_manager_)
2394 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142395}
2396
[email protected]c1bb5af2013-03-13 19:06:272397void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2398 gfx::Point anchor) {
2399 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282400
[email protected]c1bb5af2013-03-13 19:06:272401 if (!RootScrollLayer())
2402 return;
[email protected]d3afa112012-12-08 06:24:282403
[email protected]c1bb5af2013-03-13 19:06:272404 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2405 // position over the course of the magnify.
2406 float page_scale_delta = active_tree_->page_scale_delta();
2407 gfx::PointF previous_scale_anchor =
2408 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2409 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2410 page_scale_delta = active_tree_->page_scale_delta();
2411 gfx::PointF new_scale_anchor =
2412 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2413 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2414
2415 previous_pinch_anchor_ = anchor;
2416
2417 move.Scale(1 / active_tree_->page_scale_factor());
2418
2419 RootScrollLayer()->ScrollBy(move);
2420
[email protected]c1bb5af2013-03-13 19:06:272421 client_->SetNeedsCommitOnImplThread();
2422 client_->SetNeedsRedrawOnImplThread();
2423 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282424}
2425
[email protected]c1bb5af2013-03-13 19:06:272426void LayerTreeHostImpl::PinchGestureEnd() {
2427 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132428 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2429 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2430 ClearCurrentlyScrollingLayer();
2431 }
[email protected]a9bda5e2013-10-25 18:43:372432 if (top_controls_manager_)
2433 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272434 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142435}
2436
[email protected]c1bb5af2013-03-13 19:06:272437static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2438 LayerImpl* layer_impl) {
2439 if (!layer_impl)
2440 return;
[email protected]94f206c12012-08-25 00:09:142441
[email protected]c1bb5af2013-03-13 19:06:272442 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472443 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272444 if (!scroll_delta.IsZero()) {
2445 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392446 scroll.layer_id = layer_impl->id();
2447 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272448 scroll_info->scrolls.push_back(scroll);
2449 layer_impl->SetSentScrollDelta(scroll_delta);
2450 }
[email protected]94f206c12012-08-25 00:09:142451
[email protected]c1bb5af2013-03-13 19:06:272452 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2453 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142454}
2455
[email protected]c1bb5af2013-03-13 19:06:272456scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2457 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302458
[email protected]c1bb5af2013-03-13 19:06:272459 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392460 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2461 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302462
[email protected]c1bb5af2013-03-13 19:06:272463 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302464}
2465
[email protected]c1bb5af2013-03-13 19:06:272466void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282467 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102468}
2469
[email protected]c1bb5af2013-03-13 19:06:272470void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2471 if (!page_scale_animation_ || !RootScrollLayer())
2472 return;
2473
2474 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2475 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472476 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272477
[email protected]34e1b512013-09-26 17:32:332478 if (!page_scale_animation_->IsAnimationStarted())
2479 page_scale_animation_->StartAnimation(monotonic_time);
2480
[email protected]c1bb5af2013-03-13 19:06:272481 active_tree_->SetPageScaleDelta(
2482 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2483 active_tree_->page_scale_factor());
2484 gfx::Vector2dF next_scroll =
2485 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2486
2487 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2488 client_->SetNeedsRedrawOnImplThread();
2489
2490 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2491 page_scale_animation_.reset();
2492 client_->SetNeedsCommitOnImplThread();
2493 client_->RenewTreePriority();
2494 }
[email protected]829ad972013-01-28 23:36:102495}
2496
[email protected]ffb2720f2013-03-15 19:18:372497void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2498 if (!top_controls_manager_ || !RootScrollLayer())
2499 return;
2500 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2501 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172502 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2503 return;
[email protected]ffb2720f2013-03-15 19:18:372504 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2505 scroll, 1.f / active_tree_->total_page_scale_factor()));
2506}
2507
[email protected]c1bb5af2013-03-13 19:06:272508void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2509 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522510 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272511 animation_registrar_->active_animation_controllers().empty() ||
2512 !active_tree_->root_layer())
2513 return;
2514
2515 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2516
2517 last_animation_time_ = wall_clock_time;
2518 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2519
2520 AnimationRegistrar::AnimationControllerMap copy =
2521 animation_registrar_->active_animation_controllers();
2522 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2523 iter != copy.end();
2524 ++iter)
2525 (*iter).second->Animate(monotonic_seconds);
2526
2527 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082528}
2529
[email protected]3d9f7432013-04-06 00:35:182530void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522531 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272532 animation_registrar_->active_animation_controllers().empty() ||
2533 !active_tree_->root_layer())
2534 return;
2535
2536 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2537 scoped_ptr<AnimationEventsVector> events =
2538 make_scoped_ptr(new AnimationEventsVector);
2539 AnimationRegistrar::AnimationControllerMap copy =
2540 animation_registrar_->active_animation_controllers();
2541 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2542 iter != copy.end();
2543 ++iter)
[email protected]3d9f7432013-04-06 00:35:182544 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272545
2546 if (!events->empty()) {
2547 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2548 last_animation_time_);
2549 }
[email protected]131a0c22013-02-12 18:31:082550}
2551
[email protected]c1bb5af2013-03-13 19:06:272552base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2553 return base::TimeDelta::FromSeconds(1);
2554}
2555
[email protected]50644642013-06-20 13:58:552556void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352557 DCHECK(current);
[email protected]50644642013-06-20 13:58:552558 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352559 current->DidLoseOutputSurface();
2560 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552561 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352562 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552563 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352564 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552565 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352566}
2567
[email protected]b5174d712013-08-28 08:10:432568void LayerTreeHostImpl::SetOffscreenContextProvider(
2569 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2570 if (!offscreen_context_provider.get()) {
2571 offscreen_context_provider_ = NULL;
2572 return;
2573 }
2574
2575 if (!offscreen_context_provider->BindToCurrentThread()) {
2576 offscreen_context_provider_ = NULL;
2577 return;
2578 }
2579
2580 offscreen_context_provider_ = offscreen_context_provider;
2581}
2582
[email protected]c1bb5af2013-03-13 19:06:272583std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2584 std::string str;
2585 if (active_tree_->root_layer()) {
2586 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2587 base::JSONWriter::WriteWithOptions(
2588 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2589 }
2590 return str;
2591}
2592
[email protected]c1bb5af2013-03-13 19:06:272593int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362594 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272595}
2596
[email protected]c1bb5af2013-03-13 19:06:272597void LayerTreeHostImpl::SendManagedMemoryStats(
2598 size_t memory_visible_bytes,
2599 size_t memory_visible_and_nearby_bytes,
2600 size_t memory_use_bytes) {
2601 if (!renderer_)
2602 return;
2603
2604 // Round the numbers being sent up to the next 8MB, to throttle the rate
2605 // at which we spam the GPU process.
2606 static const size_t rounding_step = 8 * 1024 * 1024;
2607 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2608 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2609 rounding_step);
2610 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2611 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2612 last_sent_memory_visible_and_nearby_bytes_ ==
2613 memory_visible_and_nearby_bytes &&
2614 last_sent_memory_use_bytes_ == memory_use_bytes) {
2615 return;
2616 }
2617 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2618 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2619 last_sent_memory_use_bytes_ = memory_use_bytes;
2620
2621 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2622 last_sent_memory_visible_and_nearby_bytes_,
2623 last_sent_memory_use_bytes_);
2624}
2625
2626void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2627 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2628}
2629
2630void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2631 base::TimeTicks time) {
2632 if (!layer)
2633 return;
2634
2635 ScrollbarAnimationController* scrollbar_controller =
2636 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352637 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202638 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422639 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2640 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272641 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202642 }
[email protected]c1bb5af2013-03-13 19:06:272643
2644 for (size_t i = 0; i < layer->children().size(); ++i)
2645 AnimateScrollbarsRecursive(layer->children()[i], time);
2646}
2647
[email protected]21c9dee72013-06-15 01:20:052648void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202649 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052650 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202651}
2652
2653void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2654 base::TimeTicks time) {
2655 if (!layer)
2656 return;
2657
2658 ScrollbarAnimationController* scrollbar_controller =
2659 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352660 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2661 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202662 if (delay > base::TimeDelta())
2663 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352664 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202665 client_->SetNeedsRedrawOnImplThread();
2666 }
2667
2668 for (size_t i = 0; i < layer->children().size(); ++i)
2669 StartScrollbarAnimationRecursive(layer->children()[i], time);
2670}
2671
[email protected]c1bb5af2013-03-13 19:06:272672void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2673 if (!tile_manager_)
2674 return;
2675
[email protected]1bcced22013-09-24 13:51:192676 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272677 return;
[email protected]1bcced22013-09-24 13:51:192678 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082679 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272680}
2681
[email protected]8347d692013-05-17 23:22:382682void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552683 current_frame_timeticks_ = base::TimeTicks();
2684 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272685}
2686
[email protected]21c9dee72013-06-15 01:20:052687void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2688 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552689 if (ticks->is_null()) {
2690 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052691 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552692 *now = base::Time::Now();
2693 }
2694}
2695
2696base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2697 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2698 return current_frame_timeticks_;
2699}
2700
2701base::Time LayerTreeHostImpl::CurrentFrameTime() {
2702 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272703 return current_frame_time_;
2704}
2705
[email protected]21c9dee72013-06-15 01:20:052706base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462707 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052708}
2709
[email protected]34806722013-08-09 23:51:212710scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2711 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272712 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222713 if (this->pending_tree_)
2714 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272715 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212716 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272717 if (tile_manager_)
2718 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2719 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222720 if (pending_tree_)
2721 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212722 if (frame)
2723 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222724 return state.PassAs<base::Value>();
2725}
2726
2727scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222728 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2729 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472730 if (tile_manager_)
2731 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272732 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082733}
2734
[email protected]6e7fdeb2013-07-09 14:28:382735void LayerTreeHostImpl::SetDebugState(
2736 const LayerTreeDebugState& new_debug_state) {
2737 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2738 return;
2739 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272740 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242741
[email protected]6e7fdeb2013-07-09 14:28:382742 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122743 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382744 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022745}
2746
[email protected]741fba422013-09-20 03:34:142747void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2748 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572749 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572750
[email protected]efa48412013-09-05 15:30:162751 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142752 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162753 case UIResourceBitmap::CLAMP_TO_EDGE:
2754 wrap_mode = GL_CLAMP_TO_EDGE;
2755 break;
2756 case UIResourceBitmap::REPEAT:
2757 wrap_mode = GL_REPEAT;
2758 break;
2759 }
2760
[email protected]c9280762013-08-01 06:28:572761 // Allow for multiple creation requests with the same UIResourceId. The
2762 // previous resource is simply deleted.
2763 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2764 if (id)
2765 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122766
2767 ResourceFormat format = resource_provider_->best_texture_format();
2768 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2769 format = ETC1;
[email protected]c9280762013-08-01 06:28:572770 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142771 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162772 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142773 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122774 format);
[email protected]c9280762013-08-01 06:28:572775
[email protected]efbdb3a2013-10-04 00:35:132776 UIResourceData data;
2777 data.resource_id = id;
2778 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512779 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132780
2781 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032782
2783 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572784 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032785 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142786 gfx::Rect(bitmap.GetSize()),
2787 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572788 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482789 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572790}
2791
2792void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2793 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2794 if (id) {
2795 resource_provider_->DeleteResource(id);
2796 ui_resource_map_.erase(uid);
2797 }
[email protected]127bdc1a2013-09-11 01:44:482798 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572799}
2800
[email protected]127bdc1a2013-09-11 01:44:482801void LayerTreeHostImpl::EvictAllUIResources() {
2802 if (ui_resource_map_.empty())
2803 return;
2804
[email protected]5f4dc4f2013-09-05 14:58:212805 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2806 iter != ui_resource_map_.end();
2807 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482808 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132809 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212810 }
2811 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482812
2813 client_->SetNeedsCommitOnImplThread();
2814 client_->OnCanDrawStateChanged(CanDraw());
2815 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212816}
2817
[email protected]c9280762013-08-01 06:28:572818ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2819 UIResourceId uid) const {
2820 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2821 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132822 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572823 return 0;
2824}
2825
[email protected]709c9542013-10-26 01:43:512826bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2827 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2828 DCHECK(iter != ui_resource_map_.end());
2829 return iter->second.opaque;
2830}
2831
[email protected]127bdc1a2013-09-11 01:44:482832bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2833 return !evicted_ui_resources_.empty();
2834}
2835
2836void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2837 std::set<UIResourceId>::iterator found_in_evicted =
2838 evicted_ui_resources_.find(uid);
2839 if (found_in_evicted == evicted_ui_resources_.end())
2840 return;
2841 evicted_ui_resources_.erase(found_in_evicted);
2842 if (evicted_ui_resources_.empty())
2843 client_->OnCanDrawStateChanged(CanDraw());
2844}
2845
[email protected]5e5648a2013-11-18 00:39:332846void LayerTreeHostImpl::ScheduleMicroBenchmark(
2847 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2848 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2849}
2850
[email protected]d3143c732012-10-05 19:17:592851} // namespace cc