blob: 7f7d45a72dfa860111b4ed72d4ad9d19ed2aa1e1 [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]66b52e12013-11-17 15:53:18752 const bool prevent_occlusion = false;
[email protected]c1bb5af2013-03-13 19:06:27753 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09754 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27755 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09756 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27757 it != end;
758 ++it) {
759 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09760 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27761 RenderPass* target_render_pass =
762 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14763
[email protected]2ea5e6c2013-04-26 21:52:23764 occlusion_tracker.EnterLayer(it, prevent_occlusion);
[email protected]94f206c12012-08-25 00:09:14765
[email protected]30fe19ff2013-07-04 00:54:45766 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30767
[email protected]18a70192013-04-26 16:18:25768 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36769 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25770 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11771 it->TakeCopyRequestsAndTransformToTarget(
772 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25773 }
[email protected]30fe19ff2013-07-04 00:54:45774 } else if (it.represents_contributing_render_surface() &&
775 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27776 RenderPass::Id contributing_render_pass_id =
777 it->render_surface()->RenderPassId();
778 RenderPass* contributing_render_pass =
779 frame->render_passes_by_id[contributing_render_pass_id];
780 AppendQuadsForRenderSurfaceLayer(target_render_pass,
781 *it,
782 contributing_render_pass,
783 occlusion_tracker,
784 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45785 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09786 !it->visible_content_rect().IsEmpty()) {
[email protected]c1bb5af2013-03-13 19:06:27787 bool impl_draw_transform_is_unknown = false;
[email protected]fbc293322013-10-01 05:07:15788 bool occluded = occlusion_tracker.Occluded(
[email protected]c1bb5af2013-03-13 19:06:27789 it->render_target(),
790 it->visible_content_rect(),
791 it->draw_transform(),
[email protected]cfc2d2d2013-10-04 23:26:45792 impl_draw_transform_is_unknown);
[email protected]fbc293322013-10-01 05:07:15793 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27794 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59795
[email protected]c1bb5af2013-03-13 19:06:27796 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57797
[email protected]c1bb5af2013-03-13 19:06:27798 if (it->HasContributingDelegatedRenderPasses()) {
799 RenderPass::Id contributing_render_pass_id =
800 it->FirstContributingRenderPassId();
801 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
802 frame->render_passes_by_id.end()) {
803 RenderPass* render_pass =
804 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14805
[email protected]c1bb5af2013-03-13 19:06:27806 AppendQuadsData append_quads_data(render_pass->id);
807 AppendQuadsForLayer(render_pass,
808 *it,
809 occlusion_tracker,
810 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57811
[email protected]c1bb5af2013-03-13 19:06:27812 contributing_render_pass_id =
813 it->NextContributingRenderPassId(contributing_render_pass_id);
814 }
[email protected]94f206c12012-08-25 00:09:14815 }
816
[email protected]c1bb5af2013-03-13 19:06:27817 AppendQuadsForLayer(target_render_pass,
818 *it,
819 occlusion_tracker,
820 &append_quads_data);
821 }
[email protected]89228202012-08-29 03:20:30822
[email protected]372bad5f2013-03-21 16:38:43823 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14824 }
825
[email protected]e1e768f2013-03-26 08:48:09826 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27827 bool layer_has_animating_transform =
828 it->screen_space_transform_is_animating() ||
829 it->draw_transform_is_animating();
830 if (layer_has_animating_transform)
831 draw_frame = false;
832 }
833
[email protected]e1e768f2013-03-26 08:48:09834 if (append_quads_data.had_incomplete_tile)
[email protected]c1bb5af2013-03-13 19:06:27835 frame->contains_incomplete_tile = true;
836
837 occlusion_tracker.LeaveLayer(it);
838 }
839
[email protected]6133cc232013-07-30 18:47:07840 if (have_copy_request ||
841 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]18a70192013-04-26 16:18:25842 draw_frame = true;
843
[email protected]1d993172012-10-18 18:15:04844#ifndef NDEBUG
[email protected]c1bb5af2013-03-13 19:06:27845 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
846 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
847 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
848 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
849 != frame->render_passes_by_id.end());
850 }
[email protected]94f206c12012-08-25 00:09:14851#endif
[email protected]c1bb5af2013-03-13 19:06:27852 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14853
[email protected]c1bb5af2013-03-13 19:06:27854 if (!active_tree_->has_transparent_background()) {
855 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08856 AppendQuadsToFillScreen(
857 ResourceIdForUIResource(overhang_ui_resource_id_),
858 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
859 active_tree_->RootScrollLayerDeviceViewportBounds(),
860 frame->render_passes.back(),
861 active_tree_->root_layer(),
862 active_tree_->background_color(),
863 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27864 }
[email protected]94f206c12012-08-25 00:09:14865
[email protected]c1bb5af2013-03-13 19:06:27866 if (draw_frame)
867 occlusion_tracker.overdraw_metrics()->RecordMetrics(this);
[email protected]18a70192013-04-26 16:18:25868 else
869 DCHECK(!have_copy_request);
[email protected]94f206c12012-08-25 00:09:14870
[email protected]c1bb5af2013-03-13 19:06:27871 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48872 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14873
[email protected]30fe19ff2013-07-04 00:54:45874 // Any copy requests left in the tree are not going to get serviced, and
875 // should be aborted.
876 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
877 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
878 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
879 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
880 }
881 for (size_t i = 0; i < requests_to_abort.size(); ++i)
882 requests_to_abort[i]->SendEmptyResult();
883
[email protected]e0341352013-04-06 05:01:20884 // If we're making a frame to draw, it better have at least one render pass.
885 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48886
887 // Should only have one render pass in resourceless software mode.
888 if (output_surface_->ForcedDrawToSoftwareDevice())
889 DCHECK_EQ(1u, frame->render_passes.size());
890
[email protected]c1bb5af2013-03-13 19:06:27891 return draw_frame;
[email protected]94f206c12012-08-25 00:09:14892}
893
[email protected]200a9c062013-05-20 04:34:37894void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
895 if (input_handler_client_)
896 input_handler_client_->MainThreadHasStoppedFlinging();
897}
898
[email protected]3d9f7432013-04-06 00:35:18899void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
900 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15901 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01902 if (should_background_tick)
903 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15904
[email protected]3d9f7432013-04-06 00:35:18905 bool enabled = should_background_tick &&
906 !animation_registrar_->active_animation_controllers().empty();
907
[email protected]c1bb5af2013-03-13 19:06:27908 // Lazily create the time_source adapter so that we can vary the interval for
909 // testing.
910 if (!time_source_client_adapter_) {
911 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
912 this,
[email protected]810d40b72013-06-20 18:26:15913 DelayBasedTimeSource::Create(
914 LowFrequencyAnimationInterval(),
915 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
916 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27917 }
[email protected]94f206c12012-08-25 00:09:14918
[email protected]c1bb5af2013-03-13 19:06:27919 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14920}
921
[email protected]b9d4a362013-04-23 05:36:27922void LayerTreeHostImpl::SetViewportDamage(gfx::Rect damage_rect) {
[email protected]878705be2013-04-15 22:44:02923 viewport_damage_rect_.Union(damage_rect);
924}
925
[email protected]c1bb5af2013-03-13 19:06:27926static inline RenderPass* FindRenderPassById(
927 RenderPass::Id render_pass_id,
928 const LayerTreeHostImpl::FrameData& frame) {
929 RenderPassIdHashMap::const_iterator it =
930 frame.render_passes_by_id.find(render_pass_id);
931 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14932}
933
[email protected]c1bb5af2013-03-13 19:06:27934static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
935 LayerTreeHostImpl::FrameData* frame) {
936 RenderPass* remove_render_pass =
937 FindRenderPassById(remove_render_pass_id, *frame);
938 // The pass was already removed by another quad - probably the original, and
939 // we are the replica.
940 if (!remove_render_pass)
941 return;
942 RenderPassList& render_passes = frame->render_passes;
943 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
944 render_passes.end(),
945 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14946
[email protected]c1bb5af2013-03-13 19:06:27947 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14948
[email protected]c1bb5af2013-03-13 19:06:27949 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
950 frame->render_passes.erase(to_remove);
951 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:14952
[email protected]c1bb5af2013-03-13 19:06:27953 // Now follow up for all RenderPass quads and remove their RenderPasses
954 // recursively.
955 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29956 QuadList::ConstBackToFrontIterator quad_list_iterator =
957 quad_list.BackToFrontBegin();
958 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27959 ++quad_list_iterator) {
960 DrawQuad* current_quad = (*quad_list_iterator);
961 if (current_quad->material != DrawQuad::RENDER_PASS)
962 continue;
[email protected]94f206c12012-08-25 00:09:14963
[email protected]c1bb5af2013-03-13 19:06:27964 RenderPass::Id next_remove_render_pass_id =
965 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
966 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
967 }
[email protected]94f206c12012-08-25 00:09:14968}
969
[email protected]c1bb5af2013-03-13 19:06:27970bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
971 const RenderPassDrawQuad& quad, const FrameData& frame) const {
972 const RenderPass* render_pass =
973 FindRenderPassById(quad.render_pass_id, frame);
974 if (!render_pass)
975 return false;
[email protected]94f206c12012-08-25 00:09:14976
[email protected]c1bb5af2013-03-13 19:06:27977 // If any quad or RenderPass draws into this RenderPass, then keep it.
978 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:29979 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
980 quad_list.BackToFrontBegin();
981 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:27982 ++quad_list_iterator) {
983 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:14984
[email protected]c1bb5af2013-03-13 19:06:27985 if (current_quad->material != DrawQuad::RENDER_PASS)
986 return false;
[email protected]94f206c12012-08-25 00:09:14987
[email protected]c1bb5af2013-03-13 19:06:27988 const RenderPass* contributing_pass = FindRenderPassById(
989 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
990 if (contributing_pass)
991 return false;
992 }
993 return true;
[email protected]94f206c12012-08-25 00:09:14994}
995
996// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:27997template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:27998 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
999 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141000
1001// static
[email protected]c1bb5af2013-03-13 19:06:271002template <typename RenderPassCuller>
1003void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1004 FrameData* frame) {
1005 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1006 it != culler.RenderPassListEnd(frame->render_passes);
1007 it = culler.RenderPassListNext(it)) {
1008 const RenderPass* current_pass = frame->render_passes[it];
1009 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291010 QuadList::ConstBackToFrontIterator quad_list_iterator =
1011 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141012
[email protected]ed511b8d2013-03-25 03:29:291013 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271014 ++quad_list_iterator) {
1015 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141016
[email protected]c1bb5af2013-03-13 19:06:271017 if (current_quad->material != DrawQuad::RENDER_PASS)
1018 continue;
[email protected]94f206c12012-08-25 00:09:141019
[email protected]c1bb5af2013-03-13 19:06:271020 const RenderPassDrawQuad* render_pass_quad =
1021 RenderPassDrawQuad::MaterialCast(current_quad);
1022 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1023 continue;
[email protected]94f206c12012-08-25 00:09:141024
[email protected]c1bb5af2013-03-13 19:06:271025 // We are changing the vector in the middle of iteration. Because we
1026 // delete render passes that draw into the current pass, we are
1027 // guaranteed that any data from the iterator to the end will not
1028 // change. So, capture the iterator position from the end of the
1029 // list, and restore it after the change.
1030 size_t position_from_end = frame->render_passes.size() - it;
1031 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1032 it = frame->render_passes.size() - position_from_end;
1033 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141034 }
[email protected]c1bb5af2013-03-13 19:06:271035 }
[email protected]94f206c12012-08-25 00:09:141036}
1037
[email protected]e0341352013-04-06 05:01:201038bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame,
1039 gfx::Rect device_viewport_damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471040 TRACE_EVENT1("cc",
1041 "LayerTreeHostImpl::PrepareToDraw",
1042 "SourceFrameNumber",
1043 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141044
[email protected]425e5332013-09-21 04:10:031045 if (need_to_update_visible_tiles_before_draw_ &&
1046 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1047 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191048 }
[email protected]425e5332013-09-21 04:10:031049 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191050
[email protected]7d19dc342013-05-02 22:02:041051 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271052
[email protected]c1bb5af2013-03-13 19:06:271053 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1054 frame->render_passes.clear();
1055 frame->render_passes_by_id.clear();
1056 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201057 frame->contains_incomplete_tile = false;
1058 frame->has_no_damage = false;
1059
1060 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021061 device_viewport_damage_rect.Union(viewport_damage_rect_);
1062 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201063
1064 active_tree_->root_layer()->render_surface()->damage_tracker()->
1065 AddDamageNextUpdate(device_viewport_damage_rect);
1066 }
[email protected]94f206c12012-08-25 00:09:141067
[email protected]6133cc232013-07-30 18:47:071068 if (!CalculateRenderPasses(frame)) {
1069 DCHECK(!output_surface_->capabilities()
1070 .draw_and_swap_full_viewport_every_frame);
[email protected]c1bb5af2013-03-13 19:06:271071 return false;
[email protected]6133cc232013-07-30 18:47:071072 }
[email protected]94f206c12012-08-25 00:09:141073
[email protected]c1bb5af2013-03-13 19:06:271074 // If we return true, then we expect DrawLayers() to be called before this
1075 // function is called again.
1076 return true;
[email protected]94f206c12012-08-25 00:09:141077}
1078
[email protected]0309ba5e02013-06-26 04:11:081079void LayerTreeHostImpl::EvictTexturesForTesting() {
1080 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1081}
1082
[email protected]4f48f6e2013-08-27 06:33:381083void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1084 NOTREACHED();
1085}
1086
[email protected]425e5332013-09-21 04:10:031087void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1088 DidInitializeVisibleTile();
1089}
1090
[email protected]c1bb5af2013-03-13 19:06:271091void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1092 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121093
[email protected]c1bb5af2013-03-13 19:06:271094 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351095 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351096 ManagedMemoryPolicy::PriorityCutoffToValue(
1097 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351098 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271099 if (evicted_resources) {
1100 active_tree_->SetContentsTexturesPurged();
1101 if (pending_tree_)
1102 pending_tree_->SetContentsTexturesPurged();
1103 client_->SetNeedsCommitOnImplThread();
1104 client_->OnCanDrawStateChanged(CanDraw());
1105 client_->RenewTreePriority();
1106 }
1107 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431108
[email protected]8be1a9bf2013-05-01 03:45:191109 UpdateTileManagerMemoryPolicy(policy);
1110}
1111
1112void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1113 const ManagedMemoryPolicy& policy) {
1114 if (!tile_manager_)
1115 return;
1116
[email protected]8be1a9bf2013-05-01 03:45:191117 // TODO(reveman): We should avoid keeping around unused resources if
1118 // possible. crbug.com/224475
[email protected]1bcced22013-09-24 13:51:191119 global_tile_state_.memory_limit_in_bytes =
1120 visible_ ?
[email protected]0b8ef2842013-11-01 09:53:351121 policy.bytes_limit_when_visible : 0;
[email protected]1bcced22013-09-24 13:51:191122 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
1123 (static_cast<int64>(global_tile_state_.memory_limit_in_bytes) *
[email protected]8be1a9bf2013-05-01 03:45:191124 settings_.max_unused_resource_memory_percentage) / 100);
[email protected]1bcced22013-09-24 13:51:191125 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191126 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1127 visible_ ?
1128 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351129 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191130 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081131
[email protected]c48536a52013-09-14 00:02:081132 DidModifyTilePriorities();
1133}
1134
1135void LayerTreeHostImpl::DidModifyTilePriorities() {
1136 DCHECK(settings_.impl_side_painting);
1137 // Mark priorities as dirty and schedule a ManageTiles().
1138 tile_priorities_dirty_ = true;
1139 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141140}
1141
[email protected]86126792013-03-16 20:07:541142void LayerTreeHostImpl::DidInitializeVisibleTile() {
1143 // TODO(reveman): Determine tiles that changed and only damage
1144 // what's necessary.
1145 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031146 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541147 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471148}
1149
[email protected]94bf75c2013-06-12 13:20:041150void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381151 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041152}
1153
[email protected]3f2ff112013-08-03 02:41:071154void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1155 SetManagedMemoryPolicy(policy, zero_budget_);
1156}
1157
[email protected]5f07afc2013-07-19 05:17:181158void LayerTreeHostImpl::SetTreeActivationCallback(
1159 const base::Closure& callback) {
1160 DCHECK(proxy_->IsImplThread());
1161 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261162 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181163}
1164
[email protected]c1bb5af2013-03-13 19:06:271165void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071166 const ManagedMemoryPolicy& policy, bool zero_budget) {
1167 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271168 return;
[email protected]61de5812012-11-08 07:03:441169
[email protected]3f2ff112013-08-03 02:41:071170 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1171
1172 cached_managed_memory_policy_ = policy;
1173 zero_budget_ = zero_budget;
1174 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1175
1176 if (old_policy == actual_policy)
1177 return;
1178
1179 if (!proxy_->HasImplThread()) {
1180 // In single-thread mode, this can be called on the main thread by
1181 // GLRenderer::OnMemoryAllocationChanged.
1182 DebugScopedSetImplThread impl_thread(proxy_);
1183 EnforceManagedMemoryPolicy(actual_policy);
1184 } else {
1185 DCHECK(proxy_->IsImplThread());
1186 EnforceManagedMemoryPolicy(actual_policy);
1187 }
[email protected]50644642013-06-20 13:58:551188
[email protected]d7626ffd2013-03-29 00:17:421189 // If there is already enough memory to draw everything imaginable and the
1190 // new memory limit does not change this, then do not re-commit. Don't bother
1191 // skipping commits if this is not visible (commits don't happen when not
1192 // visible, there will almost always be a commit when this becomes visible).
1193 bool needs_commit = true;
1194 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071195 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1196 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1197 actual_policy.priority_cutoff_when_visible ==
1198 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421199 needs_commit = false;
1200 }
1201
[email protected]d7626ffd2013-03-29 00:17:421202 if (needs_commit)
1203 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141204}
1205
[email protected]f224cc92013-06-06 23:23:321206void LayerTreeHostImpl::SetExternalDrawConstraints(
1207 const gfx::Transform& transform,
[email protected]54af03522013-09-05 00:43:281208 gfx::Rect viewport,
1209 gfx::Rect clip,
1210 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321211 external_transform_ = transform;
1212 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281213 external_clip_ = clip;
1214 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321215}
1216
[email protected]1cd9f5552013-04-26 04:22:031217void LayerTreeHostImpl::SetNeedsRedrawRect(gfx::Rect damage_rect) {
1218 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1219}
1220
[email protected]daea3d42013-10-23 17:04:501221void LayerTreeHostImpl::BeginImplFrame(const BeginFrameArgs& args) {
1222 client_->BeginImplFrame(args);
[email protected]7ed47512013-03-26 22:28:451223}
1224
[email protected]4d7e46a2013-11-08 05:33:401225void LayerTreeHostImpl::DidSwapBuffers() {
1226 client_->DidSwapBuffersOnImplThread();
1227}
1228
[email protected]a7335e0b2013-09-18 09:34:511229void LayerTreeHostImpl::OnSwapBuffersComplete() {
1230 client_->OnSwapBuffersCompleteOnImplThread();
1231}
1232
1233void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271234 // TODO(piman): We may need to do some validation on this ack before
1235 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511236 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291237 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]a46f32932012-12-07 21:43:161238}
1239
[email protected]c1bb5af2013-03-13 19:06:271240void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1241 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291242}
1243
[email protected]c1bb5af2013-03-13 19:06:271244CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1245 CompositorFrameMetadata metadata;
1246 metadata.device_scale_factor = device_scale_factor_;
1247 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1248 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1249 metadata.root_layer_size = active_tree_->ScrollableSize();
1250 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1251 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
[email protected]6a50af12013-06-14 08:47:501252 metadata.latency_info = active_tree_->GetLatencyInfo();
[email protected]c1bb5af2013-03-13 19:06:271253 if (top_controls_manager_) {
1254 metadata.location_bar_offset =
1255 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1256 metadata.location_bar_content_translation =
1257 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011258 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271259 }
[email protected]bf189f62012-12-18 03:42:111260
[email protected]c1bb5af2013-03-13 19:06:271261 if (!RootScrollLayer())
[email protected]bf189f62012-12-18 03:42:111262 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271263
[email protected]ffb2720f2013-03-15 19:18:371264 metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271265
1266 return metadata;
[email protected]bf189f62012-12-18 03:42:111267}
1268
[email protected]6f50b8fa2013-07-01 19:51:491269static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1270 layer->DidBeginTracing();
1271}
[email protected]37349bc2013-06-04 01:31:521272
[email protected]f0c2a242013-03-15 19:34:521273void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1274 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351275 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271276 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201277
[email protected]3519b872013-07-30 07:17:501278 if (frame->has_no_damage) {
1279 TRACE_EVENT0("cc", "EarlyOut_NoDamage");
[email protected]6133cc232013-07-30 18:47:071280 DCHECK(!output_surface_->capabilities()
1281 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201282 return;
[email protected]3519b872013-07-30 07:17:501283 }
[email protected]e0341352013-04-06 05:01:201284
[email protected]c1bb5af2013-03-13 19:06:271285 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141286
[email protected]213fcfc2013-08-24 14:59:271287 fps_counter_->SaveTimeStamp(frame_begin_time,
1288 !output_surface_->context_provider());
[email protected]94f206c12012-08-25 00:09:141289
[email protected]c1e6cc062013-08-24 03:35:351290 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091291 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351292 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431293
[email protected]c1bb5af2013-03-13 19:06:271294 if (tile_manager_) {
1295 memory_history_->SaveEntry(
1296 tile_manager_->memory_stats_from_last_assign());
1297 }
[email protected]1191d9d2013-02-02 06:00:331298
[email protected]846f455b2013-03-18 19:07:411299 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021300 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271301 active_tree_->root_layer(),
1302 *frame->render_surface_layer_list,
1303 frame->occluding_screen_space_rects,
1304 frame->non_occluding_screen_space_rects,
1305 debug_state_);
1306 }
[email protected]94f206c12012-08-25 00:09:141307
[email protected]a848c102013-03-26 08:59:091308 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1309 const RenderingStats& stats =
1310 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181311 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091312 }
1313
[email protected]37349bc2013-06-04 01:31:521314 bool is_new_trace;
1315 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1316 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491317 if (pending_tree_) {
1318 LayerTreeHostCommon::CallFunctionForSubtree(
1319 pending_tree_->root_layer(),
1320 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1321 }
1322 LayerTreeHostCommon::CallFunctionForSubtree(
1323 active_tree_->root_layer(),
1324 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521325 }
1326
[email protected]9197dbcb2013-05-15 20:28:511327 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211328 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1329 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1330 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081331
[email protected]c1bb5af2013-03-13 19:06:271332 // Because the contents of the HUD depend on everything else in the frame, the
1333 // contents of its texture are updated as the last thing before the frame is
1334 // drawn.
[email protected]671f6672013-10-01 07:31:071335 if (active_tree_->hud_layer()) {
1336 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531337 active_tree_->hud_layer()->UpdateHudTexture(
1338 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071339 }
[email protected]94f206c12012-08-25 00:09:141340
[email protected]b09c1942013-05-10 00:06:371341 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]573e8e32013-09-09 17:24:321342 bool allow_partial_swap = false;
[email protected]251699b2013-10-09 00:21:261343 bool disable_picture_quad_image_filtering =
1344 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321345
[email protected]b09c1942013-05-10 00:06:371346 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321347 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
1348 temp_software_renderer->DrawFrame(
[email protected]251699b2013-10-09 00:21:261349 &frame->render_passes, NULL, device_scale_factor_, allow_partial_swap,
1350 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371351 } else {
[email protected]573e8e32013-09-09 17:24:321352 // We don't track damage on the HUD layer (it interacts with damage tracking
1353 // visualizations), so disable partial swaps to make the HUD layer display
1354 // properly.
1355 bool allow_partial_swap = !debug_state_.ShowHudRects();
1356
[email protected]b5174d712013-08-28 08:10:431357 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321358 offscreen_context_provider_.get(),
1359 device_scale_factor_,
[email protected]251699b2013-10-09 00:21:261360 allow_partial_swap,
1361 false);
[email protected]b09c1942013-05-10 00:06:371362 }
[email protected]c1bb5af2013-03-13 19:06:271363 // The render passes should be consumed by the renderer.
1364 DCHECK(frame->render_passes.empty());
1365 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141366
[email protected]c1bb5af2013-03-13 19:06:271367 // The next frame should start by assuming nothing has changed, and changes
1368 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321369 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1370 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541371 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271372 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1373 DidDrawDamagedArea();
1374 }
1375 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351376
[email protected]adbe30f2013-10-11 21:12:331377 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1378 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181379 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141380}
1381
[email protected]c1bb5af2013-03-13 19:06:271382void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1383 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1384 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521385
[email protected]c1bb5af2013-03-13 19:06:271386 // Once all layers have been drawn, pending texture uploads should no
1387 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151388 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141389}
1390
[email protected]c1bb5af2013-03-13 19:06:271391void LayerTreeHostImpl::FinishAllRendering() {
1392 if (renderer_)
1393 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141394}
1395
[email protected]c1bb5af2013-03-13 19:06:271396bool LayerTreeHostImpl::IsContextLost() {
1397 DCHECK(proxy_->IsImplThread());
1398 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141399}
1400
[email protected]c1bb5af2013-03-13 19:06:271401const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const {
1402 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141403}
1404
[email protected]e0341352013-04-06 05:01:201405bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
1406 if (frame.has_no_damage)
1407 return false;
[email protected]36e5ff12013-06-11 12:19:291408 renderer_->SwapBuffers();
[email protected]c5c506382013-04-30 04:42:161409 active_tree_->ClearLatencyInfo();
1410 return true;
[email protected]94f206c12012-08-25 00:09:141411}
1412
[email protected]daea3d42013-10-23 17:04:501413void LayerTreeHostImpl::SetNeedsBeginImplFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451414 if (output_surface_)
[email protected]daea3d42013-10-23 17:04:501415 output_surface_->SetNeedsBeginImplFrame(enable);
[email protected]7ed47512013-03-26 22:28:451416}
1417
[email protected]54af03522013-09-05 00:43:281418gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341419 // Use the root container layer bounds if it clips to them, otherwise, the
1420 // true viewport size should be used.
[email protected]4f0f43a2013-07-31 17:44:441421 LayerImpl* container_layer = active_tree_->RootContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341422 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441423 DCHECK(!top_controls_manager_);
1424 DCHECK_EQ(0, overdraw_bottom_height_);
1425 return container_layer->bounds();
1426 }
1427
[email protected]ffb2720f2013-03-15 19:18:371428 gfx::SizeF dip_size =
[email protected]54af03522013-09-05 00:43:281429 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
[email protected]ffb2720f2013-03-15 19:18:371430
[email protected]ed511b8d2013-03-25 03:29:291431 float top_offset =
[email protected]ffb2720f2013-03-15 19:18:371432 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
[email protected]d9083762013-03-24 01:36:401433 return gfx::SizeF(dip_size.width(),
[email protected]ed511b8d2013-03-25 03:29:291434 dip_size.height() - top_offset - overdraw_bottom_height_);
[email protected]ffb2720f2013-03-15 19:18:371435}
1436
[email protected]c1bb5af2013-03-13 19:06:271437void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]bb1e2822013-04-17 22:06:011438 // TODO(jamesr): The renderer_ check is needed to make some of the
1439 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1440 // important) in production. We should adjust the test to not need this.
1441 if (renderer_)
1442 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]94f206c12012-08-25 00:09:141443}
1444
[email protected]c1bb5af2013-03-13 19:06:271445void LayerTreeHostImpl::Readback(void* pixels,
1446 gfx::Rect rect_in_device_viewport) {
1447 DCHECK(renderer_);
1448 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141449}
1450
[email protected]59adb112013-04-09 04:48:441451bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]3209161d2013-03-29 19:17:341452 return !!RootScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011453}
1454
[email protected]c1bb5af2013-03-13 19:06:271455LayerImpl* LayerTreeHostImpl::RootLayer() const {
1456 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081457}
1458
[email protected]c1bb5af2013-03-13 19:06:271459LayerImpl* LayerTreeHostImpl::RootScrollLayer() const {
1460 return active_tree_->RootScrollLayer();
[email protected]8bef40572012-12-11 21:38:081461}
1462
[email protected]c1bb5af2013-03-13 19:06:271463LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1464 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081465}
1466
[email protected]251699b2013-10-09 00:21:261467bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1468 return CurrentlyScrollingLayer() ||
1469 (RootScrollLayer() && RootScrollLayer()->IsExternalFlingActive());
1470}
1471
[email protected]94f206c12012-08-25 00:09:141472// Content layers can be either directly scrollable or contained in an outer
1473// scrolling layer which applies the scroll transform. Given a content layer,
1474// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271475static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1476 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021477 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271478
1479 if (layer_impl->scrollable())
1480 return layer_impl;
1481
1482 if (layer_impl->DrawsContent() &&
1483 layer_impl->parent() &&
1484 layer_impl->parent()->scrollable())
1485 return layer_impl->parent();
1486
[email protected]bf1cfd9a2013-09-26 05:43:021487 return NULL;
[email protected]94f206c12012-08-25 00:09:141488}
1489
[email protected]c1bb5af2013-03-13 19:06:271490void LayerTreeHostImpl::CreatePendingTree() {
1491 CHECK(!pending_tree_);
1492 if (recycle_tree_)
1493 recycle_tree_.swap(pending_tree_);
1494 else
1495 pending_tree_ = LayerTreeImpl::create(this);
1496 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111497 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271498}
1499
[email protected]8bb6cfa2013-07-23 00:11:191500void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191501 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1502 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191503 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481504}
1505
[email protected]c1bb5af2013-03-13 19:06:271506void LayerTreeHostImpl::ActivatePendingTree() {
1507 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111508 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351509
[email protected]4f48f6e2013-08-27 06:33:381510 need_to_update_visible_tiles_before_draw_ = true;
1511
[email protected]1960a712013-04-30 17:06:471512 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271513 active_tree_->PushPersistedState(pending_tree_.get());
1514 if (pending_tree_->needs_full_tree_sync()) {
1515 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331516 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271517 active_tree_->DetachLayerTree(),
1518 active_tree_.get()));
1519 }
[email protected]b5651c22013-03-14 15:06:331520 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271521 active_tree_->root_layer());
1522 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511523
[email protected]c9280762013-08-01 06:28:571524 // Process any requests in the UI resource queue. The request queue is given
1525 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1526 // the swap.
1527 pending_tree_->ProcessUIResourceRequestQueue();
1528
[email protected]c1bb5af2013-03-13 19:06:271529 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511530
[email protected]c1bb5af2013-03-13 19:06:271531 // Now that we've synced everything from the pending tree to the active
1532 // tree, rename the pending tree the recycle tree so we can reuse it on the
1533 // next sync.
1534 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511535
[email protected]7d1b07e2013-10-01 17:31:301536 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471537 active_tree_->SetRootLayerScrollOffsetDelegate(
1538 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221539
[email protected]c1bb5af2013-03-13 19:06:271540 client_->OnCanDrawStateChanged(CanDraw());
[email protected]c1bb5af2013-03-13 19:06:271541 client_->SetNeedsRedrawOnImplThread();
1542 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241543
[email protected]a848c102013-03-26 08:59:091544 if (debug_state_.continuous_painting) {
1545 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431546 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091547 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1548 stats.main_stats.record_time +
1549 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271550 }
[email protected]2a61ad52013-05-13 14:01:291551
1552 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261553 if (!tree_activation_callback_.is_null())
1554 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011555
1556 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1557 DCHECK(active_tree_->root_layer());
[email protected]2e7ca422012-12-20 02:57:271558}
1559
[email protected]c1bb5af2013-03-13 19:06:271560void LayerTreeHostImpl::SetVisible(bool visible) {
1561 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141562
[email protected]c1bb5af2013-03-13 19:06:271563 if (visible_ == visible)
1564 return;
1565 visible_ = visible;
1566 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121567 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141568
[email protected]127bdc1a2013-09-11 01:44:481569 if (!visible_)
1570 EvictAllUIResources();
1571
[email protected]0fa42e02013-08-20 01:16:051572 // Evict tiles immediately if invisible since this tab may never get another
1573 // draw or timer tick.
1574 if (!visible_)
1575 ManageTiles();
1576
[email protected]c1bb5af2013-03-13 19:06:271577 if (!renderer_)
1578 return;
[email protected]94f206c12012-08-25 00:09:141579
[email protected]c1bb5af2013-03-13 19:06:271580 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141581}
1582
[email protected]206a3922013-05-17 06:34:121583ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071584 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
1585 if (debug_state_.rasterize_only_visible_content) {
[email protected]3f2ff112013-08-03 02:41:071586 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291587 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071588 }
[email protected]206a3922013-05-17 06:34:121589
[email protected]3f2ff112013-08-03 02:41:071590 if (zero_budget_) {
1591 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071592 }
1593
[email protected]206a3922013-05-17 06:34:121594 return actual;
1595}
1596
[email protected]3f2ff112013-08-03 02:41:071597size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1598 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1599}
1600
[email protected]990e050a2013-09-23 18:50:211601int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1602 return ManagedMemoryPolicy::PriorityCutoffToValue(
1603 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1604}
1605
[email protected]50644642013-06-20 13:58:551606void LayerTreeHostImpl::ReleaseTreeResources() {
1607 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551608 SendReleaseResourcesRecursive(active_tree_->root_layer());
1609 if (pending_tree_ && pending_tree_->root_layer())
1610 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1611 if (recycle_tree_ && recycle_tree_->root_layer())
1612 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571613
[email protected]127bdc1a2013-09-11 01:44:481614 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551615}
1616
1617void LayerTreeHostImpl::CreateAndSetRenderer(
1618 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201619 ResourceProvider* resource_provider,
1620 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551621 DCHECK(!renderer_);
1622 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321623 renderer_ = DelegatingRenderer::Create(
1624 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091625 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551626 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321627 &settings_,
[email protected]50644642013-06-20 13:58:551628 output_surface,
1629 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111630 texture_mailbox_deleter_.get(),
[email protected]50644642013-06-20 13:58:551631 settings_.highp_threshold_min,
1632 settings_.force_direct_layer_drawing);
1633 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321634 renderer_ = SoftwareRenderer::Create(
1635 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551636 }
1637
[email protected]84975832013-06-29 00:24:111638 if (renderer_) {
[email protected]50644642013-06-20 13:58:551639 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111640 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561641
1642 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1643 // initialized to get max texture size. Also, after releasing resources,
1644 // trees need another update to generate new ones.
1645 active_tree_->set_needs_update_draw_properties();
1646 if (pending_tree_)
1647 pending_tree_->set_needs_update_draw_properties();
[email protected]84975832013-06-29 00:24:111648 }
[email protected]50644642013-06-20 13:58:551649}
1650
[email protected]50af3c22013-07-13 03:50:201651void LayerTreeHostImpl::CreateAndSetTileManager(
1652 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011653 ContextProvider* context_provider,
[email protected]50af3c22013-07-13 03:50:201654 bool using_map_image) {
1655 DCHECK(settings_.impl_side_painting);
1656 DCHECK(resource_provider);
[email protected]b6eb8e332013-09-10 00:51:011657 tile_manager_ =
1658 TileManager::Create(this,
1659 resource_provider,
1660 settings_.num_raster_threads,
1661 rendering_stats_instrumentation_,
1662 using_map_image,
[email protected]b4664142013-11-08 00:50:311663 GetMaxTransferBufferUsageBytes(context_provider),
1664 GetMaxRasterTasksUsageBytes(context_provider));
[email protected]b6eb8e332013-09-10 00:51:011665
[email protected]50af3c22013-07-13 03:50:201666 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191667 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201668}
1669
[email protected]50644642013-06-20 13:58:551670void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071671 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551672}
1673
[email protected]c1bb5af2013-03-13 19:06:271674bool LayerTreeHostImpl::InitializeRenderer(
1675 scoped_ptr<OutputSurface> output_surface) {
1676 // Since we will create a new resource provider, we cannot continue to use
1677 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1678 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551679 ReleaseTreeResources();
[email protected]7ba3ca72013-04-11 06:37:251680 if (resource_provider_)
1681 resource_provider_->DidLoseOutputSurface();
[email protected]45c4b1e2013-01-16 02:19:401682
[email protected]c1bb5af2013-03-13 19:06:271683 // Note: order is important here.
1684 renderer_.reset();
1685 tile_manager_.reset();
1686 resource_provider_.reset();
1687 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141688
[email protected]c1bb5af2013-03-13 19:06:271689 if (!output_surface->BindToClient(this))
1690 return false;
[email protected]be3181652012-09-25 13:02:131691
[email protected]a7f35682013-10-22 23:05:571692 scoped_ptr<ResourceProvider> resource_provider =
1693 ResourceProvider::Create(output_surface.get(),
1694 shared_bitmap_manager_,
1695 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341696 settings_.use_rgba_4444_textures,
1697 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551698 if (!resource_provider)
1699 return false;
[email protected]2b154b22013-06-07 09:03:271700
[email protected]50644642013-06-20 13:58:551701 if (output_surface->capabilities().deferred_gl_initialization)
1702 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131703
[email protected]50af3c22013-07-13 03:50:201704 bool skip_gl_renderer = false;
1705 CreateAndSetRenderer(
1706 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131707
[email protected]50644642013-06-20 13:58:551708 if (!renderer_)
1709 return false;
[email protected]6123930c2013-05-23 06:48:371710
[email protected]50644642013-06-20 13:58:551711 if (settings_.impl_side_painting) {
[email protected]50af3c22013-07-13 03:50:201712 CreateAndSetTileManager(resource_provider.get(),
[email protected]b6eb8e332013-09-10 00:51:011713 output_surface->context_provider().get(),
[email protected]50af3c22013-07-13 03:50:201714 GetRendererCapabilities().using_map_image);
[email protected]fbe89f72013-05-21 07:24:241715 }
1716
[email protected]daea3d42013-10-23 17:04:501717 // Setup BeginImplFrameEmulation if it's not supported natively
1718 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351719 const base::TimeDelta display_refresh_interval =
1720 base::TimeDelta::FromMicroseconds(
1721 base::Time::kMicrosecondsPerSecond /
1722 settings_.refresh_rate);
1723
[email protected]daea3d42013-10-23 17:04:501724 output_surface->InitializeBeginImplFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151725 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351726 settings_.throttle_frame_production,
1727 display_refresh_interval);
1728 }
1729
1730 int max_frames_pending =
1731 output_surface->capabilities().max_frames_pending;
1732 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351733 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351734 output_surface->SetMaxFramesPending(max_frames_pending);
1735
[email protected]50644642013-06-20 13:58:551736 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271737 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141738
[email protected]c1bb5af2013-03-13 19:06:271739 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211740
[email protected]c1bb5af2013-03-13 19:06:271741 return true;
[email protected]94f206c12012-08-25 00:09:141742}
1743
[email protected]2b154b22013-06-07 09:03:271744bool LayerTreeHostImpl::DeferredInitialize(
1745 scoped_refptr<ContextProvider> offscreen_context_provider) {
1746 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551747 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091748 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271749
[email protected]50644642013-06-20 13:58:551750 ReleaseTreeResources();
1751 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271752
[email protected]d9ca99e2013-08-28 21:49:481753 bool resource_provider_success = resource_provider_->InitializeGL();
1754
1755 bool success = resource_provider_success;
1756 if (success) {
1757 bool skip_gl_renderer = false;
1758 CreateAndSetRenderer(
1759 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1760 if (!renderer_)
1761 success = false;
1762 }
1763
1764 if (success) {
1765 if (offscreen_context_provider.get() &&
1766 !offscreen_context_provider->BindToCurrentThread())
1767 success = false;
1768 }
1769
[email protected]50644642013-06-20 13:58:551770 if (success) {
1771 EnforceZeroBudget(false);
1772 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481773 } else {
1774 if (offscreen_context_provider.get()) {
1775 if (offscreen_context_provider->BindToCurrentThread())
1776 offscreen_context_provider->VerifyContexts();
1777 offscreen_context_provider = NULL;
1778 }
1779
1780 client_->DidLoseOutputSurfaceOnImplThread();
1781
1782 if (resource_provider_success) {
1783 // If this fails the context provider will be dropped from the output
1784 // surface and destroyed. But the GLRenderer expects the output surface
1785 // to stick around - and hold onto the context3d - as long as it is alive.
1786 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1787 renderer_.reset();
1788
1789 // The resource provider can't stay in GL mode or it tries to clean up GL
1790 // stuff, but the context provider is going away on the output surface
1791 // which contradicts being in GL mode.
1792 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1793 resource_provider_->InitializeSoftware();
1794 }
[email protected]50644642013-06-20 13:58:551795 }
[email protected]b5174d712013-08-28 08:10:431796
1797 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271798 return success;
1799}
1800
[email protected]50af3c22013-07-13 03:50:201801void LayerTreeHostImpl::ReleaseGL() {
1802 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1803 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091804 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201805
1806 ReleaseTreeResources();
1807 renderer_.reset();
1808 tile_manager_.reset();
1809 resource_provider_->InitializeSoftware();
1810
1811 bool skip_gl_renderer = true;
1812 CreateAndSetRenderer(
1813 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1814 DCHECK(renderer_);
1815
1816 EnforceZeroBudget(true);
1817 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011818 NULL,
[email protected]50af3c22013-07-13 03:50:201819 GetRendererCapabilities().using_map_image);
1820 DCHECK(tile_manager_);
1821
[email protected]b5174d712013-08-28 08:10:431822 SetOffscreenContextProvider(NULL);
1823
[email protected]50af3c22013-07-13 03:50:201824 client_->SetNeedsCommitOnImplThread();
1825}
1826
[email protected]18ce59702013-04-09 04:58:401827void LayerTreeHostImpl::SetViewportSize(gfx::Size device_viewport_size) {
1828 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:271829 return;
[email protected]94f206c12012-08-25 00:09:141830
[email protected]54af03522013-09-05 00:43:281831 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:271832 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:271833
[email protected]c1bb5af2013-03-13 19:06:271834 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:141835
[email protected]c1bb5af2013-03-13 19:06:271836 UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141837
[email protected]c1bb5af2013-03-13 19:06:271838 if (renderer_)
1839 renderer_->ViewportChanged();
[email protected]8db2213c2012-09-05 22:08:211840
[email protected]c1bb5af2013-03-13 19:06:271841 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:441842 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141843}
1844
[email protected]d9083762013-03-24 01:36:401845void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
1846 if (overdraw_bottom_height == overdraw_bottom_height_)
1847 return;
1848 overdraw_bottom_height_ = overdraw_bottom_height;
1849
1850 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441851 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:401852}
1853
[email protected]9f4f6a32013-09-04 21:35:121854void LayerTreeHostImpl::SetOverhangUIResource(
1855 UIResourceId overhang_ui_resource_id,
1856 gfx::Size overhang_ui_resource_size) {
1857 overhang_ui_resource_id_ = overhang_ui_resource_id;
1858 overhang_ui_resource_size_ = overhang_ui_resource_size;
1859}
1860
[email protected]c1bb5af2013-03-13 19:06:271861void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
1862 if (device_scale_factor == device_scale_factor_)
1863 return;
1864 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:271865
[email protected]94c40e6292013-05-24 22:01:501866 if (renderer_)
1867 renderer_->ViewportChanged();
1868
[email protected]c1bb5af2013-03-13 19:06:271869 UpdateMaxScrollOffset();
[email protected]59adb112013-04-09 04:48:441870 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141871}
1872
[email protected]54af03522013-09-05 00:43:281873gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
1874 return DeviceViewport().size();
1875}
1876
[email protected]f224cc92013-06-06 23:23:321877gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
1878 if (external_viewport_.IsEmpty())
1879 return gfx::Rect(device_viewport_size_);
1880
1881 return external_viewport_;
1882}
1883
[email protected]54af03522013-09-05 00:43:281884gfx::Rect LayerTreeHostImpl::DeviceClip() const {
1885 if (external_clip_.IsEmpty())
1886 return DeviceViewport();
1887
1888 return external_clip_;
1889}
1890
1891const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:321892 return external_transform_;
1893}
1894
[email protected]c1bb5af2013-03-13 19:06:271895void LayerTreeHostImpl::UpdateMaxScrollOffset() {
1896 active_tree_->UpdateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141897}
1898
[email protected]59adb112013-04-09 04:48:441899void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]c1bb5af2013-03-13 19:06:271900 client_->SetNeedsRedrawOnImplThread();
[email protected]59adb112013-04-09 04:48:441901 active_tree_->set_needs_update_draw_properties();
1902 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:141903}
1904
[email protected]c1bb5af2013-03-13 19:06:271905bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:041906 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:341907 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:141908}
1909
[email protected]200a9c062013-05-20 04:34:371910void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
1911 DCHECK(input_handler_client_ == NULL);
1912 input_handler_client_ = client;
1913}
1914
[email protected]420fdf6e2013-08-26 20:36:381915static LayerImpl* NextScrollLayer(LayerImpl* layer) {
1916 if (LayerImpl* scroll_parent = layer->scroll_parent())
1917 return scroll_parent;
1918 return layer->parent();
1919}
1920
[email protected]f620b0e72013-10-01 21:38:241921LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
1922 gfx::PointF device_viewport_point, InputHandler::ScrollInputType type,
[email protected]59a7d552013-10-22 03:36:431923 LayerImpl* layer_impl, bool* scroll_on_main_thread) const {
[email protected]bf1cfd9a2013-09-26 05:43:021924 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:141925
[email protected]c1bb5af2013-03-13 19:06:271926 // Walk up the hierarchy and look for a scrollable layer.
1927 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:381928 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:271929 // The content layer can also block attempts to scroll outside the main
1930 // thread.
1931 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
1932 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021933 *scroll_on_main_thread = true;
1934 return NULL;
[email protected]94f206c12012-08-25 00:09:141935 }
1936
[email protected]c1bb5af2013-03-13 19:06:271937 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
1938 if (!scroll_layer_impl)
1939 continue;
[email protected]94f206c12012-08-25 00:09:141940
[email protected]c1bb5af2013-03-13 19:06:271941 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:271942 // If any layer wants to divert the scroll event to the main thread, abort.
1943 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:021944 *scroll_on_main_thread = true;
1945 return NULL;
[email protected]94f206c12012-08-25 00:09:141946 }
1947
[email protected]c1bb5af2013-03-13 19:06:271948 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
1949 potentially_scrolling_layer_impl = scroll_layer_impl;
1950 }
1951
1952 // When hiding top controls is enabled and the controls are hidden or
1953 // overlaying the content, force scrolls to be enabled on the root layer to
1954 // allow bringing the top controls back into view.
1955 if (!potentially_scrolling_layer_impl && top_controls_manager_ &&
1956 top_controls_manager_->content_top_offset() !=
[email protected]8e0176d2013-03-21 03:14:521957 settings_.top_controls_height) {
[email protected]c1bb5af2013-03-13 19:06:271958 potentially_scrolling_layer_impl = RootScrollLayer();
1959 }
1960
[email protected]bf1cfd9a2013-09-26 05:43:021961 return potentially_scrolling_layer_impl;
1962}
1963
1964InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
1965 gfx::Point viewport_point, InputHandler::ScrollInputType type) {
1966 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
1967
1968 if (top_controls_manager_)
1969 top_controls_manager_->ScrollBegin();
1970
1971 DCHECK(!CurrentlyScrollingLayer());
1972 ClearCurrentlyScrollingLayer();
1973
1974 if (!EnsureRenderSurfaceLayerList())
1975 return ScrollIgnored;
1976
[email protected]f620b0e72013-10-01 21:38:241977 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
1978 device_scale_factor_);
1979 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
1980 device_viewport_point,
1981 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:021982 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:241983 LayerImpl* potentially_scrolling_layer_impl =
1984 FindScrollLayerForDeviceViewportPoint(device_viewport_point, type,
1985 layer_impl, &scroll_on_main_thread);
[email protected]bf1cfd9a2013-09-26 05:43:021986
1987 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:021988 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
1989 return ScrollOnMainThread;
1990 }
1991
[email protected]34d43432013-09-26 20:04:101992 // If we want to send a DidOverscroll for this scroll it can't be ignored.
1993 if (!potentially_scrolling_layer_impl && settings_.always_overscroll)
1994 potentially_scrolling_layer_impl = RootScrollLayer();
1995
[email protected]c1bb5af2013-03-13 19:06:271996 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:201997 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:271998 potentially_scrolling_layer_impl);
1999 should_bubble_scrolls_ = (type != NonBubblingGesture);
[email protected]d41d7cb92013-10-15 16:08:092000 last_scroll_did_bubble_ = false;
[email protected]c1bb5af2013-03-13 19:06:272001 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272002 client_->RenewTreePriority();
2003 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2004 return ScrollStarted;
2005 }
2006 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142007}
2008
[email protected]c1bb5af2013-03-13 19:06:272009gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2010 LayerImpl* layer_impl,
2011 float scale_from_viewport_to_screen_space,
2012 gfx::PointF viewport_point,
2013 gfx::Vector2dF viewport_delta) {
2014 // Layers with non-invertible screen space transforms should not have passed
2015 // the scroll hit test in the first place.
2016 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2017 gfx::Transform inverse_screen_space_transform(
2018 gfx::Transform::kSkipInitialization);
2019 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2020 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352021 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2022 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272023 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142024
[email protected]c1bb5af2013-03-13 19:06:272025 gfx::PointF screen_space_point =
2026 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142027
[email protected]c1bb5af2013-03-13 19:06:272028 gfx::Vector2dF screen_space_delta = viewport_delta;
2029 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2030
2031 // First project the scroll start and end points to local layer space to find
2032 // the scroll delta in layer coordinates.
2033 bool start_clipped, end_clipped;
2034 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2035 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212036 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272037 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212038 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272039 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212040 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272041 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212042 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272043
2044 // In general scroll point coordinates should not get clipped.
2045 DCHECK(!start_clipped);
2046 DCHECK(!end_clipped);
2047 if (start_clipped || end_clipped)
2048 return gfx::Vector2dF();
2049
2050 // local_start_point and local_end_point are in content space but we want to
2051 // move them to layer space for scrolling.
2052 float width_scale = 1.f / layer_impl->contents_scale_x();
2053 float height_scale = 1.f / layer_impl->contents_scale_y();
2054 local_start_point.Scale(width_scale, height_scale);
2055 local_end_point.Scale(width_scale, height_scale);
2056
2057 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472058 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272059 layer_impl->ScrollBy(local_end_point - local_start_point);
2060
2061 // Get the end point in the layer's content space so we can apply its
2062 // ScreenSpaceTransform.
2063 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472064 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272065 previous_delta;
2066 gfx::PointF actual_local_content_end_point =
2067 gfx::ScalePoint(actual_local_end_point,
2068 1.f / width_scale,
2069 1.f / height_scale);
2070
2071 // Calculate the applied scroll delta in viewport space coordinates.
2072 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212073 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272074 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212075 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272076 DCHECK(!end_clipped);
2077 if (end_clipped)
2078 return gfx::Vector2dF();
2079 gfx::PointF actual_viewport_end_point =
2080 gfx::ScalePoint(actual_screen_space_end_point,
2081 1.f / scale_from_viewport_to_screen_space);
2082 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142083}
2084
[email protected]c1bb5af2013-03-13 19:06:272085static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
2086 gfx::Vector2dF local_delta) {
[email protected]1960a712013-04-30 17:06:472087 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272088 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472089 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272090}
2091
2092bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point,
2093 gfx::Vector2dF scroll_delta) {
2094 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2095 if (!CurrentlyScrollingLayer())
2096 return false;
2097
2098 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532099 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292100 bool did_scroll_x = false;
2101 bool did_scroll_y = false;
[email protected]60b4d252013-03-23 18:49:422102 bool consume_by_top_controls = top_controls_manager_ &&
2103 (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0);
[email protected]d41d7cb92013-10-15 16:08:092104 last_scroll_did_bubble_ = false;
[email protected]a91e4f82013-03-15 06:58:062105
[email protected]c1bb5af2013-03-13 19:06:272106 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2107 layer_impl;
2108 layer_impl = layer_impl->parent()) {
2109 if (!layer_impl->scrollable())
2110 continue;
2111
[email protected]a2b5ded2013-05-20 21:32:532112 if (layer_impl == RootScrollLayer()) {
2113 // Only allow bubble scrolling when the scroll is in the direction to make
2114 // the top controls visible.
2115 if (consume_by_top_controls && layer_impl == RootScrollLayer()) {
2116 pending_delta = top_controls_manager_->ScrollBy(pending_delta);
2117 UpdateMaxScrollOffset();
2118 }
2119 // Track root layer deltas for reporting overscroll.
2120 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422121 }
2122
[email protected]c1bb5af2013-03-13 19:06:272123 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272124 // Gesture events need to be transformed from viewport coordinates to local
2125 // layer coordinates so that the scrolling contents exactly follow the
2126 // user's finger. In contrast, wheel events represent a fixed amount of
2127 // scrolling so we can just apply them directly.
2128 if (!wheel_scrolling_) {
2129 float scale_from_viewport_to_screen_space = device_scale_factor_;
2130 applied_delta =
2131 ScrollLayerWithViewportSpaceDelta(layer_impl,
2132 scale_from_viewport_to_screen_space,
2133 viewport_point, pending_delta);
2134 } else {
2135 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142136 }
[email protected]94f206c12012-08-25 00:09:142137
[email protected]c1bb5af2013-03-13 19:06:272138 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]2bd503f2013-07-23 05:35:292139 float move_threshold = 0.1f;
2140 bool did_move_layer_x = std::abs(applied_delta.x()) > move_threshold;
2141 bool did_move_layer_y = std::abs(applied_delta.y()) > move_threshold;
2142 did_scroll_x |= did_move_layer_x;
2143 did_scroll_y |= did_move_layer_y;
2144 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]d41d7cb92013-10-15 16:08:092145 if (!did_lock_scrolling_layer_)
[email protected]c1bb5af2013-03-13 19:06:272146 continue;
[email protected]d41d7cb92013-10-15 16:08:092147
2148 if (should_bubble_scrolls_) {
2149 last_scroll_did_bubble_ = true;
2150 continue;
2151 }
2152
2153 break;
[email protected]94f206c12012-08-25 00:09:142154 }
[email protected]a2b5ded2013-05-20 21:32:532155
2156 if (layer_impl == RootScrollLayer())
2157 unused_root_delta.Subtract(applied_delta);
2158
[email protected]c1bb5af2013-03-13 19:06:272159 did_lock_scrolling_layer_ = true;
2160 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202161 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272162 break;
[email protected]94f206c12012-08-25 00:09:142163 }
[email protected]94f206c12012-08-25 00:09:142164
[email protected]c1bb5af2013-03-13 19:06:272165 // If the applied delta is within 45 degrees of the input delta, bail out to
2166 // make it easier to scroll just one layer in one direction without
2167 // affecting any of its parents.
2168 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212169 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272170 applied_delta, pending_delta) < angle_threshold) {
2171 pending_delta = gfx::Vector2d();
2172 break;
[email protected]4a23c374c2012-12-08 08:38:552173 }
[email protected]c1bb5af2013-03-13 19:06:272174
2175 // Allow further movement only on an axis perpendicular to the direction in
2176 // which the layer moved.
2177 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212178 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272179
[email protected]a2b5ded2013-05-20 21:32:532180 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272181 break;
2182 }
2183
[email protected]2bd503f2013-07-23 05:35:292184 bool did_scroll = did_scroll_x || did_scroll_y;
[email protected]c1bb5af2013-03-13 19:06:272185 if (did_scroll) {
2186 client_->SetNeedsCommitOnImplThread();
2187 client_->SetNeedsRedrawOnImplThread();
2188 client_->RenewTreePriority();
2189 }
[email protected]a2b5ded2013-05-20 21:32:532190
[email protected]2bd503f2013-07-23 05:35:292191 // Scrolling along an axis resets accumulated root overscroll for that axis.
2192 if (did_scroll_x)
2193 accumulated_root_overscroll_.set_x(0);
2194 if (did_scroll_y)
2195 accumulated_root_overscroll_.set_y(0);
2196
[email protected]a2b5ded2013-05-20 21:32:532197 accumulated_root_overscroll_ += unused_root_delta;
2198 bool did_overscroll = !gfx::ToRoundedVector2d(unused_root_delta).IsZero();
2199 if (did_overscroll && input_handler_client_) {
[email protected]bfb6e242013-07-17 23:15:112200 DidOverscrollParams params;
2201 params.accumulated_overscroll = accumulated_root_overscroll_;
2202 params.latest_overscroll_delta = unused_root_delta;
2203 params.current_fling_velocity = current_fling_velocity_;
2204 input_handler_client_->DidOverscroll(params);
[email protected]a2b5ded2013-05-20 21:32:532205 }
2206
[email protected]c1bb5af2013-03-13 19:06:272207 return did_scroll;
[email protected]4a23c374c2012-12-08 08:38:552208}
2209
[email protected]be782f52013-03-23 21:36:142210// This implements scrolling by page as described here:
2211// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2212// for events with WHEEL_PAGESCROLL set.
[email protected]c28df4c12013-05-22 17:36:492213bool LayerTreeHostImpl::ScrollVerticallyByPage(gfx::Point viewport_point,
2214 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142215 DCHECK(wheel_scrolling_);
2216
2217 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2218 layer_impl;
2219 layer_impl = layer_impl->parent()) {
2220 if (!layer_impl->scrollable())
2221 continue;
2222
2223 if (!layer_impl->vertical_scrollbar_layer())
2224 continue;
2225
2226 float height = layer_impl->vertical_scrollbar_layer()->bounds().height();
2227
2228 // These magical values match WebKit and are designed to scroll nearly the
2229 // entire visible content height but leave a bit of overlap.
2230 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492231 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142232 page = -page;
2233
2234 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2235
2236 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2237
2238 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142239 client_->SetNeedsCommitOnImplThread();
2240 client_->SetNeedsRedrawOnImplThread();
2241 client_->RenewTreePriority();
2242 return true;
2243 }
2244
2245 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2246 }
2247
2248 return false;
2249}
2250
[email protected]1960a712013-04-30 17:06:472251void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2252 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2253 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2254 active_tree_->SetRootLayerScrollOffsetDelegate(
2255 root_layer_scroll_offset_delegate_);
2256}
2257
2258void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2259 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2260 client_->SetNeedsCommitOnImplThread();
2261}
2262
[email protected]c1bb5af2013-03-13 19:06:272263void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2264 active_tree_->ClearCurrentlyScrollingLayer();
2265 did_lock_scrolling_layer_ = false;
[email protected]a2b5ded2013-05-20 21:32:532266 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]6e921bd2013-04-29 21:10:202267 current_fling_velocity_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142268}
2269
[email protected]c1bb5af2013-03-13 19:06:272270void LayerTreeHostImpl::ScrollEnd() {
2271 if (top_controls_manager_)
2272 top_controls_manager_->ScrollEnd();
2273 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052274 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142275}
2276
[email protected]5ff3c9782013-04-29 17:35:122277InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462278 if (!active_tree_->CurrentlyScrollingLayer())
2279 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212280
[email protected]75147f52013-08-02 19:11:462281 if (settings_.ignore_root_layer_flings &&
2282 active_tree_->CurrentlyScrollingLayer() ==
2283 active_tree_->RootScrollLayer()) {
2284 ClearCurrentlyScrollingLayer();
2285 return ScrollIgnored;
2286 }
2287
[email protected]df0c42342013-10-08 20:52:122288 if (!wheel_scrolling_)
[email protected]d41d7cb92013-10-15 16:08:092289 should_bubble_scrolls_ = last_scroll_did_bubble_;
[email protected]df0c42342013-10-08 20:52:122290
[email protected]75147f52013-08-02 19:11:462291 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212292}
2293
[email protected]6e921bd2013-04-29 21:10:202294void LayerTreeHostImpl::NotifyCurrentFlingVelocity(gfx::Vector2dF velocity) {
2295 current_fling_velocity_ = velocity;
2296}
2297
[email protected]bf1cfd9a2013-09-26 05:43:022298float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
2299 gfx::PointF device_viewport_point,
2300 LayerImpl* layer_impl) {
2301 if (!layer_impl)
2302 return std::numeric_limits<float>::max();
2303
2304 gfx::Rect layer_impl_bounds(
2305 layer_impl->content_bounds());
2306
2307 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2308 layer_impl->screen_space_transform(),
2309 layer_impl_bounds);
2310
2311 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2312 device_viewport_point);
2313}
2314
2315void LayerTreeHostImpl::MouseMoveAt(gfx::Point viewport_point) {
2316 if (!EnsureRenderSurfaceLayerList())
2317 return;
2318
[email protected]f620b0e72013-10-01 21:38:242319 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2320 device_scale_factor_);
2321
2322 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2323 device_viewport_point,
2324 active_tree_->RenderSurfaceLayerList());
2325 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2326 return;
2327
2328 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2329 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2330 scroll_layer_id_when_mouse_over_scrollbar_);
2331
2332 ScrollbarAnimationController* animation_controller =
2333 scroll_layer_impl->scrollbar_animation_controller();
2334 if (animation_controller) {
2335 animation_controller->DidMouseMoveOffScrollbar(
2336 CurrentPhysicalTimeTicks());
2337 StartScrollbarAnimation();
2338 }
2339 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2340 }
2341
[email protected]bf1cfd9a2013-09-26 05:43:022342 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242343 LayerImpl* scroll_layer_impl = FindScrollLayerForDeviceViewportPoint(
2344 device_viewport_point, InputHandler::Gesture, layer_impl,
[email protected]bf1cfd9a2013-09-26 05:43:022345 &scroll_on_main_thread);
2346 if (scroll_on_main_thread || !scroll_layer_impl)
2347 return;
2348
2349 ScrollbarAnimationController* animation_controller =
2350 scroll_layer_impl->scrollbar_animation_controller();
2351 if (!animation_controller)
2352 return;
2353
[email protected]bf1cfd9a2013-09-26 05:43:022354 float distance_to_scrollbar = std::min(
2355 DeviceSpaceDistanceToLayer(device_viewport_point,
2356 scroll_layer_impl->horizontal_scrollbar_layer()),
2357 DeviceSpaceDistanceToLayer(device_viewport_point,
2358 scroll_layer_impl->vertical_scrollbar_layer()));
2359
2360 bool should_animate = animation_controller->DidMouseMoveNear(
2361 CurrentPhysicalTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202362 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022363 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022364}
2365
[email protected]f620b0e72013-10-01 21:38:242366bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
2367 gfx::PointF device_viewport_point) {
2368 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2369 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2370 layer_impl = active_tree_->LayerById(scroll_layer_id);
2371 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2372 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202373 bool should_animate =
2374 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
2375 CurrentPhysicalTimeTicks(), 0);
2376 if (should_animate)
2377 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242378 } else {
2379 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2380 }
2381
2382 return true;
2383 }
2384
2385 return false;
2386}
2387
[email protected]c1bb5af2013-03-13 19:06:272388void LayerTreeHostImpl::PinchGestureBegin() {
2389 pinch_gesture_active_ = true;
2390 previous_pinch_anchor_ = gfx::Point();
2391 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132392 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]9ec24df2013-08-09 23:32:572393 active_tree_->SetCurrentlyScrollingLayer(RootScrollLayer());
[email protected]a9bda5e2013-10-25 18:43:372394 if (top_controls_manager_)
2395 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142396}
2397
[email protected]c1bb5af2013-03-13 19:06:272398void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
2399 gfx::Point anchor) {
2400 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282401
[email protected]c1bb5af2013-03-13 19:06:272402 if (!RootScrollLayer())
2403 return;
[email protected]d3afa112012-12-08 06:24:282404
[email protected]c1bb5af2013-03-13 19:06:272405 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2406 // position over the course of the magnify.
2407 float page_scale_delta = active_tree_->page_scale_delta();
2408 gfx::PointF previous_scale_anchor =
2409 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2410 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2411 page_scale_delta = active_tree_->page_scale_delta();
2412 gfx::PointF new_scale_anchor =
2413 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2414 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2415
2416 previous_pinch_anchor_ = anchor;
2417
2418 move.Scale(1 / active_tree_->page_scale_factor());
2419
2420 RootScrollLayer()->ScrollBy(move);
2421
[email protected]c1bb5af2013-03-13 19:06:272422 client_->SetNeedsCommitOnImplThread();
2423 client_->SetNeedsRedrawOnImplThread();
2424 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282425}
2426
[email protected]c1bb5af2013-03-13 19:06:272427void LayerTreeHostImpl::PinchGestureEnd() {
2428 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132429 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2430 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2431 ClearCurrentlyScrollingLayer();
2432 }
[email protected]a9bda5e2013-10-25 18:43:372433 if (top_controls_manager_)
2434 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272435 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142436}
2437
[email protected]c1bb5af2013-03-13 19:06:272438static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2439 LayerImpl* layer_impl) {
2440 if (!layer_impl)
2441 return;
[email protected]94f206c12012-08-25 00:09:142442
[email protected]c1bb5af2013-03-13 19:06:272443 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472444 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272445 if (!scroll_delta.IsZero()) {
2446 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392447 scroll.layer_id = layer_impl->id();
2448 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272449 scroll_info->scrolls.push_back(scroll);
2450 layer_impl->SetSentScrollDelta(scroll_delta);
2451 }
[email protected]94f206c12012-08-25 00:09:142452
[email protected]c1bb5af2013-03-13 19:06:272453 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2454 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142455}
2456
[email protected]c1bb5af2013-03-13 19:06:272457scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2458 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302459
[email protected]c1bb5af2013-03-13 19:06:272460 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392461 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2462 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302463
[email protected]c1bb5af2013-03-13 19:06:272464 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302465}
2466
[email protected]c1bb5af2013-03-13 19:06:272467void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282468 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102469}
2470
[email protected]c1bb5af2013-03-13 19:06:272471void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) {
2472 if (!page_scale_animation_ || !RootScrollLayer())
2473 return;
2474
2475 double monotonic_time = (time - base::TimeTicks()).InSecondsF();
2476 gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() +
[email protected]1960a712013-04-30 17:06:472477 RootScrollLayer()->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272478
[email protected]34e1b512013-09-26 17:32:332479 if (!page_scale_animation_->IsAnimationStarted())
2480 page_scale_animation_->StartAnimation(monotonic_time);
2481
[email protected]c1bb5af2013-03-13 19:06:272482 active_tree_->SetPageScaleDelta(
2483 page_scale_animation_->PageScaleFactorAtTime(monotonic_time) /
2484 active_tree_->page_scale_factor());
2485 gfx::Vector2dF next_scroll =
2486 page_scale_animation_->ScrollOffsetAtTime(monotonic_time);
2487
2488 RootScrollLayer()->ScrollBy(next_scroll - scroll_total);
2489 client_->SetNeedsRedrawOnImplThread();
2490
2491 if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) {
2492 page_scale_animation_.reset();
2493 client_->SetNeedsCommitOnImplThread();
2494 client_->RenewTreePriority();
2495 }
[email protected]829ad972013-01-28 23:36:102496}
2497
[email protected]ffb2720f2013-03-15 19:18:372498void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
2499 if (!top_controls_manager_ || !RootScrollLayer())
2500 return;
2501 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
2502 UpdateMaxScrollOffset();
[email protected]5ef82622013-05-01 16:26:172503 if (RootScrollLayer()->TotalScrollOffset().y() == 0.f)
2504 return;
[email protected]ffb2720f2013-03-15 19:18:372505 RootScrollLayer()->ScrollBy(gfx::ScaleVector2d(
2506 scroll, 1.f / active_tree_->total_page_scale_factor()));
2507}
2508
[email protected]c1bb5af2013-03-13 19:06:272509void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time,
2510 base::Time wall_clock_time) {
[email protected]8e0176d2013-03-21 03:14:522511 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272512 animation_registrar_->active_animation_controllers().empty() ||
2513 !active_tree_->root_layer())
2514 return;
2515
2516 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2517
2518 last_animation_time_ = wall_clock_time;
2519 double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF();
2520
2521 AnimationRegistrar::AnimationControllerMap copy =
2522 animation_registrar_->active_animation_controllers();
2523 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2524 iter != copy.end();
2525 ++iter)
2526 (*iter).second->Animate(monotonic_seconds);
2527
2528 client_->SetNeedsRedrawOnImplThread();
[email protected]131a0c22013-02-12 18:31:082529}
2530
[email protected]3d9f7432013-04-06 00:35:182531void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522532 if (!settings_.accelerated_animation_enabled ||
[email protected]c1bb5af2013-03-13 19:06:272533 animation_registrar_->active_animation_controllers().empty() ||
2534 !active_tree_->root_layer())
2535 return;
2536
2537 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2538 scoped_ptr<AnimationEventsVector> events =
2539 make_scoped_ptr(new AnimationEventsVector);
2540 AnimationRegistrar::AnimationControllerMap copy =
2541 animation_registrar_->active_animation_controllers();
2542 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2543 iter != copy.end();
2544 ++iter)
[email protected]3d9f7432013-04-06 00:35:182545 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272546
2547 if (!events->empty()) {
2548 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(),
2549 last_animation_time_);
2550 }
[email protected]131a0c22013-02-12 18:31:082551}
2552
[email protected]c1bb5af2013-03-13 19:06:272553base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2554 return base::TimeDelta::FromSeconds(1);
2555}
2556
[email protected]50644642013-06-20 13:58:552557void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352558 DCHECK(current);
[email protected]50644642013-06-20 13:58:552559 // TODO(boliu): Rename DidLoseOutputSurface to ReleaseResources.
[email protected]ff1211d2013-06-07 01:58:352560 current->DidLoseOutputSurface();
2561 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552562 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352563 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552564 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352565 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552566 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352567}
2568
[email protected]b5174d712013-08-28 08:10:432569void LayerTreeHostImpl::SetOffscreenContextProvider(
2570 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2571 if (!offscreen_context_provider.get()) {
2572 offscreen_context_provider_ = NULL;
2573 return;
2574 }
2575
2576 if (!offscreen_context_provider->BindToCurrentThread()) {
2577 offscreen_context_provider_ = NULL;
2578 return;
2579 }
2580
2581 offscreen_context_provider_ = offscreen_context_provider;
2582}
2583
[email protected]c1bb5af2013-03-13 19:06:272584std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2585 std::string str;
2586 if (active_tree_->root_layer()) {
2587 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2588 base::JSONWriter::WriteWithOptions(
2589 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2590 }
2591 return str;
2592}
2593
[email protected]c1bb5af2013-03-13 19:06:272594int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362595 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272596}
2597
[email protected]c1bb5af2013-03-13 19:06:272598void LayerTreeHostImpl::SendManagedMemoryStats(
2599 size_t memory_visible_bytes,
2600 size_t memory_visible_and_nearby_bytes,
2601 size_t memory_use_bytes) {
2602 if (!renderer_)
2603 return;
2604
2605 // Round the numbers being sent up to the next 8MB, to throttle the rate
2606 // at which we spam the GPU process.
2607 static const size_t rounding_step = 8 * 1024 * 1024;
2608 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2609 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2610 rounding_step);
2611 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2612 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2613 last_sent_memory_visible_and_nearby_bytes_ ==
2614 memory_visible_and_nearby_bytes &&
2615 last_sent_memory_use_bytes_ == memory_use_bytes) {
2616 return;
2617 }
2618 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2619 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2620 last_sent_memory_use_bytes_ = memory_use_bytes;
2621
2622 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2623 last_sent_memory_visible_and_nearby_bytes_,
2624 last_sent_memory_use_bytes_);
2625}
2626
2627void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2628 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2629}
2630
2631void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2632 base::TimeTicks time) {
2633 if (!layer)
2634 return;
2635
2636 ScrollbarAnimationController* scrollbar_controller =
2637 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352638 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202639 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422640 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2641 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:272642 client_->SetNeedsRedrawOnImplThread();
[email protected]0fc818e2013-03-18 06:45:202643 }
[email protected]c1bb5af2013-03-13 19:06:272644
2645 for (size_t i = 0; i < layer->children().size(); ++i)
2646 AnimateScrollbarsRecursive(layer->children()[i], time);
2647}
2648
[email protected]21c9dee72013-06-15 01:20:052649void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202650 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]21c9dee72013-06-15 01:20:052651 StartScrollbarAnimationRecursive(RootLayer(), CurrentPhysicalTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202652}
2653
2654void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2655 base::TimeTicks time) {
2656 if (!layer)
2657 return;
2658
2659 ScrollbarAnimationController* scrollbar_controller =
2660 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352661 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2662 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202663 if (delay > base::TimeDelta())
2664 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352665 else if (scrollbar_controller->Animate(time))
[email protected]0fc818e2013-03-18 06:45:202666 client_->SetNeedsRedrawOnImplThread();
2667 }
2668
2669 for (size_t i = 0; i < layer->children().size(); ++i)
2670 StartScrollbarAnimationRecursive(layer->children()[i], time);
2671}
2672
[email protected]c1bb5af2013-03-13 19:06:272673void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2674 if (!tile_manager_)
2675 return;
2676
[email protected]1bcced22013-09-24 13:51:192677 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272678 return;
[email protected]1bcced22013-09-24 13:51:192679 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082680 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272681}
2682
[email protected]8347d692013-05-17 23:22:382683void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552684 current_frame_timeticks_ = base::TimeTicks();
2685 current_frame_time_ = base::Time();
[email protected]c1bb5af2013-03-13 19:06:272686}
2687
[email protected]21c9dee72013-06-15 01:20:052688void LayerTreeHostImpl::UpdateCurrentFrameTime(base::TimeTicks* ticks,
2689 base::Time* now) const {
[email protected]fb7425a2013-04-22 16:28:552690 if (ticks->is_null()) {
2691 DCHECK(now->is_null());
[email protected]21c9dee72013-06-15 01:20:052692 *ticks = CurrentPhysicalTimeTicks();
[email protected]fb7425a2013-04-22 16:28:552693 *now = base::Time::Now();
2694 }
2695}
2696
2697base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
2698 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
2699 return current_frame_timeticks_;
2700}
2701
2702base::Time LayerTreeHostImpl::CurrentFrameTime() {
2703 UpdateCurrentFrameTime(&current_frame_timeticks_, &current_frame_time_);
[email protected]c1bb5af2013-03-13 19:06:272704 return current_frame_time_;
2705}
2706
[email protected]21c9dee72013-06-15 01:20:052707base::TimeTicks LayerTreeHostImpl::CurrentPhysicalTimeTicks() const {
[email protected]de2cf8c2013-10-25 19:46:462708 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052709}
2710
[email protected]34806722013-08-09 23:51:212711scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2712 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272713 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222714 if (this->pending_tree_)
2715 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272716 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212717 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272718 if (tile_manager_)
2719 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2720 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222721 if (pending_tree_)
2722 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212723 if (frame)
2724 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222725 return state.PassAs<base::Value>();
2726}
2727
2728scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222729 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2730 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472731 if (tile_manager_)
2732 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272733 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082734}
2735
[email protected]6e7fdeb2013-07-09 14:28:382736void LayerTreeHostImpl::SetDebugState(
2737 const LayerTreeDebugState& new_debug_state) {
2738 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2739 return;
2740 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272741 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242742
[email protected]6e7fdeb2013-07-09 14:28:382743 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122744 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382745 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022746}
2747
[email protected]741fba422013-09-20 03:34:142748void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2749 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572750 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572751
[email protected]efa48412013-09-05 15:30:162752 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142753 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162754 case UIResourceBitmap::CLAMP_TO_EDGE:
2755 wrap_mode = GL_CLAMP_TO_EDGE;
2756 break;
2757 case UIResourceBitmap::REPEAT:
2758 wrap_mode = GL_REPEAT;
2759 break;
2760 }
2761
[email protected]c9280762013-08-01 06:28:572762 // Allow for multiple creation requests with the same UIResourceId. The
2763 // previous resource is simply deleted.
2764 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2765 if (id)
2766 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122767
2768 ResourceFormat format = resource_provider_->best_texture_format();
2769 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2770 format = ETC1;
[email protected]c9280762013-08-01 06:28:572771 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142772 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162773 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142774 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:122775 format);
[email protected]c9280762013-08-01 06:28:572776
[email protected]efbdb3a2013-10-04 00:35:132777 UIResourceData data;
2778 data.resource_id = id;
2779 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:512780 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:132781
2782 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:032783
2784 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:572785 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:032786 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:142787 gfx::Rect(bitmap.GetSize()),
2788 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:572789 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:482790 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572791}
2792
2793void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
2794 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2795 if (id) {
2796 resource_provider_->DeleteResource(id);
2797 ui_resource_map_.erase(uid);
2798 }
[email protected]127bdc1a2013-09-11 01:44:482799 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:572800}
2801
[email protected]127bdc1a2013-09-11 01:44:482802void LayerTreeHostImpl::EvictAllUIResources() {
2803 if (ui_resource_map_.empty())
2804 return;
2805
[email protected]5f4dc4f2013-09-05 14:58:212806 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
2807 iter != ui_resource_map_.end();
2808 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:482809 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:132810 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:212811 }
2812 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:482813
2814 client_->SetNeedsCommitOnImplThread();
2815 client_->OnCanDrawStateChanged(CanDraw());
2816 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:212817}
2818
[email protected]c9280762013-08-01 06:28:572819ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
2820 UIResourceId uid) const {
2821 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2822 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:132823 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:572824 return 0;
2825}
2826
[email protected]709c9542013-10-26 01:43:512827bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
2828 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
2829 DCHECK(iter != ui_resource_map_.end());
2830 return iter->second.opaque;
2831}
2832
[email protected]127bdc1a2013-09-11 01:44:482833bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
2834 return !evicted_ui_resources_.empty();
2835}
2836
2837void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
2838 std::set<UIResourceId>::iterator found_in_evicted =
2839 evicted_ui_resources_.find(uid);
2840 if (found_in_evicted == evicted_ui_resources_.end())
2841 return;
2842 evicted_ui_resources_.erase(found_in_evicted);
2843 if (evicted_ui_resources_.empty())
2844 client_->OnCanDrawStateChanged(CanDraw());
2845}
2846
[email protected]5e5648a2013-11-18 00:39:332847void LayerTreeHostImpl::ScheduleMicroBenchmark(
2848 scoped_ptr<MicroBenchmarkImpl> benchmark) {
2849 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
2850}
2851
[email protected]d3143c732012-10-05 19:17:592852} // namespace cc