blob: cbfbd456436bf1396f9462e36a17e9462aa6a877 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
[email protected]b6eb8e332013-09-10 00:51:018#include <limits>
[email protected]ac7c7f52012-11-08 06:26:509
[email protected]ad5d1422012-10-19 13:40:2910#include "base/basictypes.h"
[email protected]34806722013-08-09 23:51:2111#include "base/containers/hash_tables.h"
[email protected]4a23c374c2012-12-08 08:38:5512#include "base/json/json_writer.h"
[email protected]f5864912013-02-01 03:18:1413#include "base/metrics/histogram.h"
[email protected]de4afb5e2012-12-20 00:11:3414#include "base/stl_util.h"
[email protected]8e61d4b2013-06-10 22:11:4815#include "base/strings/stringprintf.h"
[email protected]95e4e1a02013-03-18 07:09:0916#include "cc/animation/scrollbar_animation_controller.h"
[email protected]85d136f782013-04-26 22:04:4017#include "cc/animation/timing_function.h"
[email protected]6be422b2013-12-08 06:47:3118#include "cc/base/latency_info_swap_promise_monitor.h"
[email protected]681ccff2013-03-18 06:13:5219#include "cc/base/math_util.h"
20#include "cc/base/util.h"
[email protected]adbe30f2013-10-11 21:12:3321#include "cc/debug/benchmark_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2722#include "cc/debug/debug_rect_history.h"
[email protected]dbe759a2013-12-02 19:23:0223#include "cc/debug/devtools_instrumentation.h"
[email protected]6e84de22013-03-18 06:54:2724#include "cc/debug/frame_rate_counter.h"
[email protected]6e84de22013-03-18 06:54:2725#include "cc/debug/paint_time_counter.h"
[email protected]372bad5f2013-03-21 16:38:4326#include "cc/debug/rendering_stats_instrumentation.h"
[email protected]f6742f52013-05-08 23:52:2227#include "cc/debug/traced_value.h"
[email protected]3052b10f2013-03-18 07:41:2128#include "cc/input/page_scale_animation.h"
29#include "cc/input/top_controls_manager.h"
[email protected]cc3cfaa2013-03-18 09:05:5230#include "cc/layers/append_quads_data.h"
31#include "cc/layers/heads_up_display_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2832#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5233#include "cc/layers/layer_iterator.h"
[email protected]3a83478b2013-08-22 20:55:1734#include "cc/layers/painted_scrollbar_layer_impl.h"
[email protected]50761e92013-03-29 20:51:2835#include "cc/layers/render_surface_impl.h"
[email protected]bf1cfd9a2013-09-26 05:43:0236#include "cc/layers/scrollbar_layer_impl_base.h"
[email protected]7f0d825f2013-03-18 07:24:3037#include "cc/output/compositor_frame_metadata.h"
[email protected]30fe19ff2013-07-04 00:54:4538#include "cc/output/copy_output_request.h"
[email protected]7f0d825f2013-03-18 07:24:3039#include "cc/output/delegating_renderer.h"
40#include "cc/output/gl_renderer.h"
41#include "cc/output/software_renderer.h"
[email protected]89e82672013-03-18 07:50:5642#include "cc/quads/render_pass_draw_quad.h"
43#include "cc/quads/shared_quad_state.h"
44#include "cc/quads/solid_color_draw_quad.h"
[email protected]9f4f6a32013-09-04 21:35:1245#include "cc/quads/texture_draw_quad.h"
[email protected]ec7541d2014-04-09 01:23:5346#include "cc/resources/direct_raster_worker_pool.h"
47#include "cc/resources/image_raster_worker_pool.h"
[email protected]e12dd0e2013-03-18 08:24:4048#include "cc/resources/memory_history.h"
49#include "cc/resources/picture_layer_tiling.h"
[email protected]ec7541d2014-04-09 01:23:5350#include "cc/resources/pixel_buffer_raster_worker_pool.h"
[email protected]e12dd0e2013-03-18 08:24:4051#include "cc/resources/prioritized_resource_manager.h"
[email protected]7beac1b52014-04-10 23:09:5652#include "cc/resources/raster_worker_pool.h"
[email protected]ea468c6c2013-09-10 08:25:1153#include "cc/resources/texture_mailbox_deleter.h"
[email protected]c9280762013-08-01 06:28:5754#include "cc/resources/ui_resource_bitmap.h"
[email protected]be4655a2013-03-18 08:36:3155#include "cc/scheduler/delay_based_time_source.h"
[email protected]556fd292013-03-18 08:03:0456#include "cc/trees/damage_tracker.h"
57#include "cc/trees/layer_tree_host.h"
58#include "cc/trees/layer_tree_host_common.h"
59#include "cc/trees/layer_tree_impl.h"
[email protected]34ba1ffb2014-03-05 06:55:0360#include "cc/trees/occlusion_tracker.h"
[email protected]556fd292013-03-18 08:03:0461#include "cc/trees/quad_culler.h"
62#include "cc/trees/single_thread_proxy.h"
63#include "cc/trees/tree_synchronizer.h"
[email protected]8d7aa012013-11-23 21:19:1264#include "gpu/GLES2/gl2extchromium.h"
[email protected]de2cf8c2013-10-25 19:46:4665#include "ui/gfx/frame_time.h"
[email protected]d455d552012-11-02 00:19:0666#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1367#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1468
[email protected]94f206c12012-08-25 00:09:1469namespace {
70
[email protected]c1bb5af2013-03-13 19:06:2771void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) {
72 if (visible) {
73 TRACE_EVENT_ASYNC_BEGIN1("webkit",
74 "LayerTreeHostImpl::SetVisible",
75 id,
76 "LayerTreeHostImpl",
77 id);
78 return;
79 }
[email protected]94f206c12012-08-25 00:09:1480
[email protected]c1bb5af2013-03-13 19:06:2781 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id);
[email protected]94f206c12012-08-25 00:09:1482}
83
[email protected]b6eb8e332013-09-10 00:51:0184size_t GetMaxTransferBufferUsageBytes(cc::ContextProvider* context_provider) {
[email protected]b4664142013-11-08 00:50:3185 // Software compositing should not use this value in production. Just use a
86 // default value when testing uploads with the software compositor.
87 if (!context_provider)
[email protected]b6eb8e332013-09-10 00:51:0188 return std::numeric_limits<size_t>::max();
[email protected]b4664142013-11-08 00:50:3189
90 // We want to make sure the default transfer buffer size is equal to the
91 // amount of data that can be uploaded by the compositor to avoid stalling
92 // the pipeline.
93 // For reference Chromebook Pixel can upload 1MB in about 0.5ms.
94 const size_t kMaxBytesUploadedPerMs = 1024 * 1024 * 2;
95 // Assuming a two frame deep pipeline between CPU and GPU and we are
96 // drawing 60 frames per second which would require us to draw one
97 // frame in 16 milliseconds.
98 const size_t kMaxTransferBufferUsageBytes = 16 * 2 * kMaxBytesUploadedPerMs;
99 return std::min(
100 context_provider->ContextCapabilities().max_transfer_buffer_usage_bytes,
101 kMaxTransferBufferUsageBytes);
102}
103
104size_t GetMaxRasterTasksUsageBytes(cc::ContextProvider* context_provider) {
105 // Transfer-buffer/raster-tasks limits are different but related. We make
106 // equal here, as this is ideal when using transfer buffers. When not using
107 // transfer buffers we should still limit raster to something similar, to
108 // preserve caching behavior (and limit memory waste when priorities change).
109 return GetMaxTransferBufferUsageBytes(context_provider);
[email protected]b6eb8e332013-09-10 00:51:01110}
111
[email protected]00d92d32014-01-23 09:46:50112unsigned GetMapImageTextureTarget(cc::ContextProvider* context_provider) {
[email protected]8d7aa012013-11-23 21:19:12113 if (!context_provider)
114 return GL_TEXTURE_2D;
115
[email protected]900db4412014-01-12 02:19:01116 if (context_provider->ContextCapabilities().gpu.egl_image_external)
[email protected]8d7aa012013-11-23 21:19:12117 return GL_TEXTURE_EXTERNAL_OES;
[email protected]900db4412014-01-12 02:19:01118 if (context_provider->ContextCapabilities().gpu.texture_rectangle)
[email protected]8d7aa012013-11-23 21:19:12119 return GL_TEXTURE_RECTANGLE_ARB;
[email protected]8d7aa012013-11-23 21:19:12120
121 return GL_TEXTURE_2D;
122}
123
[email protected]c1bb5af2013-03-13 19:06:27124} // namespace
[email protected]94f206c12012-08-25 00:09:14125
[email protected]9c88e562012-09-14 22:21:30126namespace cc {
[email protected]94f206c12012-08-25 00:09:14127
[email protected]96baf3e2012-10-22 23:09:55128class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]c1bb5af2013-03-13 19:06:27129 public:
130 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create(
131 LayerTreeHostImpl* layer_tree_host_impl,
132 scoped_refptr<DelayBasedTimeSource> time_source) {
133 return make_scoped_ptr(
134 new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl,
135 time_source));
136 }
137 virtual ~LayerTreeHostImplTimeSourceAdapter() {
[email protected]6d0e69d2013-03-20 14:53:26138 time_source_->SetClient(NULL);
139 time_source_->SetActive(false);
[email protected]c1bb5af2013-03-13 19:06:27140 }
141
[email protected]6d0e69d2013-03-20 14:53:26142 virtual void OnTimerTick() OVERRIDE {
[email protected]c1bb5af2013-03-13 19:06:27143 // In single threaded mode we attempt to simulate changing the current
144 // thread by maintaining a fake thread id. When we switch from one
145 // thread to another, we construct DebugScopedSetXXXThread objects that
146 // update the thread id. This lets DCHECKS that ensure we're on the
147 // right thread to work correctly in single threaded mode. The problem
148 // here is that the timer tasks are run via the message loop, and when
149 // they run, we've had no chance to construct a DebugScopedSetXXXThread
150 // object. The result is that we report that we're running on the main
151 // thread. In multi-threaded mode, this timer is run on the compositor
152 // thread, so to keep this consistent in single-threaded mode, we'll
153 // construct a DebugScopedSetImplThread object. There is no need to do
154 // this in multi-threaded mode since the real thread id's will be
155 // correct. In fact, setting fake thread id's interferes with the real
156 // thread id's and causes breakage.
157 scoped_ptr<DebugScopedSetImplThread> set_impl_thread;
158 if (!layer_tree_host_impl_->proxy()->HasImplThread()) {
159 set_impl_thread.reset(
160 new DebugScopedSetImplThread(layer_tree_host_impl_->proxy()));
[email protected]94f206c12012-08-25 00:09:14161 }
162
[email protected]94bf75c2013-06-12 13:20:04163 // TODO(enne): This should probably happen post-animate.
164 if (layer_tree_host_impl_->pending_tree()) {
[email protected]4f48f6e2013-08-27 06:33:38165 layer_tree_host_impl_->pending_tree()->UpdateDrawProperties();
166 layer_tree_host_impl_->ManageTiles();
[email protected]94bf75c2013-06-12 13:20:04167 }
168
[email protected]fb7425a2013-04-22 16:28:55169 layer_tree_host_impl_->Animate(
[email protected]27152642014-03-11 20:42:00170 layer_tree_host_impl_->CurrentFrameTimeTicks());
[email protected]3d9f7432013-04-06 00:35:18171 layer_tree_host_impl_->UpdateBackgroundAnimateTicking(true);
172 bool start_ready_animations = true;
173 layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
[email protected]8347d692013-05-17 23:22:38174 layer_tree_host_impl_->ResetCurrentFrameTimeForNextFrame();
[email protected]c1bb5af2013-03-13 19:06:27175 }
[email protected]373974232013-01-10 22:20:50176
[email protected]c1bb5af2013-03-13 19:06:27177 void SetActive(bool active) {
[email protected]6d0e69d2013-03-20 14:53:26178 if (active != time_source_->Active())
179 time_source_->SetActive(active);
[email protected]c1bb5af2013-03-13 19:06:27180 }
[email protected]94f206c12012-08-25 00:09:14181
[email protected]d9fce6722013-08-30 01:10:01182 bool Active() const { return time_source_->Active(); }
183
[email protected]c1bb5af2013-03-13 19:06:27184 private:
185 LayerTreeHostImplTimeSourceAdapter(
186 LayerTreeHostImpl* layer_tree_host_impl,
187 scoped_refptr<DelayBasedTimeSource> time_source)
188 : layer_tree_host_impl_(layer_tree_host_impl),
189 time_source_(time_source) {
[email protected]6d0e69d2013-03-20 14:53:26190 time_source_->SetClient(this);
[email protected]c1bb5af2013-03-13 19:06:27191 }
[email protected]94f206c12012-08-25 00:09:14192
[email protected]c1bb5af2013-03-13 19:06:27193 LayerTreeHostImpl* layer_tree_host_impl_;
194 scoped_refptr<DelayBasedTimeSource> time_source_;
[email protected]94f206c12012-08-25 00:09:14195
[email protected]c1bb5af2013-03-13 19:06:27196 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14197};
198
[email protected]96baf3e2012-10-22 23:09:55199LayerTreeHostImpl::FrameData::FrameData()
[email protected]e0341352013-04-06 05:01:20200 : contains_incomplete_tile(false), has_no_damage(false) {}
[email protected]c1bb5af2013-03-13 19:06:27201
202LayerTreeHostImpl::FrameData::~FrameData() {}
203
204scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create(
205 const LayerTreeSettings& settings,
206 LayerTreeHostImplClient* client,
[email protected]372bad5f2013-03-21 16:38:43207 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57208 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02209 SharedBitmapManager* manager,
210 int id) {
[email protected]a7f35682013-10-22 23:05:57211 return make_scoped_ptr(new LayerTreeHostImpl(
[email protected]dbe759a2013-12-02 19:23:02212 settings, client, proxy, rendering_stats_instrumentation, manager, id));
[email protected]493067512012-09-19 23:34:10213}
214
[email protected]372bad5f2013-03-21 16:38:43215LayerTreeHostImpl::LayerTreeHostImpl(
216 const LayerTreeSettings& settings,
217 LayerTreeHostImplClient* client,
218 Proxy* proxy,
[email protected]a7f35682013-10-22 23:05:57219 RenderingStatsInstrumentation* rendering_stats_instrumentation,
[email protected]dbe759a2013-12-02 19:23:02220 SharedBitmapManager* manager,
221 int id)
[email protected]c1bb5af2013-03-13 19:06:27222 : client_(client),
223 proxy_(proxy),
[email protected]200a9c062013-05-20 04:34:37224 input_handler_client_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27225 did_lock_scrolling_layer_(false),
226 should_bubble_scrolls_(false),
227 wheel_scrolling_(false),
[email protected]f161ca9e2014-04-01 13:57:01228 scroll_affects_scroll_handler_(false),
[email protected]f620b0e72013-10-01 21:38:24229 scroll_layer_id_when_mouse_over_scrollbar_(0),
[email protected]c48536a52013-09-14 00:02:08230 tile_priorities_dirty_(false),
[email protected]1960a712013-04-30 17:06:47231 root_layer_scroll_offset_delegate_(NULL),
[email protected]c1bb5af2013-03-13 19:06:27232 settings_(settings),
[email protected]c1bb5af2013-03-13 19:06:27233 visible_(true),
[email protected]3f2ff112013-08-03 02:41:07234 cached_managed_memory_policy_(
[email protected]b56c1302013-03-20 21:17:34235 PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
[email protected]f44d5552013-10-29 04:56:29236 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
[email protected]3f2ff112013-08-03 02:41:07237 ManagedMemoryPolicy::kDefaultNumResourcesLimit),
[email protected]c1bb5af2013-03-13 19:06:27238 pinch_gesture_active_(false),
[email protected]2fa342b82013-09-24 03:19:13239 pinch_gesture_end_should_clear_scrolling_layer_(false),
[email protected]9e3594522013-03-18 00:57:36240 fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())),
[email protected]7497316a2013-03-15 12:42:29241 paint_time_counter_(PaintTimeCounter::Create()),
[email protected]c1bb5af2013-03-13 19:06:27242 memory_history_(MemoryHistory::Create()),
[email protected]d35992782013-03-14 14:54:02243 debug_rect_history_(DebugRectHistory::Create()),
[email protected]dd909b1f2014-03-18 00:19:52244 texture_mailbox_deleter_(new TextureMailboxDeleter(
245 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
246 : proxy_->MainThreadTaskRunner())),
[email protected]d7626ffd2013-03-29 00:17:42247 max_memory_needed_bytes_(0),
[email protected]c1bb5af2013-03-13 19:06:27248 last_sent_memory_visible_bytes_(0),
249 last_sent_memory_visible_and_nearby_bytes_(0),
250 last_sent_memory_use_bytes_(0),
[email protected]50644642013-06-20 13:58:55251 zero_budget_(false),
[email protected]f224cc92013-06-06 23:23:32252 device_scale_factor_(1.f),
[email protected]9f4f6a32013-09-04 21:35:12253 overhang_ui_resource_id_(0),
[email protected]f224cc92013-06-06 23:23:32254 overdraw_bottom_height_(0.f),
[email protected]54af03522013-09-05 00:43:28255 device_viewport_valid_for_tile_management_(true),
[email protected]c32a1962013-07-30 19:41:17256 external_stencil_test_enabled_(false),
[email protected]372bad5f2013-03-21 16:38:43257 animation_registrar_(AnimationRegistrar::Create()),
[email protected]39643fb2013-07-09 17:28:19258 rendering_stats_instrumentation_(rendering_stats_instrumentation),
[email protected]5e5648a2013-11-18 00:39:33259 micro_benchmark_controller_(this),
[email protected]a7f35682013-10-22 23:05:57260 need_to_update_visible_tiles_before_draw_(false),
[email protected]767f38d72014-03-18 21:26:41261#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:36262 did_lose_called_(false),
263#endif
[email protected]dbe759a2013-12-02 19:23:02264 shared_bitmap_manager_(manager),
265 id_(id) {
[email protected]c1bb5af2013-03-13 19:06:27266 DCHECK(proxy_->IsImplThread());
267 DidVisibilityChange(this, visible_);
268
[email protected]8e0176d2013-03-21 03:14:52269 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27270
[email protected]8e0176d2013-03-21 03:14:52271 if (settings.calculate_top_controls_position) {
[email protected]c1bb5af2013-03-13 19:06:27272 top_controls_manager_ =
273 TopControlsManager::Create(this,
[email protected]8e0176d2013-03-21 03:14:52274 settings.top_controls_height,
275 settings.top_controls_show_threshold,
276 settings.top_controls_hide_threshold);
[email protected]c1bb5af2013-03-13 19:06:27277 }
278
[email protected]8e0176d2013-03-21 03:14:52279 SetDebugState(settings.initial_debug_state);
[email protected]c1bb5af2013-03-13 19:06:27280
281 // LTHI always has an active tree.
282 active_tree_ = LayerTreeImpl::create(this);
[email protected]9197dbcb2013-05-15 20:28:51283 TRACE_EVENT_OBJECT_CREATED_WITH_ID(
284 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]493067512012-09-19 23:34:10285}
286
[email protected]c1bb5af2013-03-13 19:06:27287LayerTreeHostImpl::~LayerTreeHostImpl() {
288 DCHECK(proxy_->IsImplThread());
289 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]9197dbcb2013-05-15 20:28:51290 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
291 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerTreeHostImpl", this);
[email protected]c1bb5af2013-03-13 19:06:27292
[email protected]200a9c062013-05-20 04:34:37293 if (input_handler_client_) {
294 input_handler_client_->WillShutdown();
295 input_handler_client_ = NULL;
296 }
297
[email protected]75deb742013-06-24 20:19:18298 // The layer trees must be destroyed before the layer tree host. We've
299 // made a contract with our animation controllers that the registrar
300 // will outlive them, and we must make good.
[email protected]df17af52014-02-06 02:20:40301 if (recycle_tree_)
302 recycle_tree_->Shutdown();
303 if (pending_tree_)
304 pending_tree_->Shutdown();
305 active_tree_->Shutdown();
[email protected]75deb742013-06-24 20:19:18306 recycle_tree_.reset();
307 pending_tree_.reset();
308 active_tree_.reset();
[email protected]ec7541d2014-04-09 01:23:53309 tile_manager_.reset();
[email protected]7beac1b52014-04-10 23:09:56310 raster_worker_pool_.reset();
[email protected]ec7541d2014-04-09 01:23:53311 direct_raster_worker_pool_.reset();
[email protected]94f206c12012-08-25 00:09:14312}
313
[email protected]097a0cbf2013-12-12 17:04:58314void LayerTreeHostImpl::BeginMainFrameAborted(bool did_handle) {
315 // If the begin frame data was handled, then scroll and scale set was applied
316 // by the main thread, so the active tree needs to be updated as if these sent
317 // values were applied and committed.
318 if (did_handle) {
319 active_tree_->ApplySentScrollAndScaleDeltasFromAbortedCommit();
320 active_tree_->ResetContentsTexturesPurged();
321 }
322}
323
[email protected]c1bb5af2013-03-13 19:06:27324void LayerTreeHostImpl::BeginCommit() {}
[email protected]3b31c6ac2012-12-06 21:27:29325
[email protected]c1bb5af2013-03-13 19:06:27326void LayerTreeHostImpl::CommitComplete() {
327 TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete");
[email protected]131a0c22013-02-12 18:31:08328
[email protected]8e0176d2013-03-21 03:14:52329 if (settings_.impl_side_painting) {
[email protected]58241dc2013-08-20 01:39:25330 // Impl-side painting needs an update immediately post-commit to have the
331 // opportunity to create tilings. Other paths can call UpdateDrawProperties
332 // more lazily when needed prior to drawing.
[email protected]daea3d42013-10-23 17:04:50333 pending_tree()->ApplyScrollDeltasSinceBeginMainFrame();
[email protected]c1bb5af2013-03-13 19:06:27334 pending_tree_->set_needs_update_draw_properties();
[email protected]7d19dc342013-05-02 22:02:04335 pending_tree_->UpdateDrawProperties();
[email protected]a23451e2013-06-07 20:58:26336 // Start working on newly created tiles immediately if needed.
[email protected]c48536a52013-09-14 00:02:08337 if (!tile_manager_ || !tile_priorities_dirty_)
[email protected]4f48f6e2013-08-27 06:33:38338 NotifyReadyToActivate();
339 else
340 ManageTiles();
[email protected]c1bb5af2013-03-13 19:06:27341 } else {
342 active_tree_->set_needs_update_draw_properties();
[email protected]d9fce6722013-08-30 01:10:01343 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
344 DCHECK(active_tree_->root_layer());
[email protected]c1bb5af2013-03-13 19:06:27345 }
[email protected]3ba4cae2013-01-16 03:58:38346
[email protected]c1bb5af2013-03-13 19:06:27347 client_->SendManagedMemoryStats();
[email protected]5e5648a2013-11-18 00:39:33348
349 micro_benchmark_controller_.DidCompleteCommit();
[email protected]94f206c12012-08-25 00:09:14350}
351
[email protected]6133cc232013-07-30 18:47:07352bool LayerTreeHostImpl::CanDraw() const {
[email protected]c1bb5af2013-03-13 19:06:27353 // Note: If you are changing this function or any other function that might
354 // affect the result of CanDraw, make sure to call
355 // client_->OnCanDrawStateChanged in the proper places and update the
356 // NotifyIfCanDrawChanged test.
[email protected]94f206c12012-08-25 00:09:14357
[email protected]6133cc232013-07-30 18:47:07358 if (!renderer_) {
359 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer",
360 TRACE_EVENT_SCOPE_THREAD);
361 return false;
362 }
363
364 // Must have an OutputSurface if |renderer_| is not NULL.
365 DCHECK(output_surface_);
366
367 // TODO(boliu): Make draws without root_layer work and move this below
368 // draw_and_swap_full_viewport_every_frame check. Tracked in crbug.com/264967.
[email protected]c1bb5af2013-03-13 19:06:27369 if (!active_tree_->root_layer()) {
[email protected]c76faea2013-03-26 07:42:42370 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer",
371 TRACE_EVENT_SCOPE_THREAD);
[email protected]2f1acc262012-11-16 21:42:22372 return false;
[email protected]c1bb5af2013-03-13 19:06:27373 }
[email protected]6133cc232013-07-30 18:47:07374
375 if (output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
376 return true;
377
[email protected]54af03522013-09-05 00:43:28378 if (DrawViewportSize().IsEmpty()) {
[email protected]c76faea2013-03-26 07:42:42379 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport",
380 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27381 return false;
382 }
383 if (active_tree_->ViewportSizeInvalid()) {
384 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42385 "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed",
386 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27387 return false;
388 }
[email protected]c1bb5af2013-03-13 19:06:27389 if (active_tree_->ContentsTexturesPurged()) {
390 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:42391 "cc", "LayerTreeHostImpl::CanDraw contents textures purged",
392 TRACE_EVENT_SCOPE_THREAD);
[email protected]c1bb5af2013-03-13 19:06:27393 return false;
394 }
[email protected]127bdc1a2013-09-11 01:44:48395 if (EvictedUIResourcesExist()) {
396 TRACE_EVENT_INSTANT0(
397 "cc", "LayerTreeHostImpl::CanDraw UI resources evicted not recreated",
398 TRACE_EVENT_SCOPE_THREAD);
399 return false;
400 }
[email protected]c1bb5af2013-03-13 19:06:27401 return true;
[email protected]2f1acc262012-11-16 21:42:22402}
403
[email protected]27152642014-03-11 20:42:00404void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time) {
[email protected]200a9c062013-05-20 04:34:37405 if (input_handler_client_)
406 input_handler_client_->Animate(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27407 AnimatePageScale(monotonic_time);
[email protected]27152642014-03-11 20:42:00408 AnimateLayers(monotonic_time);
[email protected]c1bb5af2013-03-13 19:06:27409 AnimateScrollbars(monotonic_time);
[email protected]ffb2720f2013-03-15 19:18:37410 AnimateTopControls(monotonic_time);
[email protected]94f206c12012-08-25 00:09:14411}
412
[email protected]c1bb5af2013-03-13 19:06:27413void LayerTreeHostImpl::ManageTiles() {
[email protected]a23451e2013-06-07 20:58:26414 if (!tile_manager_)
415 return;
[email protected]c48536a52013-09-14 00:02:08416 if (!tile_priorities_dirty_)
[email protected]a23451e2013-06-07 20:58:26417 return;
[email protected]54af03522013-09-05 00:43:28418
[email protected]c48536a52013-09-14 00:02:08419 tile_priorities_dirty_ = false;
[email protected]1bcced22013-09-24 13:51:19420 tile_manager_->ManageTiles(global_tile_state_);
[email protected]c1bb5af2013-03-13 19:06:27421
422 size_t memory_required_bytes;
423 size_t memory_nice_to_have_bytes;
[email protected]7accf2232013-08-14 23:10:03424 size_t memory_allocated_bytes;
[email protected]c1bb5af2013-03-13 19:06:27425 size_t memory_used_bytes;
426 tile_manager_->GetMemoryStats(&memory_required_bytes,
427 &memory_nice_to_have_bytes,
[email protected]7accf2232013-08-14 23:10:03428 &memory_allocated_bytes,
[email protected]c1bb5af2013-03-13 19:06:27429 &memory_used_bytes);
430 SendManagedMemoryStats(memory_required_bytes,
431 memory_nice_to_have_bytes,
432 memory_used_bytes);
[email protected]99cc6b42014-01-17 00:12:57433
434 client_->DidManageTiles();
[email protected]f57bbc02012-11-21 07:02:15435}
436
[email protected]243e4f12014-02-05 09:18:42437void LayerTreeHostImpl::StartPageScaleAnimation(
438 const gfx::Vector2d& target_offset,
439 bool anchor_point,
440 float page_scale,
441 base::TimeDelta duration) {
[email protected]adeda572014-01-31 00:49:47442 if (!InnerViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:27443 return;
444
[email protected]adeda572014-01-31 00:49:47445 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:27446 gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize();
[email protected]54af03522013-09-05 00:43:28447 gfx::SizeF viewport_size = UnscaledScrollableViewportSize();
[email protected]c1bb5af2013-03-13 19:06:27448
[email protected]85d136f782013-04-26 22:04:40449 // Easing constants experimentally determined.
450 scoped_ptr<TimingFunction> timing_function =
451 CubicBezierTimingFunction::Create(.8, 0, .3, .9).PassAs<TimingFunction>();
452
[email protected]c1bb5af2013-03-13 19:06:27453 page_scale_animation_ =
454 PageScaleAnimation::Create(scroll_total,
455 active_tree_->total_page_scale_factor(),
456 viewport_size,
457 scaled_scrollable_size,
[email protected]85d136f782013-04-26 22:04:40458 timing_function.Pass());
[email protected]c1bb5af2013-03-13 19:06:27459
460 if (anchor_point) {
461 gfx::Vector2dF anchor(target_offset);
462 page_scale_animation_->ZoomWithAnchor(anchor,
463 page_scale,
464 duration.InSecondsF());
465 } else {
466 gfx::Vector2dF scaled_target_offset = target_offset;
467 page_scale_animation_->ZoomTo(scaled_target_offset,
468 page_scale,
469 duration.InSecondsF());
470 }
471
[email protected]4e4136d2013-11-29 02:22:44472 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:27473 client_->SetNeedsCommitOnImplThread();
474 client_->RenewTreePriority();
[email protected]f57bbc02012-11-21 07:02:15475}
476
[email protected]c1bb5af2013-03-13 19:06:27477void LayerTreeHostImpl::ScheduleAnimation() {
[email protected]4e4136d2013-11-29 02:22:44478 SetNeedsRedraw();
[email protected]f57bbc02012-11-21 07:02:15479}
480
[email protected]47a723f2014-03-05 12:42:49481bool LayerTreeHostImpl::HaveTouchEventHandlersAt(
482 const gfx::Point& viewport_point) {
[email protected]ed1aab12013-10-08 14:27:07483 if (!settings_.touch_hit_testing)
484 return true;
[email protected]c1bb5af2013-03-13 19:06:27485 if (!EnsureRenderSurfaceLayerList())
486 return false;
[email protected]f57bbc02012-11-21 07:02:15487
[email protected]c1bb5af2013-03-13 19:06:27488 gfx::PointF device_viewport_point =
489 gfx::ScalePoint(viewport_point, device_scale_factor_);
[email protected]f57bbc02012-11-21 07:02:15490
[email protected]c974d5d2013-10-24 01:02:48491 LayerImpl* layer_impl =
492 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
493 device_viewport_point,
494 active_tree_->RenderSurfaceLayerList());
495 return layer_impl != NULL;
[email protected]c1bb5af2013-03-13 19:06:27496}
497
[email protected]6be422b2013-12-08 06:47:31498scoped_ptr<SwapPromiseMonitor>
499LayerTreeHostImpl::CreateLatencyInfoSwapPromiseMonitor(
500 ui::LatencyInfo* latency) {
501 return scoped_ptr<SwapPromiseMonitor>(
502 new LatencyInfoSwapPromiseMonitor(latency, NULL, this));
[email protected]b76329d2013-06-11 04:15:08503}
504
[email protected]c1bb5af2013-03-13 19:06:27505void LayerTreeHostImpl::TrackDamageForAllSurfaces(
506 LayerImpl* root_draw_layer,
[email protected]50761e92013-03-29 20:51:28507 const LayerImplList& render_surface_layer_list) {
[email protected]c1bb5af2013-03-13 19:06:27508 // For now, we use damage tracking to compute a global scissor. To do this, we
509 // must compute all damage tracking before drawing anything, so that we know
510 // the root damage rect. The root damage rect is then used to scissor each
511 // surface.
512
513 for (int surface_index = render_surface_layer_list.size() - 1;
[email protected]bf691c22013-03-26 21:15:06514 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27515 --surface_index) {
516 LayerImpl* render_surface_layer = render_surface_layer_list[surface_index];
517 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
518 DCHECK(render_surface);
519 render_surface->damage_tracker()->UpdateDamageTrackingState(
520 render_surface->layer_list(),
521 render_surface_layer->id(),
522 render_surface->SurfacePropertyChangedOnlyFromDescendant(),
523 render_surface->content_rect(),
524 render_surface_layer->mask_layer(),
[email protected]1dc7943e2013-09-26 04:41:48525 render_surface_layer->filters());
[email protected]c1bb5af2013-03-13 19:06:27526 }
527}
528
[email protected]34806722013-08-09 23:51:21529scoped_ptr<base::Value> LayerTreeHostImpl::FrameData::AsValue() const {
530 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
531 value->SetBoolean("contains_incomplete_tile", contains_incomplete_tile);
532 value->SetBoolean("has_no_damage", has_no_damage);
533
534 // Quad data can be quite large, so only dump render passes if we select
535 // cc.debug.quads.
536 bool quads_enabled;
537 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
538 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), &quads_enabled);
539 if (quads_enabled) {
540 scoped_ptr<base::ListValue> render_pass_list(new base::ListValue());
541 for (size_t i = 0; i < render_passes.size(); ++i)
542 render_pass_list->Append(render_passes[i]->AsValue().release());
543 value->Set("render_passes", render_pass_list.release());
544 }
545 return value.PassAs<base::Value>();
546}
547
[email protected]c1bb5af2013-03-13 19:06:27548void LayerTreeHostImpl::FrameData::AppendRenderPass(
549 scoped_ptr<RenderPass> render_pass) {
550 render_passes_by_id[render_pass->id] = render_pass.get();
551 render_passes.push_back(render_pass.Pass());
552}
553
[email protected]ffbb2212013-06-02 23:47:59554static DrawMode GetDrawMode(OutputSurface* output_surface) {
555 if (output_surface->ForcedDrawToSoftwareDevice()) {
556 return DRAW_MODE_RESOURCELESS_SOFTWARE;
[email protected]0634cdd42013-08-16 00:46:09557 } else if (output_surface->context_provider()) {
[email protected]ffbb2212013-06-02 23:47:59558 return DRAW_MODE_HARDWARE;
559 } else {
[email protected]a7f35682013-10-22 23:05:57560 DCHECK_EQ(!output_surface->software_device(),
561 output_surface->capabilities().delegated_rendering);
[email protected]ffbb2212013-06-02 23:47:59562 return DRAW_MODE_SOFTWARE;
563 }
564}
565
[email protected]34ba1ffb2014-03-05 06:55:03566static void AppendQuadsForLayer(
567 RenderPass* target_render_pass,
568 LayerImpl* layer,
569 const OcclusionTracker<LayerImpl>& occlusion_tracker,
570 AppendQuadsData* append_quads_data) {
[email protected]c1bb5af2013-03-13 19:06:27571 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49572 QuadCuller quad_culler(&target_render_pass->quad_list,
573 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27574 layer,
575 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27576 for_surface);
577 layer->AppendQuads(&quad_culler, append_quads_data);
578}
579
580static void AppendQuadsForRenderSurfaceLayer(
581 RenderPass* target_render_pass,
582 LayerImpl* layer,
583 const RenderPass* contributing_render_pass,
[email protected]34ba1ffb2014-03-05 06:55:03584 const OcclusionTracker<LayerImpl>& occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27585 AppendQuadsData* append_quads_data) {
586 bool for_surface = true;
[email protected]c7e95b42013-03-18 01:13:49587 QuadCuller quad_culler(&target_render_pass->quad_list,
588 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27589 layer,
590 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27591 for_surface);
592
593 bool is_replica = false;
594 layer->render_surface()->AppendQuads(&quad_culler,
595 append_quads_data,
596 is_replica,
597 contributing_render_pass->id);
598
599 // Add replica after the surface so that it appears below the surface.
600 if (layer->has_replica()) {
601 is_replica = true;
602 layer->render_surface()->AppendQuads(&quad_culler,
603 append_quads_data,
604 is_replica,
605 contributing_render_pass->id);
606 }
607}
608
609static void AppendQuadsToFillScreen(
[email protected]3744e27b2013-11-06 21:44:08610 ResourceProvider::ResourceId overhang_resource_id,
[email protected]cde98022014-01-16 19:59:20611 const gfx::SizeF& overhang_resource_scaled_size,
[email protected]0023fc72014-01-10 20:05:06612 const gfx::Rect& root_scroll_layer_rect,
[email protected]c1bb5af2013-03-13 19:06:27613 RenderPass* target_render_pass,
614 LayerImpl* root_layer,
615 SkColor screen_background_color,
[email protected]34ba1ffb2014-03-05 06:55:03616 const OcclusionTracker<LayerImpl>& occlusion_tracker) {
[email protected]c1bb5af2013-03-13 19:06:27617 if (!root_layer || !SkColorGetA(screen_background_color))
618 return;
619
620 Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen();
621 if (fill_region.IsEmpty())
622 return;
623
[email protected]3744e27b2013-11-06 21:44:08624 // Divide the fill region into the part to be filled with the overhang
625 // resource and the part to be filled with the background color.
626 Region screen_background_color_region = fill_region;
627 Region overhang_region;
628 if (overhang_resource_id) {
629 overhang_region = fill_region;
630 overhang_region.Subtract(root_scroll_layer_rect);
631 screen_background_color_region.Intersect(root_scroll_layer_rect);
632 }
633
[email protected]c1bb5af2013-03-13 19:06:27634 bool for_surface = false;
[email protected]c7e95b42013-03-18 01:13:49635 QuadCuller quad_culler(&target_render_pass->quad_list,
636 &target_render_pass->shared_quad_state_list,
[email protected]c1bb5af2013-03-13 19:06:27637 root_layer,
638 occlusion_tracker,
[email protected]c1bb5af2013-03-13 19:06:27639 for_surface);
640
641 // Manually create the quad state for the gutter quads, as the root layer
642 // doesn't have any bounds and so can't generate this itself.
643 // TODO(danakj): Make the gutter quads generated by the solid color layer
644 // (make it smarter about generating quads to fill unoccluded areas).
645
646 gfx::Rect root_target_rect = root_layer->render_surface()->content_rect();
647 float opacity = 1.f;
648 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49649 quad_culler.UseSharedQuadState(SharedQuadState::Create());
[email protected]7785e1002014-02-20 21:04:32650 shared_quad_state->SetAll(gfx::Transform(),
[email protected]c1bb5af2013-03-13 19:06:27651 root_target_rect.size(),
652 root_target_rect,
653 root_target_rect,
[email protected]dc462d782012-11-21 21:43:01654 false,
[email protected]7bbeaf4e2013-11-26 10:27:22655 opacity,
656 SkXfermode::kSrcOver_Mode);
[email protected]f57bbc02012-11-21 07:02:15657
[email protected]3744e27b2013-11-06 21:44:08658 for (Region::Iterator fill_rects(screen_background_color_region);
[email protected]c1bb5af2013-03-13 19:06:27659 fill_rects.has_rect();
660 fill_rects.next()) {
[email protected]7785e1002014-02-20 21:04:32661 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24662 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08663 // Skip the quad culler and just append the quads directly to avoid
664 // occlusion checks.
665 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
[email protected]9bf06c72014-03-07 18:16:24666 quad->SetNew(shared_quad_state,
667 screen_space_rect,
668 visible_screen_space_rect,
669 screen_background_color,
670 false);
[email protected]0d8c7eaa2014-03-13 05:26:30671 quad_culler.MaybeAppend(quad.PassAs<DrawQuad>());
[email protected]3744e27b2013-11-06 21:44:08672 }
673 for (Region::Iterator fill_rects(overhang_region);
674 fill_rects.has_rect();
675 fill_rects.next()) {
676 DCHECK(overhang_resource_id);
[email protected]7785e1002014-02-20 21:04:32677 gfx::Rect screen_space_rect = fill_rects.rect();
[email protected]9bf06c72014-03-07 18:16:24678 gfx::Rect opaque_screen_space_rect = screen_space_rect;
679 gfx::Rect visible_screen_space_rect = screen_space_rect;
[email protected]3744e27b2013-11-06 21:44:08680 scoped_ptr<TextureDrawQuad> tex_quad = TextureDrawQuad::Create();
681 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
682 tex_quad->SetNew(
683 shared_quad_state,
[email protected]7785e1002014-02-20 21:04:32684 screen_space_rect,
[email protected]9bf06c72014-03-07 18:16:24685 opaque_screen_space_rect,
686 visible_screen_space_rect,
[email protected]3744e27b2013-11-06 21:44:08687 overhang_resource_id,
688 false,
[email protected]7785e1002014-02-20 21:04:32689 gfx::PointF(
690 screen_space_rect.x() / overhang_resource_scaled_size.width(),
691 screen_space_rect.y() / overhang_resource_scaled_size.height()),
692 gfx::PointF(
693 screen_space_rect.right() / overhang_resource_scaled_size.width(),
694 screen_space_rect.bottom() /
695 overhang_resource_scaled_size.height()),
[email protected]3744e27b2013-11-06 21:44:08696 screen_background_color,
697 vertex_opacity,
698 false);
[email protected]0d8c7eaa2014-03-13 05:26:30699 quad_culler.MaybeAppend(tex_quad.PassAs<DrawQuad>());
[email protected]c1bb5af2013-03-13 19:06:27700 }
[email protected]467b3612012-08-28 07:41:16701}
702
[email protected]7ddfe7e82014-01-30 07:22:11703DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::CalculateRenderPasses(
704 FrameData* frame) {
[email protected]c1bb5af2013-03-13 19:06:27705 DCHECK(frame->render_passes.empty());
[email protected]7ddfe7e82014-01-30 07:22:11706 DCHECK(CanDraw());
707 DCHECK(active_tree_->root_layer());
[email protected]2d692992012-12-19 01:19:32708
[email protected]c1bb5af2013-03-13 19:06:27709 TrackDamageForAllSurfaces(active_tree_->root_layer(),
710 *frame->render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14711
[email protected]e0341352013-04-06 05:01:20712 // If the root render surface has no visible damage, then don't generate a
713 // frame at all.
714 RenderSurfaceImpl* root_surface =
715 active_tree_->root_layer()->render_surface();
716 bool root_surface_has_no_visible_damage =
717 !root_surface->damage_tracker()->current_damage_rect().Intersects(
718 root_surface->content_rect());
719 bool root_surface_has_contributing_layers =
720 !root_surface->layer_list().empty();
[email protected]5fda7f32014-03-12 20:44:29721 bool hud_wants_to_draw_ = active_tree_->hud_layer() &&
722 active_tree_->hud_layer()->IsAnimatingHUDContents();
[email protected]e0341352013-04-06 05:01:20723 if (root_surface_has_contributing_layers &&
[email protected]37c339be2014-02-06 09:44:23724 root_surface_has_no_visible_damage &&
[email protected]5fda7f32014-03-12 20:44:29725 active_tree_->LayersWithCopyOutputRequest().empty() &&
726 !hud_wants_to_draw_) {
[email protected]e0341352013-04-06 05:01:20727 TRACE_EVENT0("cc",
728 "LayerTreeHostImpl::CalculateRenderPasses::EmptyDamageRect");
729 frame->has_no_damage = true;
[email protected]6133cc232013-07-30 18:47:07730 DCHECK(!output_surface_->capabilities()
731 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:11732 return DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]e0341352013-04-06 05:01:20733 }
734
[email protected]c1bb5af2013-03-13 19:06:27735 TRACE_EVENT1("cc",
736 "LayerTreeHostImpl::CalculateRenderPasses",
737 "render_surface_layer_list.size()",
[email protected]bf691c22013-03-26 21:15:06738 static_cast<uint64>(frame->render_surface_layer_list->size()));
[email protected]94f206c12012-08-25 00:09:14739
[email protected]c1bb5af2013-03-13 19:06:27740 // Create the render passes in dependency order.
741 for (int surface_index = frame->render_surface_layer_list->size() - 1;
[email protected]bf691c22013-03-26 21:15:06742 surface_index >= 0;
[email protected]c1bb5af2013-03-13 19:06:27743 --surface_index) {
744 LayerImpl* render_surface_layer =
745 (*frame->render_surface_layer_list)[surface_index];
[email protected]30fe19ff2013-07-04 00:54:45746 RenderSurfaceImpl* render_surface = render_surface_layer->render_surface();
747
748 bool should_draw_into_render_pass =
749 render_surface_layer->parent() == NULL ||
750 render_surface->contributes_to_drawn_surface() ||
751 render_surface_layer->HasCopyRequest();
752 if (should_draw_into_render_pass)
753 render_surface_layer->render_surface()->AppendRenderPasses(frame);
[email protected]c1bb5af2013-03-13 19:06:27754 }
[email protected]94f206c12012-08-25 00:09:14755
[email protected]f636ce82014-03-10 22:33:08756 // When we are displaying the HUD, change the root damage rect to cover the
757 // entire root surface. This will disable partial-swap/scissor optimizations
758 // that would prevent the HUD from updating, since the HUD does not cause
759 // damage itself, to prevent it from messing with damage visualizations. Since
760 // damage visualizations are done off the LayerImpls and RenderSurfaceImpls,
761 // changing the RenderPass does not affect them.
762 if (active_tree_->hud_layer()) {
763 RenderPass* root_pass = frame->render_passes.back();
764 root_pass->damage_rect = root_pass->output_rect;
765 }
766
[email protected]34ba1ffb2014-03-05 06:55:03767 OcclusionTracker<LayerImpl> occlusion_tracker(
[email protected]97c6a7342014-03-12 20:36:43768 active_tree_->root_layer()->render_surface()->content_rect());
[email protected]c1bb5af2013-03-13 19:06:27769 occlusion_tracker.set_minimum_tracking_size(
[email protected]8e0176d2013-03-21 03:14:52770 settings_.minimum_occlusion_tracking_size);
[email protected]94f206c12012-08-25 00:09:14771
[email protected]846f455b2013-03-18 19:07:41772 if (debug_state_.show_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27773 occlusion_tracker.set_occluding_screen_space_rects_container(
774 &frame->occluding_screen_space_rects);
775 }
[email protected]846f455b2013-03-18 19:07:41776 if (debug_state_.show_non_occluding_rects) {
[email protected]c1bb5af2013-03-13 19:06:27777 occlusion_tracker.set_non_occluding_screen_space_rects_container(
778 &frame->non_occluding_screen_space_rects);
779 }
[email protected]94f206c12012-08-25 00:09:14780
[email protected]ba1b33e2014-02-28 16:44:51781 // Add quads to the Render passes in front-to-back order to allow for testing
[email protected]c1bb5af2013-03-13 19:06:27782 // occlusion and performing culling during the tree walk.
[email protected]ba1b33e2014-02-28 16:44:51783 typedef LayerIterator<LayerImpl> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14784
[email protected]c1bb5af2013-03-13 19:06:27785 // Typically when we are missing a texture and use a checkerboard quad, we
786 // still draw the frame. However when the layer being checkerboarded is moving
787 // due to an impl-animation, we drop the frame to avoid flashing due to the
788 // texture suddenly appearing in the future.
[email protected]7ddfe7e82014-01-30 07:22:11789 DrawSwapReadbackResult::DrawResult draw_result =
790 DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25791 // When we have a copy request for a layer, we need to draw no matter
792 // what, as the layer may disappear after this frame.
793 bool have_copy_request = false;
[email protected]94f206c12012-08-25 00:09:14794
[email protected]372bad5f2013-03-21 16:38:43795 int layers_drawn = 0;
796
[email protected]ffbb2212013-06-02 23:47:59797 const DrawMode draw_mode = GetDrawMode(output_surface_.get());
798
[email protected]c1bb5af2013-03-13 19:06:27799 LayerIteratorType end =
[email protected]71dfcc72013-03-20 21:30:09800 LayerIteratorType::End(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27801 for (LayerIteratorType it =
[email protected]71dfcc72013-03-20 21:30:09802 LayerIteratorType::Begin(frame->render_surface_layer_list);
[email protected]c1bb5af2013-03-13 19:06:27803 it != end;
804 ++it) {
805 RenderPass::Id target_render_pass_id =
[email protected]71dfcc72013-03-20 21:30:09806 it.target_render_surface_layer()->render_surface()->RenderPassId();
[email protected]c1bb5af2013-03-13 19:06:27807 RenderPass* target_render_pass =
808 frame->render_passes_by_id[target_render_pass_id];
[email protected]94f206c12012-08-25 00:09:14809
[email protected]e47b0a0a2013-11-18 23:26:22810 occlusion_tracker.EnterLayer(it);
[email protected]94f206c12012-08-25 00:09:14811
[email protected]30fe19ff2013-07-04 00:54:45812 AppendQuadsData append_quads_data(target_render_pass_id);
[email protected]89228202012-08-29 03:20:30813
[email protected]18a70192013-04-26 16:18:25814 if (it.represents_target_render_surface()) {
[email protected]0e5f7142013-05-24 06:45:36815 if (it->HasCopyRequest()) {
[email protected]18a70192013-04-26 16:18:25816 have_copy_request = true;
[email protected]d4d017e2013-06-20 21:46:11817 it->TakeCopyRequestsAndTransformToTarget(
818 &target_render_pass->copy_requests);
[email protected]18a70192013-04-26 16:18:25819 }
[email protected]30fe19ff2013-07-04 00:54:45820 } else if (it.represents_contributing_render_surface() &&
821 it->render_surface()->contributes_to_drawn_surface()) {
[email protected]c1bb5af2013-03-13 19:06:27822 RenderPass::Id contributing_render_pass_id =
823 it->render_surface()->RenderPassId();
824 RenderPass* contributing_render_pass =
825 frame->render_passes_by_id[contributing_render_pass_id];
826 AppendQuadsForRenderSurfaceLayer(target_render_pass,
827 *it,
828 contributing_render_pass,
829 occlusion_tracker,
830 &append_quads_data);
[email protected]22538a2d2013-11-12 11:24:45831 } else if (it.represents_itself() && it->DrawsContent() &&
[email protected]e1e768f2013-03-26 08:48:09832 !it->visible_content_rect().IsEmpty()) {
[email protected]7d6d6c92014-03-06 01:18:50833 bool occluded = occlusion_tracker.Occluded(it->render_target(),
834 it->visible_content_rect(),
835 it->draw_transform());
[email protected]fbc293322013-10-01 05:07:15836 if (!occluded && it->WillDraw(draw_mode, resource_provider_.get())) {
[email protected]c1bb5af2013-03-13 19:06:27837 DCHECK_EQ(active_tree_, it->layer_tree_impl());
[email protected]ffbb2212013-06-02 23:47:59838
[email protected]c1bb5af2013-03-13 19:06:27839 frame->will_draw_layers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57840
[email protected]c1bb5af2013-03-13 19:06:27841 if (it->HasContributingDelegatedRenderPasses()) {
842 RenderPass::Id contributing_render_pass_id =
843 it->FirstContributingRenderPassId();
844 while (frame->render_passes_by_id.find(contributing_render_pass_id) !=
845 frame->render_passes_by_id.end()) {
846 RenderPass* render_pass =
847 frame->render_passes_by_id[contributing_render_pass_id];
[email protected]f5864912013-02-01 03:18:14848
[email protected]c1bb5af2013-03-13 19:06:27849 AppendQuadsData append_quads_data(render_pass->id);
850 AppendQuadsForLayer(render_pass,
851 *it,
852 occlusion_tracker,
853 &append_quads_data);
[email protected]7d929c02012-09-20 17:26:57854
[email protected]c1bb5af2013-03-13 19:06:27855 contributing_render_pass_id =
856 it->NextContributingRenderPassId(contributing_render_pass_id);
857 }
[email protected]94f206c12012-08-25 00:09:14858 }
859
[email protected]c1bb5af2013-03-13 19:06:27860 AppendQuadsForLayer(target_render_pass,
861 *it,
862 occlusion_tracker,
863 &append_quads_data);
864 }
[email protected]89228202012-08-29 03:20:30865
[email protected]372bad5f2013-03-21 16:38:43866 ++layers_drawn;
[email protected]94f206c12012-08-25 00:09:14867 }
868
[email protected]e1e768f2013-03-26 08:48:09869 if (append_quads_data.num_missing_tiles) {
[email protected]c1bb5af2013-03-13 19:06:27870 bool layer_has_animating_transform =
871 it->screen_space_transform_is_animating() ||
872 it->draw_transform_is_animating();
[email protected]7ddfe7e82014-01-30 07:22:11873 if (layer_has_animating_transform) {
874 draw_result =
875 DrawSwapReadbackResult::DRAW_ABORTED_CHECKERBOARD_ANIMATIONS;
876 }
[email protected]c1bb5af2013-03-13 19:06:27877 }
878
[email protected]3d609bb2014-02-01 01:10:23879 if (append_quads_data.had_incomplete_tile) {
[email protected]c1bb5af2013-03-13 19:06:27880 frame->contains_incomplete_tile = true;
[email protected]3d609bb2014-02-01 01:10:23881 if (active_tree()->RequiresHighResToDraw())
882 draw_result =
883 DrawSwapReadbackResult::DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
884 }
[email protected]c1bb5af2013-03-13 19:06:27885
886 occlusion_tracker.LeaveLayer(it);
887 }
888
[email protected]6133cc232013-07-30 18:47:07889 if (have_copy_request ||
890 output_surface_->capabilities().draw_and_swap_full_viewport_every_frame)
[email protected]7ddfe7e82014-01-30 07:22:11891 draw_result = DrawSwapReadbackResult::DRAW_SUCCESS;
[email protected]18a70192013-04-26 16:18:25892
[email protected]767f38d72014-03-18 21:26:41893#if DCHECK_IS_ON
[email protected]c1bb5af2013-03-13 19:06:27894 for (size_t i = 0; i < frame->render_passes.size(); ++i) {
895 for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j)
896 DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state);
897 DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id)
898 != frame->render_passes_by_id.end());
899 }
[email protected]94f206c12012-08-25 00:09:14900#endif
[email protected]c1bb5af2013-03-13 19:06:27901 DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin());
[email protected]94f206c12012-08-25 00:09:14902
[email protected]c1bb5af2013-03-13 19:06:27903 if (!active_tree_->has_transparent_background()) {
904 frame->render_passes.back()->has_transparent_background = false;
[email protected]3744e27b2013-11-06 21:44:08905 AppendQuadsToFillScreen(
906 ResourceIdForUIResource(overhang_ui_resource_id_),
907 gfx::ScaleSize(overhang_ui_resource_size_, device_scale_factor_),
908 active_tree_->RootScrollLayerDeviceViewportBounds(),
909 frame->render_passes.back(),
910 active_tree_->root_layer(),
911 active_tree_->background_color(),
912 occlusion_tracker);
[email protected]c1bb5af2013-03-13 19:06:27913 }
[email protected]94f206c12012-08-25 00:09:14914
[email protected]c1bb5af2013-03-13 19:06:27915 RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame);
[email protected]45948712013-09-27 02:46:48916 renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes);
[email protected]94f206c12012-08-25 00:09:14917
[email protected]30fe19ff2013-07-04 00:54:45918 // Any copy requests left in the tree are not going to get serviced, and
919 // should be aborted.
[email protected]19334ac2013-12-12 23:59:11920 ScopedPtrVector<CopyOutputRequest> requests_to_abort;
921 while (!active_tree_->LayersWithCopyOutputRequest().empty()) {
922 LayerImpl* layer = active_tree_->LayersWithCopyOutputRequest().back();
923 layer->TakeCopyRequestsAndTransformToTarget(&requests_to_abort);
[email protected]30fe19ff2013-07-04 00:54:45924 }
[email protected]19334ac2013-12-12 23:59:11925 for (size_t i = 0; i < requests_to_abort.size(); ++i)
926 requests_to_abort[i]->SendEmptyResult();
[email protected]30fe19ff2013-07-04 00:54:45927
[email protected]e0341352013-04-06 05:01:20928 // If we're making a frame to draw, it better have at least one render pass.
929 DCHECK(!frame->render_passes.empty());
[email protected]45948712013-09-27 02:46:48930
931 // Should only have one render pass in resourceless software mode.
932 if (output_surface_->ForcedDrawToSoftwareDevice())
933 DCHECK_EQ(1u, frame->render_passes.size());
934
[email protected]7ddfe7e82014-01-30 07:22:11935 return draw_result;
[email protected]94f206c12012-08-25 00:09:14936}
937
[email protected]200a9c062013-05-20 04:34:37938void LayerTreeHostImpl::MainThreadHasStoppedFlinging() {
939 if (input_handler_client_)
940 input_handler_client_->MainThreadHasStoppedFlinging();
941}
942
[email protected]3d9f7432013-04-06 00:35:18943void LayerTreeHostImpl::UpdateBackgroundAnimateTicking(
944 bool should_background_tick) {
[email protected]810d40b72013-06-20 18:26:15945 DCHECK(proxy_->IsImplThread());
[email protected]d9fce6722013-08-30 01:10:01946 if (should_background_tick)
947 DCHECK(active_tree_->root_layer());
[email protected]810d40b72013-06-20 18:26:15948
[email protected]cd77bb62014-03-27 05:16:36949 bool enabled = should_background_tick && needs_animate_layers();
[email protected]3d9f7432013-04-06 00:35:18950
[email protected]c1bb5af2013-03-13 19:06:27951 // Lazily create the time_source adapter so that we can vary the interval for
952 // testing.
953 if (!time_source_client_adapter_) {
954 time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create(
955 this,
[email protected]810d40b72013-06-20 18:26:15956 DelayBasedTimeSource::Create(
957 LowFrequencyAnimationInterval(),
958 proxy_->HasImplThread() ? proxy_->ImplThreadTaskRunner()
959 : proxy_->MainThreadTaskRunner()));
[email protected]c1bb5af2013-03-13 19:06:27960 }
[email protected]94f206c12012-08-25 00:09:14961
[email protected]c1bb5af2013-03-13 19:06:27962 time_source_client_adapter_->SetActive(enabled);
[email protected]94f206c12012-08-25 00:09:14963}
964
[email protected]b8384e22013-12-03 02:20:48965void LayerTreeHostImpl::DidAnimateScrollOffset() {
966 client_->SetNeedsCommitOnImplThread();
967 client_->RenewTreePriority();
968}
969
[email protected]0023fc72014-01-10 20:05:06970void LayerTreeHostImpl::SetViewportDamage(const gfx::Rect& damage_rect) {
[email protected]878705be2013-04-15 22:44:02971 viewport_damage_rect_.Union(damage_rect);
972}
973
[email protected]c1bb5af2013-03-13 19:06:27974static inline RenderPass* FindRenderPassById(
975 RenderPass::Id render_pass_id,
976 const LayerTreeHostImpl::FrameData& frame) {
977 RenderPassIdHashMap::const_iterator it =
978 frame.render_passes_by_id.find(render_pass_id);
979 return it != frame.render_passes_by_id.end() ? it->second : NULL;
[email protected]94f206c12012-08-25 00:09:14980}
981
[email protected]c1bb5af2013-03-13 19:06:27982static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id,
983 LayerTreeHostImpl::FrameData* frame) {
984 RenderPass* remove_render_pass =
985 FindRenderPassById(remove_render_pass_id, *frame);
986 // The pass was already removed by another quad - probably the original, and
987 // we are the replica.
988 if (!remove_render_pass)
989 return;
990 RenderPassList& render_passes = frame->render_passes;
991 RenderPassList::iterator to_remove = std::find(render_passes.begin(),
992 render_passes.end(),
993 remove_render_pass);
[email protected]94f206c12012-08-25 00:09:14994
[email protected]c1bb5af2013-03-13 19:06:27995 DCHECK(to_remove != render_passes.end());
[email protected]94f206c12012-08-25 00:09:14996
[email protected]c1bb5af2013-03-13 19:06:27997 scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove);
998 frame->render_passes.erase(to_remove);
999 frame->render_passes_by_id.erase(remove_render_pass_id);
[email protected]94f206c12012-08-25 00:09:141000
[email protected]c1bb5af2013-03-13 19:06:271001 // Now follow up for all RenderPass quads and remove their RenderPasses
1002 // recursively.
1003 const QuadList& quad_list = removed_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291004 QuadList::ConstBackToFrontIterator quad_list_iterator =
1005 quad_list.BackToFrontBegin();
1006 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271007 ++quad_list_iterator) {
1008 DrawQuad* current_quad = (*quad_list_iterator);
1009 if (current_quad->material != DrawQuad::RENDER_PASS)
1010 continue;
[email protected]94f206c12012-08-25 00:09:141011
[email protected]c1bb5af2013-03-13 19:06:271012 RenderPass::Id next_remove_render_pass_id =
1013 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id;
1014 RemoveRenderPassesRecursive(next_remove_render_pass_id, frame);
1015 }
[email protected]94f206c12012-08-25 00:09:141016}
1017
[email protected]c1bb5af2013-03-13 19:06:271018bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass(
1019 const RenderPassDrawQuad& quad, const FrameData& frame) const {
1020 const RenderPass* render_pass =
1021 FindRenderPassById(quad.render_pass_id, frame);
1022 if (!render_pass)
1023 return false;
[email protected]94f206c12012-08-25 00:09:141024
[email protected]c1bb5af2013-03-13 19:06:271025 // If any quad or RenderPass draws into this RenderPass, then keep it.
1026 const QuadList& quad_list = render_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291027 for (QuadList::ConstBackToFrontIterator quad_list_iterator =
1028 quad_list.BackToFrontBegin();
1029 quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271030 ++quad_list_iterator) {
1031 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141032
[email protected]c1bb5af2013-03-13 19:06:271033 if (current_quad->material != DrawQuad::RENDER_PASS)
1034 return false;
[email protected]94f206c12012-08-25 00:09:141035
[email protected]c1bb5af2013-03-13 19:06:271036 const RenderPass* contributing_pass = FindRenderPassById(
1037 RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame);
1038 if (contributing_pass)
1039 return false;
1040 }
1041 return true;
[email protected]94f206c12012-08-25 00:09:141042}
1043
1044// Defined for linking tests.
[email protected]c1bb5af2013-03-13 19:06:271045template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses<
[email protected]c1bb5af2013-03-13 19:06:271046 LayerTreeHostImpl::CullRenderPassesWithNoQuads>(
1047 CullRenderPassesWithNoQuads culler, FrameData*);
[email protected]94f206c12012-08-25 00:09:141048
1049// static
[email protected]c1bb5af2013-03-13 19:06:271050template <typename RenderPassCuller>
1051void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler,
1052 FrameData* frame) {
1053 for (size_t it = culler.RenderPassListBegin(frame->render_passes);
1054 it != culler.RenderPassListEnd(frame->render_passes);
1055 it = culler.RenderPassListNext(it)) {
1056 const RenderPass* current_pass = frame->render_passes[it];
1057 const QuadList& quad_list = current_pass->quad_list;
[email protected]ed511b8d2013-03-25 03:29:291058 QuadList::ConstBackToFrontIterator quad_list_iterator =
1059 quad_list.BackToFrontBegin();
[email protected]94f206c12012-08-25 00:09:141060
[email protected]ed511b8d2013-03-25 03:29:291061 for (; quad_list_iterator != quad_list.BackToFrontEnd();
[email protected]c1bb5af2013-03-13 19:06:271062 ++quad_list_iterator) {
1063 DrawQuad* current_quad = *quad_list_iterator;
[email protected]94f206c12012-08-25 00:09:141064
[email protected]c1bb5af2013-03-13 19:06:271065 if (current_quad->material != DrawQuad::RENDER_PASS)
1066 continue;
[email protected]94f206c12012-08-25 00:09:141067
[email protected]c1bb5af2013-03-13 19:06:271068 const RenderPassDrawQuad* render_pass_quad =
1069 RenderPassDrawQuad::MaterialCast(current_quad);
1070 if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame))
1071 continue;
[email protected]94f206c12012-08-25 00:09:141072
[email protected]c1bb5af2013-03-13 19:06:271073 // We are changing the vector in the middle of iteration. Because we
1074 // delete render passes that draw into the current pass, we are
1075 // guaranteed that any data from the iterator to the end will not
1076 // change. So, capture the iterator position from the end of the
1077 // list, and restore it after the change.
1078 size_t position_from_end = frame->render_passes.size() - it;
1079 RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame);
1080 it = frame->render_passes.size() - position_from_end;
1081 DCHECK_GE(frame->render_passes.size(), position_from_end);
[email protected]94f206c12012-08-25 00:09:141082 }
[email protected]c1bb5af2013-03-13 19:06:271083 }
[email protected]94f206c12012-08-25 00:09:141084}
1085
[email protected]7ddfe7e82014-01-30 07:22:111086DrawSwapReadbackResult::DrawResult LayerTreeHostImpl::PrepareToDraw(
1087 FrameData* frame,
1088 const gfx::Rect& damage_rect) {
[email protected]7a52f43e2013-07-10 01:58:471089 TRACE_EVENT1("cc",
1090 "LayerTreeHostImpl::PrepareToDraw",
1091 "SourceFrameNumber",
1092 active_tree_->source_frame_number());
[email protected]94f206c12012-08-25 00:09:141093
[email protected]425e5332013-09-21 04:10:031094 if (need_to_update_visible_tiles_before_draw_ &&
1095 tile_manager_ && tile_manager_->UpdateVisibleTiles()) {
1096 DidInitializeVisibleTile();
[email protected]39643fb2013-07-09 17:28:191097 }
[email protected]425e5332013-09-21 04:10:031098 need_to_update_visible_tiles_before_draw_ = true;
[email protected]39643fb2013-07-09 17:28:191099
[email protected]7d19dc342013-05-02 22:02:041100 active_tree_->UpdateDrawProperties();
[email protected]2e7ca422012-12-20 02:57:271101
[email protected]c1bb5af2013-03-13 19:06:271102 frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList();
1103 frame->render_passes.clear();
1104 frame->render_passes_by_id.clear();
1105 frame->will_draw_layers.clear();
[email protected]e0341352013-04-06 05:01:201106 frame->contains_incomplete_tile = false;
1107 frame->has_no_damage = false;
1108
[email protected]0023fc72014-01-10 20:05:061109 gfx::Rect device_viewport_damage_rect(damage_rect);
[email protected]e0341352013-04-06 05:01:201110 if (active_tree_->root_layer()) {
[email protected]878705be2013-04-15 22:44:021111 device_viewport_damage_rect.Union(viewport_damage_rect_);
1112 viewport_damage_rect_ = gfx::Rect();
[email protected]e0341352013-04-06 05:01:201113
1114 active_tree_->root_layer()->render_surface()->damage_tracker()->
1115 AddDamageNextUpdate(device_viewport_damage_rect);
1116 }
[email protected]94f206c12012-08-25 00:09:141117
[email protected]7ddfe7e82014-01-30 07:22:111118 DrawSwapReadbackResult::DrawResult draw_result = CalculateRenderPasses(frame);
1119 if (draw_result != DrawSwapReadbackResult::DRAW_SUCCESS) {
[email protected]6133cc232013-07-30 18:47:071120 DCHECK(!output_surface_->capabilities()
1121 .draw_and_swap_full_viewport_every_frame);
[email protected]7ddfe7e82014-01-30 07:22:111122 return draw_result;
[email protected]6133cc232013-07-30 18:47:071123 }
[email protected]94f206c12012-08-25 00:09:141124
[email protected]7ddfe7e82014-01-30 07:22:111125 // If we return DRAW_SUCCESS, then we expect DrawLayers() to be called before
1126 // this function is called again.
1127 return draw_result;
[email protected]94f206c12012-08-25 00:09:141128}
1129
[email protected]0309ba5e02013-06-26 04:11:081130void LayerTreeHostImpl::EvictTexturesForTesting() {
1131 EnforceManagedMemoryPolicy(ManagedMemoryPolicy(0));
1132}
1133
[email protected]4f48f6e2013-08-27 06:33:381134void LayerTreeHostImpl::BlockNotifyReadyToActivateForTesting(bool block) {
1135 NOTREACHED();
1136}
1137
[email protected]425e5332013-09-21 04:10:031138void LayerTreeHostImpl::DidInitializeVisibleTileForTesting() {
1139 DidInitializeVisibleTile();
1140}
1141
[email protected]c1bb5af2013-03-13 19:06:271142void LayerTreeHostImpl::EnforceManagedMemoryPolicy(
1143 const ManagedMemoryPolicy& policy) {
[email protected]206a3922013-05-17 06:34:121144
[email protected]c1bb5af2013-03-13 19:06:271145 bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread(
[email protected]0b8ef2842013-11-01 09:53:351146 visible_ ? policy.bytes_limit_when_visible : 0,
[email protected]46b8acc2013-03-19 22:38:351147 ManagedMemoryPolicy::PriorityCutoffToValue(
1148 visible_ ? policy.priority_cutoff_when_visible
[email protected]0b8ef2842013-11-01 09:53:351149 : gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING));
[email protected]c1bb5af2013-03-13 19:06:271150 if (evicted_resources) {
1151 active_tree_->SetContentsTexturesPurged();
1152 if (pending_tree_)
1153 pending_tree_->SetContentsTexturesPurged();
1154 client_->SetNeedsCommitOnImplThread();
1155 client_->OnCanDrawStateChanged(CanDraw());
1156 client_->RenewTreePriority();
1157 }
1158 client_->SendManagedMemoryStats();
[email protected]8947cbe2012-11-28 05:27:431159
[email protected]8be1a9bf2013-05-01 03:45:191160 UpdateTileManagerMemoryPolicy(policy);
1161}
1162
1163void LayerTreeHostImpl::UpdateTileManagerMemoryPolicy(
1164 const ManagedMemoryPolicy& policy) {
1165 if (!tile_manager_)
1166 return;
1167
[email protected]8be1a9bf2013-05-01 03:45:191168 // TODO(reveman): We should avoid keeping around unused resources if
1169 // possible. crbug.com/224475
[email protected]25489c02014-02-13 23:18:411170 global_tile_state_.hard_memory_limit_in_bytes = 0;
1171 global_tile_state_.soft_memory_limit_in_bytes = 0;
1172 if (visible_ && policy.bytes_limit_when_visible > 0) {
1173 global_tile_state_.hard_memory_limit_in_bytes =
1174 policy.bytes_limit_when_visible;
1175 global_tile_state_.soft_memory_limit_in_bytes =
[email protected]831414e2014-02-22 02:27:371176 (static_cast<int64>(global_tile_state_.hard_memory_limit_in_bytes) *
1177 settings_.max_memory_for_prepaint_percentage) /
1178 100;
[email protected]25489c02014-02-13 23:18:411179 }
1180 // Unused limit is calculated from soft-limit, as hard-limit may
1181 // be very high and shouldn't typically be exceeded.
[email protected]1bcced22013-09-24 13:51:191182 global_tile_state_.unused_memory_limit_in_bytes = static_cast<size_t>(
[email protected]25489c02014-02-13 23:18:411183 (static_cast<int64>(global_tile_state_.soft_memory_limit_in_bytes) *
1184 settings_.max_unused_resource_memory_percentage) /
1185 100);
[email protected]1bcced22013-09-24 13:51:191186 global_tile_state_.memory_limit_policy =
[email protected]8be1a9bf2013-05-01 03:45:191187 ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy(
1188 visible_ ?
1189 policy.priority_cutoff_when_visible :
[email protected]0b8ef2842013-11-01 09:53:351190 gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING);
[email protected]1bcced22013-09-24 13:51:191191 global_tile_state_.num_resources_limit = policy.num_resources_limit;
[email protected]c48536a52013-09-14 00:02:081192
[email protected]c48536a52013-09-14 00:02:081193 DidModifyTilePriorities();
1194}
1195
1196void LayerTreeHostImpl::DidModifyTilePriorities() {
1197 DCHECK(settings_.impl_side_painting);
1198 // Mark priorities as dirty and schedule a ManageTiles().
1199 tile_priorities_dirty_ = true;
1200 client_->SetNeedsManageTilesOnImplThread();
[email protected]94f206c12012-08-25 00:09:141201}
1202
[email protected]86126792013-03-16 20:07:541203void LayerTreeHostImpl::DidInitializeVisibleTile() {
1204 // TODO(reveman): Determine tiles that changed and only damage
1205 // what's necessary.
1206 SetFullRootLayerDamage();
[email protected]425e5332013-09-21 04:10:031207 if (client_ && !client_->IsInsideDraw())
[email protected]86126792013-03-16 20:07:541208 client_->DidInitializeVisibleTileOnImplThread();
[email protected]74d9063c2013-01-18 03:14:471209}
1210
[email protected]94bf75c2013-06-12 13:20:041211void LayerTreeHostImpl::NotifyReadyToActivate() {
[email protected]4f48f6e2013-08-27 06:33:381212 client_->NotifyReadyToActivate();
[email protected]94bf75c2013-06-12 13:20:041213}
1214
[email protected]3f2ff112013-08-03 02:41:071215void LayerTreeHostImpl::SetMemoryPolicy(const ManagedMemoryPolicy& policy) {
1216 SetManagedMemoryPolicy(policy, zero_budget_);
1217}
1218
[email protected]5f07afc2013-07-19 05:17:181219void LayerTreeHostImpl::SetTreeActivationCallback(
1220 const base::Closure& callback) {
1221 DCHECK(proxy_->IsImplThread());
1222 DCHECK(settings_.impl_side_painting || callback.is_null());
[email protected]2022c672013-07-23 19:55:261223 tree_activation_callback_ = callback;
[email protected]5f07afc2013-07-19 05:17:181224}
1225
[email protected]c1bb5af2013-03-13 19:06:271226void LayerTreeHostImpl::SetManagedMemoryPolicy(
[email protected]3f2ff112013-08-03 02:41:071227 const ManagedMemoryPolicy& policy, bool zero_budget) {
1228 if (cached_managed_memory_policy_ == policy && zero_budget_ == zero_budget)
[email protected]c1bb5af2013-03-13 19:06:271229 return;
[email protected]61de5812012-11-08 07:03:441230
[email protected]3f2ff112013-08-03 02:41:071231 ManagedMemoryPolicy old_policy = ActualManagedMemoryPolicy();
1232
1233 cached_managed_memory_policy_ = policy;
1234 zero_budget_ = zero_budget;
1235 ManagedMemoryPolicy actual_policy = ActualManagedMemoryPolicy();
1236
1237 if (old_policy == actual_policy)
1238 return;
1239
1240 if (!proxy_->HasImplThread()) {
1241 // In single-thread mode, this can be called on the main thread by
1242 // GLRenderer::OnMemoryAllocationChanged.
1243 DebugScopedSetImplThread impl_thread(proxy_);
1244 EnforceManagedMemoryPolicy(actual_policy);
1245 } else {
1246 DCHECK(proxy_->IsImplThread());
1247 EnforceManagedMemoryPolicy(actual_policy);
1248 }
[email protected]50644642013-06-20 13:58:551249
[email protected]d7626ffd2013-03-29 00:17:421250 // If there is already enough memory to draw everything imaginable and the
1251 // new memory limit does not change this, then do not re-commit. Don't bother
1252 // skipping commits if this is not visible (commits don't happen when not
1253 // visible, there will almost always be a commit when this becomes visible).
1254 bool needs_commit = true;
1255 if (visible() &&
[email protected]3f2ff112013-08-03 02:41:071256 actual_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1257 old_policy.bytes_limit_when_visible >= max_memory_needed_bytes_ &&
1258 actual_policy.priority_cutoff_when_visible ==
1259 old_policy.priority_cutoff_when_visible) {
[email protected]d7626ffd2013-03-29 00:17:421260 needs_commit = false;
1261 }
1262
[email protected]d7626ffd2013-03-29 00:17:421263 if (needs_commit)
1264 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:141265}
1266
[email protected]f224cc92013-06-06 23:23:321267void LayerTreeHostImpl::SetExternalDrawConstraints(
1268 const gfx::Transform& transform,
[email protected]0023fc72014-01-10 20:05:061269 const gfx::Rect& viewport,
1270 const gfx::Rect& clip,
[email protected]54af03522013-09-05 00:43:281271 bool valid_for_tile_management) {
[email protected]f224cc92013-06-06 23:23:321272 external_transform_ = transform;
1273 external_viewport_ = viewport;
[email protected]54af03522013-09-05 00:43:281274 external_clip_ = clip;
1275 device_viewport_valid_for_tile_management_ = valid_for_tile_management;
[email protected]f224cc92013-06-06 23:23:321276}
1277
[email protected]0023fc72014-01-10 20:05:061278void LayerTreeHostImpl::SetNeedsRedrawRect(const gfx::Rect& damage_rect) {
[email protected]6be422b2013-12-08 06:47:311279 if (damage_rect.IsEmpty())
1280 return;
1281 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
[email protected]1cd9f5552013-04-26 04:22:031282 client_->SetNeedsRedrawRectOnImplThread(damage_rect);
1283}
1284
[email protected]97e9ccd62014-04-12 01:07:151285void LayerTreeHostImpl::BeginFrame(const BeginFrameArgs& args) {
1286 client_->BeginFrame(args);
[email protected]7ed47512013-03-26 22:28:451287}
1288
[email protected]4d7e46a2013-11-08 05:33:401289void LayerTreeHostImpl::DidSwapBuffers() {
1290 client_->DidSwapBuffersOnImplThread();
1291}
1292
[email protected]a7335e0b2013-09-18 09:34:511293void LayerTreeHostImpl::OnSwapBuffersComplete() {
1294 client_->OnSwapBuffersCompleteOnImplThread();
1295}
1296
1297void LayerTreeHostImpl::ReclaimResources(const CompositorFrameAck* ack) {
[email protected]c1bb5af2013-03-13 19:06:271298 // TODO(piman): We may need to do some validation on this ack before
1299 // processing it.
[email protected]a7335e0b2013-09-18 09:34:511300 if (renderer_)
[email protected]36e5ff12013-06-11 12:19:291301 renderer_->ReceiveSwapBuffersAck(*ack);
[email protected]43b552672014-04-05 00:38:571302
1303 // In OOM, we now might be able to release more resources that were held
1304 // because they were exported.
1305 if (tile_manager_) {
1306 DCHECK(tile_manager_->resource_pool());
1307
1308 // TODO(vmpstr): Move resource pool to be LTHI member.
1309 tile_manager_->resource_pool()->CheckBusyResources();
1310 tile_manager_->resource_pool()->ReduceResourceUsage();
1311 }
[email protected]e97908d2014-04-09 12:48:041312 // If we're not visible, we likely released resources, so we want to
1313 // aggressively flush here to make sure those DeleteTextures make it to the
1314 // GPU process to free up the memory.
1315 if (resource_provider_ && !visible_)
1316 resource_provider_->ShallowFlushIfSupported();
[email protected]a46f32932012-12-07 21:43:161317}
1318
[email protected]c1bb5af2013-03-13 19:06:271319void LayerTreeHostImpl::OnCanDrawStateChangedForTree() {
1320 client_->OnCanDrawStateChanged(CanDraw());
[email protected]3b31c6ac2012-12-06 21:27:291321}
1322
[email protected]c1bb5af2013-03-13 19:06:271323CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const {
1324 CompositorFrameMetadata metadata;
1325 metadata.device_scale_factor = device_scale_factor_;
1326 metadata.page_scale_factor = active_tree_->total_page_scale_factor();
1327 metadata.viewport_size = active_tree_->ScrollableViewportSize();
1328 metadata.root_layer_size = active_tree_->ScrollableSize();
1329 metadata.min_page_scale_factor = active_tree_->min_page_scale_factor();
1330 metadata.max_page_scale_factor = active_tree_->max_page_scale_factor();
1331 if (top_controls_manager_) {
1332 metadata.location_bar_offset =
1333 gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset());
1334 metadata.location_bar_content_translation =
1335 gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset());
[email protected]5a54b2822013-03-26 10:00:011336 metadata.overdraw_bottom_height = overdraw_bottom_height_;
[email protected]c1bb5af2013-03-13 19:06:271337 }
[email protected]bf189f62012-12-18 03:42:111338
[email protected]adeda572014-01-31 00:49:471339 if (!InnerViewportScrollLayer())
[email protected]bf189f62012-12-18 03:42:111340 return metadata;
[email protected]c1bb5af2013-03-13 19:06:271341
[email protected]adeda572014-01-31 00:49:471342 metadata.root_scroll_offset = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:271343
1344 return metadata;
[email protected]bf189f62012-12-18 03:42:111345}
1346
[email protected]6f50b8fa2013-07-01 19:51:491347static void LayerTreeHostImplDidBeginTracingCallback(LayerImpl* layer) {
1348 layer->DidBeginTracing();
1349}
[email protected]37349bc2013-06-04 01:31:521350
[email protected]f0c2a242013-03-15 19:34:521351void LayerTreeHostImpl::DrawLayers(FrameData* frame,
1352 base::TimeTicks frame_begin_time) {
[email protected]c1e6cc062013-08-24 03:35:351353 TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers");
[email protected]c1bb5af2013-03-13 19:06:271354 DCHECK(CanDraw());
[email protected]e0341352013-04-06 05:01:201355
[email protected]3519b872013-07-30 07:17:501356 if (frame->has_no_damage) {
[email protected]87b180cc2014-02-06 21:19:191357 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoDamage", TRACE_EVENT_SCOPE_THREAD);
[email protected]6133cc232013-07-30 18:47:071358 DCHECK(!output_surface_->capabilities()
1359 .draw_and_swap_full_viewport_every_frame);
[email protected]e0341352013-04-06 05:01:201360 return;
[email protected]3519b872013-07-30 07:17:501361 }
[email protected]e0341352013-04-06 05:01:201362
[email protected]c1bb5af2013-03-13 19:06:271363 DCHECK(!frame->render_passes.empty());
[email protected]94f206c12012-08-25 00:09:141364
[email protected]213fcfc2013-08-24 14:59:271365 fps_counter_->SaveTimeStamp(frame_begin_time,
1366 !output_surface_->context_provider());
[email protected]c1e6cc062013-08-24 03:35:351367 bool on_main_thread = false;
[email protected]922c6e1f2013-10-09 04:04:091368 rendering_stats_instrumentation_->IncrementFrameCount(
[email protected]c1e6cc062013-08-24 03:35:351369 1, on_main_thread);
[email protected]372bad5f2013-03-21 16:38:431370
[email protected]c1bb5af2013-03-13 19:06:271371 if (tile_manager_) {
1372 memory_history_->SaveEntry(
1373 tile_manager_->memory_stats_from_last_assign());
1374 }
[email protected]1191d9d2013-02-02 06:00:331375
[email protected]846f455b2013-03-18 19:07:411376 if (debug_state_.ShowHudRects()) {
[email protected]d35992782013-03-14 14:54:021377 debug_rect_history_->SaveDebugRectsForCurrentFrame(
[email protected]c1bb5af2013-03-13 19:06:271378 active_tree_->root_layer(),
[email protected]f636ce82014-03-10 22:33:081379 active_tree_->hud_layer(),
[email protected]c1bb5af2013-03-13 19:06:271380 *frame->render_surface_layer_list,
1381 frame->occluding_screen_space_rects,
1382 frame->non_occluding_screen_space_rects,
1383 debug_state_);
1384 }
[email protected]94f206c12012-08-25 00:09:141385
[email protected]a848c102013-03-26 08:59:091386 if (!settings_.impl_side_painting && debug_state_.continuous_painting) {
1387 const RenderingStats& stats =
1388 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]a9dc0d0f2013-08-17 02:43:181389 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time);
[email protected]a848c102013-03-26 08:59:091390 }
1391
[email protected]37349bc2013-06-04 01:31:521392 bool is_new_trace;
1393 TRACE_EVENT_IS_NEW_TRACE(&is_new_trace);
1394 if (is_new_trace) {
[email protected]6f50b8fa2013-07-01 19:51:491395 if (pending_tree_) {
1396 LayerTreeHostCommon::CallFunctionForSubtree(
1397 pending_tree_->root_layer(),
1398 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
1399 }
1400 LayerTreeHostCommon::CallFunctionForSubtree(
1401 active_tree_->root_layer(),
1402 base::Bind(&LayerTreeHostImplDidBeginTracingCallback));
[email protected]37349bc2013-06-04 01:31:521403 }
1404
[email protected]9197dbcb2013-05-15 20:28:511405 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
[email protected]34806722013-08-09 23:51:211406 TRACE_DISABLED_BY_DEFAULT("cc.debug") ","
1407 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"), "cc::LayerTreeHostImpl",
1408 this, TracedValue::FromValue(AsValueWithFrame(frame).release()));
[email protected]131a0c22013-02-12 18:31:081409
[email protected]c1bb5af2013-03-13 19:06:271410 // Because the contents of the HUD depend on everything else in the frame, the
1411 // contents of its texture are updated as the last thing before the frame is
1412 // drawn.
[email protected]671f6672013-10-01 07:31:071413 if (active_tree_->hud_layer()) {
1414 TRACE_EVENT0("cc", "DrawLayers.UpdateHudTexture");
[email protected]b4f94452013-10-11 19:28:531415 active_tree_->hud_layer()->UpdateHudTexture(
1416 GetDrawMode(output_surface_.get()), resource_provider_.get());
[email protected]671f6672013-10-01 07:31:071417 }
[email protected]94f206c12012-08-25 00:09:141418
[email protected]b09c1942013-05-10 00:06:371419 if (output_surface_->ForcedDrawToSoftwareDevice()) {
[email protected]251699b2013-10-09 00:21:261420 bool disable_picture_quad_image_filtering =
1421 IsCurrentlyScrolling() || needs_animate_layers();
[email protected]573e8e32013-09-09 17:24:321422
[email protected]b09c1942013-05-10 00:06:371423 scoped_ptr<SoftwareRenderer> temp_software_renderer =
[email protected]573e8e32013-09-09 17:24:321424 SoftwareRenderer::Create(this, &settings_, output_surface_.get(), NULL);
[email protected]59fee302013-12-10 17:53:301425 temp_software_renderer->DrawFrame(&frame->render_passes,
1426 NULL,
1427 device_scale_factor_,
1428 DeviceViewport(),
1429 DeviceClip(),
[email protected]59fee302013-12-10 17:53:301430 disable_picture_quad_image_filtering);
[email protected]b09c1942013-05-10 00:06:371431 } else {
[email protected]b5174d712013-08-28 08:10:431432 renderer_->DrawFrame(&frame->render_passes,
[email protected]573e8e32013-09-09 17:24:321433 offscreen_context_provider_.get(),
1434 device_scale_factor_,
[email protected]59fee302013-12-10 17:53:301435 DeviceViewport(),
1436 DeviceClip(),
[email protected]251699b2013-10-09 00:21:261437 false);
[email protected]b09c1942013-05-10 00:06:371438 }
[email protected]c1bb5af2013-03-13 19:06:271439 // The render passes should be consumed by the renderer.
1440 DCHECK(frame->render_passes.empty());
1441 frame->render_passes_by_id.clear();
[email protected]94f206c12012-08-25 00:09:141442
[email protected]c1bb5af2013-03-13 19:06:271443 // The next frame should start by assuming nothing has changed, and changes
1444 // are noted as they occur.
[email protected]573e8e32013-09-09 17:24:321445 // TODO(boliu): If we did a temporary software renderer frame, propogate the
1446 // damage forward to the next frame.
[email protected]264dc0332013-03-17 21:00:541447 for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) {
[email protected]c1bb5af2013-03-13 19:06:271448 (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()->
1449 DidDrawDamagedArea();
1450 }
1451 active_tree_->root_layer()->ResetAllChangeTrackingForSubtree();
[email protected]c1e6cc062013-08-24 03:35:351452
[email protected]d73094482014-02-13 21:28:281453 devtools_instrumentation::DidDrawFrame(id_);
[email protected]adbe30f2013-10-11 21:12:331454 BenchmarkInstrumentation::IssueImplThreadRenderingStatsEvent(
1455 rendering_stats_instrumentation_->impl_thread_rendering_stats());
[email protected]a9dc0d0f2013-08-17 02:43:181456 rendering_stats_instrumentation_->AccumulateAndClearImplThreadStats();
[email protected]94f206c12012-08-25 00:09:141457}
1458
[email protected]c1bb5af2013-03-13 19:06:271459void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) {
1460 for (size_t i = 0; i < frame.will_draw_layers.size(); ++i)
1461 frame.will_draw_layers[i]->DidDraw(resource_provider_.get());
[email protected]b914e102012-10-02 08:11:521462
[email protected]c1bb5af2013-03-13 19:06:271463 // Once all layers have been drawn, pending texture uploads should no
1464 // longer block future uploads.
[email protected]d15d9622013-06-21 02:49:151465 resource_provider_->MarkPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:141466}
1467
[email protected]c1bb5af2013-03-13 19:06:271468void LayerTreeHostImpl::FinishAllRendering() {
1469 if (renderer_)
1470 renderer_->Finish();
[email protected]94f206c12012-08-25 00:09:141471}
1472
[email protected]c1bb5af2013-03-13 19:06:271473bool LayerTreeHostImpl::IsContextLost() {
1474 DCHECK(proxy_->IsImplThread());
1475 return renderer_ && renderer_->IsContextLost();
[email protected]94f206c12012-08-25 00:09:141476}
1477
[email protected]7a8bcd262014-01-15 12:54:581478const RendererCapabilitiesImpl&
1479LayerTreeHostImpl::GetRendererCapabilities() const {
[email protected]c1bb5af2013-03-13 19:06:271480 return renderer_->Capabilities();
[email protected]94f206c12012-08-25 00:09:141481}
1482
[email protected]e0341352013-04-06 05:01:201483bool LayerTreeHostImpl::SwapBuffers(const LayerTreeHostImpl::FrameData& frame) {
[email protected]3d609bb2014-02-01 01:10:231484 active_tree()->ResetRequiresHighResToDraw();
[email protected]b69c1db2013-11-27 00:05:191485 if (frame.has_no_damage) {
1486 active_tree()->BreakSwapPromises(SwapPromise::SWAP_FAILS);
[email protected]e0341352013-04-06 05:01:201487 return false;
[email protected]b69c1db2013-11-27 00:05:191488 }
[email protected]4a28a612013-11-27 02:06:331489 CompositorFrameMetadata metadata = MakeCompositorFrameMetadata();
[email protected]d359203a2013-11-29 06:16:551490 active_tree()->FinishSwapPromises(&metadata);
[email protected]15ba6dea2014-04-02 01:44:131491 for (size_t i = 0; i < metadata.latency_info.size(); i++) {
1492 TRACE_EVENT_FLOW_STEP0(
1493 "input",
1494 "LatencyInfo.Flow",
1495 TRACE_ID_DONT_MANGLE(metadata.latency_info[i].trace_id),
1496 "SwapBuffers");
1497 }
[email protected]4a28a612013-11-27 02:06:331498 renderer_->SwapBuffers(metadata);
[email protected]c5c506382013-04-30 04:42:161499 return true;
[email protected]94f206c12012-08-25 00:09:141500}
1501
[email protected]97e9ccd62014-04-12 01:07:151502void LayerTreeHostImpl::SetNeedsBeginFrame(bool enable) {
[email protected]7ed47512013-03-26 22:28:451503 if (output_surface_)
[email protected]97e9ccd62014-04-12 01:07:151504 output_surface_->SetNeedsBeginFrame(enable);
1505}
1506
1507void LayerTreeHostImpl::WillBeginImplFrame(const BeginFrameArgs& args) {
1508 // Sample the frame time now. This time will be used for updating animations
1509 // when we draw.
1510 UpdateCurrentFrameTime();
[email protected]7ed47512013-03-26 22:28:451511}
1512
[email protected]fef74fd2014-02-27 06:28:171513gfx::SizeF LayerTreeHostImpl::ComputeInnerViewportContainerSize() const {
1514 gfx::SizeF dip_size =
1515 gfx::ScaleSize(device_viewport_size_, 1.f / device_scale_factor());
1516
1517 float top_offset =
1518 top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f;
1519
1520 return gfx::SizeF(dip_size.width(),
1521 dip_size.height() - top_offset - overdraw_bottom_height_);
1522}
1523
1524void LayerTreeHostImpl::UpdateInnerViewportContainerSize() {
1525 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
1526 if (!container_layer)
1527 return;
1528
[email protected]05dfe4f2014-02-27 19:52:501529 // We pass the value returned from UnscaledScrollableViewportSize() here as
1530 // it accounts for scrollbar dimensions when
1531 // container_layer->masks_to_bounds() is set.
1532 container_layer->SetTemporaryImplBounds(UnscaledScrollableViewportSize());
[email protected]fef74fd2014-02-27 06:28:171533}
1534
[email protected]54af03522013-09-05 00:43:281535gfx::SizeF LayerTreeHostImpl::UnscaledScrollableViewportSize() const {
[email protected]eb8b32ee2013-10-20 08:30:341536 // Use the root container layer bounds if it clips to them, otherwise, the
1537 // true viewport size should be used.
[email protected]fef74fd2014-02-27 06:28:171538 LayerImpl* container_layer = active_tree_->InnerViewportContainerLayer();
[email protected]eb8b32ee2013-10-20 08:30:341539 if (container_layer && container_layer->masks_to_bounds()) {
[email protected]4f0f43a2013-07-31 17:44:441540 DCHECK(!top_controls_manager_);
1541 DCHECK_EQ(0, overdraw_bottom_height_);
1542 return container_layer->bounds();
1543 }
1544
[email protected]fef74fd2014-02-27 06:28:171545 return ComputeInnerViewportContainerSize();
1546}
[email protected]ffb2720f2013-03-15 19:18:371547
[email protected]fef74fd2014-02-27 06:28:171548float LayerTreeHostImpl::VerticalAdjust() const {
1549 if (!active_tree_->InnerViewportContainerLayer())
1550 return 0;
1551
1552 return active_tree_->InnerViewportContainerLayer()->BoundsDelta().y();
[email protected]ffb2720f2013-03-15 19:18:371553}
1554
[email protected]c1bb5af2013-03-13 19:06:271555void LayerTreeHostImpl::DidLoseOutputSurface() {
[email protected]13044fe72013-12-02 20:52:191556 if (resource_provider_)
1557 resource_provider_->DidLoseOutputSurface();
[email protected]bb1e2822013-04-17 22:06:011558 // TODO(jamesr): The renderer_ check is needed to make some of the
1559 // LayerTreeHostContextTest tests pass, but shouldn't be necessary (or
1560 // important) in production. We should adjust the test to not need this.
1561 if (renderer_)
1562 client_->DidLoseOutputSurfaceOnImplThread();
[email protected]767f38d72014-03-18 21:26:411563#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361564 did_lose_called_ = true;
1565#endif
[email protected]94f206c12012-08-25 00:09:141566}
1567
[email protected]c1bb5af2013-03-13 19:06:271568void LayerTreeHostImpl::Readback(void* pixels,
[email protected]0023fc72014-01-10 20:05:061569 const gfx::Rect& rect_in_device_viewport) {
[email protected]c1bb5af2013-03-13 19:06:271570 DCHECK(renderer_);
1571 renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport);
[email protected]94f206c12012-08-25 00:09:141572}
1573
[email protected]59adb112013-04-09 04:48:441574bool LayerTreeHostImpl::HaveRootScrollLayer() const {
[email protected]adeda572014-01-31 00:49:471575 return !!InnerViewportScrollLayer();
[email protected]69b50ec2013-01-19 04:58:011576}
1577
[email protected]c1bb5af2013-03-13 19:06:271578LayerImpl* LayerTreeHostImpl::RootLayer() const {
1579 return active_tree_->root_layer();
[email protected]8bef40572012-12-11 21:38:081580}
1581
[email protected]adeda572014-01-31 00:49:471582LayerImpl* LayerTreeHostImpl::InnerViewportScrollLayer() const {
1583 return active_tree_->InnerViewportScrollLayer();
1584}
1585
1586LayerImpl* LayerTreeHostImpl::OuterViewportScrollLayer() const {
1587 return active_tree_->OuterViewportScrollLayer();
[email protected]8bef40572012-12-11 21:38:081588}
1589
[email protected]c1bb5af2013-03-13 19:06:271590LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const {
1591 return active_tree_->CurrentlyScrollingLayer();
[email protected]8bef40572012-12-11 21:38:081592}
1593
[email protected]251699b2013-10-09 00:21:261594bool LayerTreeHostImpl::IsCurrentlyScrolling() const {
1595 return CurrentlyScrollingLayer() ||
[email protected]adeda572014-01-31 00:49:471596 (InnerViewportScrollLayer() &&
1597 InnerViewportScrollLayer()->IsExternalFlingActive()) ||
1598 (OuterViewportScrollLayer() &&
1599 OuterViewportScrollLayer()->IsExternalFlingActive());
[email protected]251699b2013-10-09 00:21:261600}
1601
[email protected]94f206c12012-08-25 00:09:141602// Content layers can be either directly scrollable or contained in an outer
1603// scrolling layer which applies the scroll transform. Given a content layer,
1604// this function returns the associated scroll layer if any.
[email protected]c1bb5af2013-03-13 19:06:271605static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) {
1606 if (!layer_impl)
[email protected]bf1cfd9a2013-09-26 05:43:021607 return NULL;
[email protected]c1bb5af2013-03-13 19:06:271608
1609 if (layer_impl->scrollable())
1610 return layer_impl;
1611
1612 if (layer_impl->DrawsContent() &&
1613 layer_impl->parent() &&
1614 layer_impl->parent()->scrollable())
1615 return layer_impl->parent();
1616
[email protected]bf1cfd9a2013-09-26 05:43:021617 return NULL;
[email protected]94f206c12012-08-25 00:09:141618}
1619
[email protected]c1bb5af2013-03-13 19:06:271620void LayerTreeHostImpl::CreatePendingTree() {
1621 CHECK(!pending_tree_);
1622 if (recycle_tree_)
1623 recycle_tree_.swap(pending_tree_);
1624 else
1625 pending_tree_ = LayerTreeImpl::create(this);
1626 client_->OnCanDrawStateChanged(CanDraw());
[email protected]186468f2013-10-24 02:44:111627 TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]2e7ca422012-12-20 02:57:271628}
1629
[email protected]8bb6cfa2013-07-23 00:11:191630void LayerTreeHostImpl::UpdateVisibleTiles() {
[email protected]8bb6cfa2013-07-23 00:11:191631 if (tile_manager_ && tile_manager_->UpdateVisibleTiles())
1632 DidInitializeVisibleTile();
[email protected]8bb6cfa2013-07-23 00:11:191633 need_to_update_visible_tiles_before_draw_ = false;
[email protected]eabe5002013-01-12 22:07:481634}
1635
[email protected]c1bb5af2013-03-13 19:06:271636void LayerTreeHostImpl::ActivatePendingTree() {
1637 CHECK(pending_tree_);
[email protected]186468f2013-10-24 02:44:111638 TRACE_EVENT_ASYNC_END0("cc", "PendingTree:waiting", pending_tree_.get());
[email protected]1e0f8d62013-01-09 07:41:351639
[email protected]4f48f6e2013-08-27 06:33:381640 need_to_update_visible_tiles_before_draw_ = true;
1641
[email protected]1960a712013-04-30 17:06:471642 active_tree_->SetRootLayerScrollOffsetDelegate(NULL);
[email protected]c1bb5af2013-03-13 19:06:271643 active_tree_->PushPersistedState(pending_tree_.get());
1644 if (pending_tree_->needs_full_tree_sync()) {
1645 active_tree_->SetRootLayer(
[email protected]b5651c22013-03-14 15:06:331646 TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271647 active_tree_->DetachLayerTree(),
1648 active_tree_.get()));
1649 }
[email protected]b5651c22013-03-14 15:06:331650 TreeSynchronizer::PushProperties(pending_tree_->root_layer(),
[email protected]c1bb5af2013-03-13 19:06:271651 active_tree_->root_layer());
1652 DCHECK(!recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511653
[email protected]c9280762013-08-01 06:28:571654 // Process any requests in the UI resource queue. The request queue is given
1655 // in LayerTreeHost::FinishCommitOnImplThread. This must take place before
1656 // the swap.
1657 pending_tree_->ProcessUIResourceRequestQueue();
1658
[email protected]c1bb5af2013-03-13 19:06:271659 pending_tree_->PushPropertiesTo(active_tree_.get());
[email protected]48871fc2013-01-23 07:36:511660
[email protected]c1bb5af2013-03-13 19:06:271661 // Now that we've synced everything from the pending tree to the active
1662 // tree, rename the pending tree the recycle tree so we can reuse it on the
1663 // next sync.
1664 pending_tree_.swap(recycle_tree_);
[email protected]48871fc2013-01-23 07:36:511665
[email protected]7d1b07e2013-10-01 17:31:301666 active_tree_->DidBecomeActive();
[email protected]1960a712013-04-30 17:06:471667 active_tree_->SetRootLayerScrollOffsetDelegate(
1668 root_layer_scroll_offset_delegate_);
[email protected]37386f052013-01-13 00:42:221669
[email protected]c1bb5af2013-03-13 19:06:271670 client_->OnCanDrawStateChanged(CanDraw());
[email protected]4e4136d2013-11-29 02:22:441671 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:271672 client_->RenewTreePriority();
[email protected]652cf132013-02-15 21:53:241673
[email protected]a848c102013-03-26 08:59:091674 if (debug_state_.continuous_painting) {
1675 const RenderingStats& stats =
[email protected]372bad5f2013-03-21 16:38:431676 rendering_stats_instrumentation_->GetRenderingStats();
[email protected]922c6e1f2013-10-09 04:04:091677 paint_time_counter_->SavePaintTime(stats.main_stats.paint_time +
1678 stats.main_stats.record_time +
1679 stats.impl_stats.rasterize_time);
[email protected]c1bb5af2013-03-13 19:06:271680 }
[email protected]2a61ad52013-05-13 14:01:291681
[email protected]fef74fd2014-02-27 06:28:171682 UpdateInnerViewportContainerSize();
[email protected]2a61ad52013-05-13 14:01:291683 client_->DidActivatePendingTree();
[email protected]2022c672013-07-23 19:55:261684 if (!tree_activation_callback_.is_null())
1685 tree_activation_callback_.Run();
[email protected]d9fce6722013-08-30 01:10:011686
1687 if (time_source_client_adapter_ && time_source_client_adapter_->Active())
1688 DCHECK(active_tree_->root_layer());
[email protected]d73094482014-02-13 21:28:281689 devtools_instrumentation::DidActivateLayerTree(
1690 id_, active_tree_->source_frame_number());
[email protected]2e7ca422012-12-20 02:57:271691}
1692
[email protected]c1bb5af2013-03-13 19:06:271693void LayerTreeHostImpl::SetVisible(bool visible) {
1694 DCHECK(proxy_->IsImplThread());
[email protected]94f206c12012-08-25 00:09:141695
[email protected]c1bb5af2013-03-13 19:06:271696 if (visible_ == visible)
1697 return;
1698 visible_ = visible;
1699 DidVisibilityChange(this, visible_);
[email protected]206a3922013-05-17 06:34:121700 EnforceManagedMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]94f206c12012-08-25 00:09:141701
[email protected]3d609bb2014-02-01 01:10:231702 if (!visible_) {
1703 active_tree()->SetRequiresHighResToDraw();
[email protected]127bdc1a2013-09-11 01:44:481704 EvictAllUIResources();
[email protected]3d609bb2014-02-01 01:10:231705 }
[email protected]127bdc1a2013-09-11 01:44:481706
[email protected]0fa42e02013-08-20 01:16:051707 // Evict tiles immediately if invisible since this tab may never get another
1708 // draw or timer tick.
1709 if (!visible_)
1710 ManageTiles();
1711
[email protected]c1bb5af2013-03-13 19:06:271712 if (!renderer_)
1713 return;
[email protected]94f206c12012-08-25 00:09:141714
[email protected]c1bb5af2013-03-13 19:06:271715 renderer_->SetVisible(visible);
[email protected]94f206c12012-08-25 00:09:141716}
1717
[email protected]6be422b2013-12-08 06:47:311718void LayerTreeHostImpl::SetNeedsRedraw() {
1719 NotifySwapPromiseMonitorsOfSetNeedsRedraw();
1720 client_->SetNeedsRedrawOnImplThread();
1721}
1722
[email protected]206a3922013-05-17 06:34:121723ManagedMemoryPolicy LayerTreeHostImpl::ActualManagedMemoryPolicy() const {
[email protected]3f2ff112013-08-03 02:41:071724 ManagedMemoryPolicy actual = cached_managed_memory_policy_;
[email protected]ef444422014-01-17 10:11:011725 // TODO(ernstm): The second condition disables pre-painting for all layers
1726 // when GPU rasterization is enabled. Once we selectively enable GPU
1727 // rasterization per layer, we also need to disable pre-painting selectively:
1728 // crbug.com/335387
1729 if (debug_state_.rasterize_only_visible_content ||
[email protected]a23530d2014-03-11 06:04:141730 settings_.rasterization_site == LayerTreeSettings::GpuRasterization) {
[email protected]3f2ff112013-08-03 02:41:071731 actual.priority_cutoff_when_visible =
[email protected]f44d5552013-10-29 04:56:291732 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
[email protected]3f2ff112013-08-03 02:41:071733 }
[email protected]206a3922013-05-17 06:34:121734
[email protected]3f2ff112013-08-03 02:41:071735 if (zero_budget_) {
1736 actual.bytes_limit_when_visible = 0;
[email protected]3f2ff112013-08-03 02:41:071737 }
1738
[email protected]206a3922013-05-17 06:34:121739 return actual;
1740}
1741
[email protected]3f2ff112013-08-03 02:41:071742size_t LayerTreeHostImpl::memory_allocation_limit_bytes() const {
1743 return ActualManagedMemoryPolicy().bytes_limit_when_visible;
1744}
1745
[email protected]990e050a2013-09-23 18:50:211746int LayerTreeHostImpl::memory_allocation_priority_cutoff() const {
1747 return ManagedMemoryPolicy::PriorityCutoffToValue(
1748 ActualManagedMemoryPolicy().priority_cutoff_when_visible);
1749}
1750
[email protected]50644642013-06-20 13:58:551751void LayerTreeHostImpl::ReleaseTreeResources() {
1752 if (active_tree_->root_layer())
[email protected]50644642013-06-20 13:58:551753 SendReleaseResourcesRecursive(active_tree_->root_layer());
1754 if (pending_tree_ && pending_tree_->root_layer())
1755 SendReleaseResourcesRecursive(pending_tree_->root_layer());
1756 if (recycle_tree_ && recycle_tree_->root_layer())
1757 SendReleaseResourcesRecursive(recycle_tree_->root_layer());
[email protected]c9280762013-08-01 06:28:571758
[email protected]127bdc1a2013-09-11 01:44:481759 EvictAllUIResources();
[email protected]50644642013-06-20 13:58:551760}
1761
1762void LayerTreeHostImpl::CreateAndSetRenderer(
1763 OutputSurface* output_surface,
[email protected]50af3c22013-07-13 03:50:201764 ResourceProvider* resource_provider,
1765 bool skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551766 DCHECK(!renderer_);
1767 if (output_surface->capabilities().delegated_rendering) {
[email protected]573e8e32013-09-09 17:24:321768 renderer_ = DelegatingRenderer::Create(
1769 this, &settings_, output_surface, resource_provider);
[email protected]0634cdd42013-08-16 00:46:091770 } else if (output_surface->context_provider() && !skip_gl_renderer) {
[email protected]50644642013-06-20 13:58:551771 renderer_ = GLRenderer::Create(this,
[email protected]573e8e32013-09-09 17:24:321772 &settings_,
[email protected]50644642013-06-20 13:58:551773 output_surface,
1774 resource_provider,
[email protected]ea468c6c2013-09-10 08:25:111775 texture_mailbox_deleter_.get(),
[email protected]2eeea2b32013-12-06 21:29:241776 settings_.highp_threshold_min);
[email protected]50644642013-06-20 13:58:551777 } else if (output_surface->software_device()) {
[email protected]573e8e32013-09-09 17:24:321778 renderer_ = SoftwareRenderer::Create(
1779 this, &settings_, output_surface, resource_provider);
[email protected]50644642013-06-20 13:58:551780 }
1781
[email protected]84975832013-06-29 00:24:111782 if (renderer_) {
[email protected]50644642013-06-20 13:58:551783 renderer_->SetVisible(visible_);
[email protected]84975832013-06-29 00:24:111784 SetFullRootLayerDamage();
[email protected]b0a99ad22013-09-12 17:25:561785
1786 // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs to be
1787 // initialized to get max texture size. Also, after releasing resources,
1788 // trees need another update to generate new ones.
1789 active_tree_->set_needs_update_draw_properties();
1790 if (pending_tree_)
1791 pending_tree_->set_needs_update_draw_properties();
[email protected]fa339032014-02-18 22:11:591792 client_->UpdateRendererCapabilitiesOnImplThread();
[email protected]84975832013-06-29 00:24:111793 }
[email protected]50644642013-06-20 13:58:551794}
1795
[email protected]50af3c22013-07-13 03:50:201796void LayerTreeHostImpl::CreateAndSetTileManager(
1797 ResourceProvider* resource_provider,
[email protected]b6eb8e332013-09-10 00:51:011798 ContextProvider* context_provider,
[email protected]4e472a42014-02-11 01:56:371799 bool using_map_image,
1800 bool allow_rasterize_on_demand) {
[email protected]50af3c22013-07-13 03:50:201801 DCHECK(settings_.impl_side_painting);
1802 DCHECK(resource_provider);
[email protected]98805e62014-03-05 01:33:591803 DCHECK(proxy_->ImplThreadTaskRunner());
[email protected]ec7541d2014-04-09 01:23:531804
[email protected]ec7541d2014-04-09 01:23:531805 if (using_map_image) {
[email protected]7beac1b52014-04-10 23:09:561806 raster_worker_pool_ = ImageRasterWorkerPool::Create(
[email protected]ec7541d2014-04-09 01:23:531807 proxy_->ImplThreadTaskRunner(),
[email protected]7beac1b52014-04-10 23:09:561808 RasterWorkerPool::GetTaskGraphRunner(),
[email protected]ec7541d2014-04-09 01:23:531809 resource_provider,
1810 GetMapImageTextureTarget(context_provider));
[email protected]ec7541d2014-04-09 01:23:531811 } else {
[email protected]7beac1b52014-04-10 23:09:561812 raster_worker_pool_ = PixelBufferRasterWorkerPool::Create(
[email protected]ec7541d2014-04-09 01:23:531813 proxy_->ImplThreadTaskRunner(),
[email protected]7beac1b52014-04-10 23:09:561814 RasterWorkerPool::GetTaskGraphRunner(),
[email protected]ec7541d2014-04-09 01:23:531815 resource_provider,
1816 GetMaxTransferBufferUsageBytes(context_provider));
[email protected]ec7541d2014-04-09 01:23:531817 }
1818 direct_raster_worker_pool_ = DirectRasterWorkerPool::Create(
1819 proxy_->ImplThreadTaskRunner(), resource_provider, context_provider);
[email protected]b6eb8e332013-09-10 00:51:011820 tile_manager_ =
1821 TileManager::Create(this,
1822 resource_provider,
[email protected]7beac1b52014-04-10 23:09:561823 raster_worker_pool_->AsRasterizer(),
1824 direct_raster_worker_pool_->AsRasterizer(),
[email protected]8d7aa012013-11-23 21:19:121825 GetMaxRasterTasksUsageBytes(context_provider),
[email protected]ec7541d2014-04-09 01:23:531826 allow_rasterize_on_demand,
1827 rendering_stats_instrumentation_);
[email protected]b6eb8e332013-09-10 00:51:011828
[email protected]50af3c22013-07-13 03:50:201829 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]8bb6cfa2013-07-23 00:11:191830 need_to_update_visible_tiles_before_draw_ = false;
[email protected]50af3c22013-07-13 03:50:201831}
1832
[email protected]50644642013-06-20 13:58:551833void LayerTreeHostImpl::EnforceZeroBudget(bool zero_budget) {
[email protected]3f2ff112013-08-03 02:41:071834 SetManagedMemoryPolicy(cached_managed_memory_policy_, zero_budget);
[email protected]50644642013-06-20 13:58:551835}
1836
[email protected]c1bb5af2013-03-13 19:06:271837bool LayerTreeHostImpl::InitializeRenderer(
1838 scoped_ptr<OutputSurface> output_surface) {
[email protected]767f38d72014-03-18 21:26:411839#if DCHECK_IS_ON
[email protected]ce2e8112013-11-28 07:44:361840 DCHECK(!renderer_ || did_lose_called_);
1841#endif
1842
[email protected]c1bb5af2013-03-13 19:06:271843 // Since we will create a new resource provider, we cannot continue to use
1844 // the old resources (i.e. render_surfaces and texture IDs). Clear them
1845 // before we destroy the old resource provider.
[email protected]50644642013-06-20 13:58:551846 ReleaseTreeResources();
[email protected]45c4b1e2013-01-16 02:19:401847
[email protected]c1bb5af2013-03-13 19:06:271848 // Note: order is important here.
1849 renderer_.reset();
1850 tile_manager_.reset();
[email protected]7beac1b52014-04-10 23:09:561851 raster_worker_pool_.reset();
[email protected]ec7541d2014-04-09 01:23:531852 direct_raster_worker_pool_.reset();
[email protected]c1bb5af2013-03-13 19:06:271853 resource_provider_.reset();
1854 output_surface_.reset();
[email protected]94f206c12012-08-25 00:09:141855
[email protected]c1bb5af2013-03-13 19:06:271856 if (!output_surface->BindToClient(this))
1857 return false;
[email protected]be3181652012-09-25 13:02:131858
[email protected]a7f35682013-10-22 23:05:571859 scoped_ptr<ResourceProvider> resource_provider =
1860 ResourceProvider::Create(output_surface.get(),
1861 shared_bitmap_manager_,
1862 settings_.highp_threshold_min,
[email protected]b796c34f2013-11-01 11:52:341863 settings_.use_rgba_4444_textures,
1864 settings_.texture_id_allocation_chunk_size);
[email protected]50644642013-06-20 13:58:551865 if (!resource_provider)
1866 return false;
[email protected]2b154b22013-06-07 09:03:271867
[email protected]50644642013-06-20 13:58:551868 if (output_surface->capabilities().deferred_gl_initialization)
1869 EnforceZeroBudget(true);
[email protected]be3181652012-09-25 13:02:131870
[email protected]50af3c22013-07-13 03:50:201871 bool skip_gl_renderer = false;
1872 CreateAndSetRenderer(
1873 output_surface.get(), resource_provider.get(), skip_gl_renderer);
[email protected]be3181652012-09-25 13:02:131874
[email protected]50644642013-06-20 13:58:551875 if (!renderer_)
1876 return false;
[email protected]6123930c2013-05-23 06:48:371877
[email protected]50644642013-06-20 13:58:551878 if (settings_.impl_side_painting) {
[email protected]4e472a42014-02-11 01:56:371879 CreateAndSetTileManager(
1880 resource_provider.get(),
1881 output_surface->context_provider().get(),
1882 GetRendererCapabilities().using_map_image,
1883 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]fbe89f72013-05-21 07:24:241884 }
1885
[email protected]97e9ccd62014-04-12 01:07:151886 // Setup BeginFrameEmulation if it's not supported natively
[email protected]daea3d42013-10-23 17:04:501887 if (!settings_.begin_impl_frame_scheduling_enabled) {
[email protected]049fc7a2013-06-18 12:32:351888 const base::TimeDelta display_refresh_interval =
1889 base::TimeDelta::FromMicroseconds(
1890 base::Time::kMicrosecondsPerSecond /
1891 settings_.refresh_rate);
1892
[email protected]97e9ccd62014-04-12 01:07:151893 output_surface->InitializeBeginFrameEmulation(
[email protected]810d40b72013-06-20 18:26:151894 proxy_->ImplThreadTaskRunner(),
[email protected]049fc7a2013-06-18 12:32:351895 settings_.throttle_frame_production,
1896 display_refresh_interval);
1897 }
1898
1899 int max_frames_pending =
1900 output_surface->capabilities().max_frames_pending;
1901 if (max_frames_pending <= 0)
[email protected]df3c24c92013-06-19 03:54:351902 max_frames_pending = OutputSurface::DEFAULT_MAX_FRAMES_PENDING;
[email protected]049fc7a2013-06-18 12:32:351903 output_surface->SetMaxFramesPending(max_frames_pending);
1904
[email protected]50644642013-06-20 13:58:551905 resource_provider_ = resource_provider.Pass();
[email protected]c1bb5af2013-03-13 19:06:271906 output_surface_ = output_surface.Pass();
[email protected]94f206c12012-08-25 00:09:141907
[email protected]c1bb5af2013-03-13 19:06:271908 client_->OnCanDrawStateChanged(CanDraw());
[email protected]8db2213c2012-09-05 22:08:211909
[email protected]c1bb5af2013-03-13 19:06:271910 return true;
[email protected]94f206c12012-08-25 00:09:141911}
1912
[email protected]2b154b22013-06-07 09:03:271913bool LayerTreeHostImpl::DeferredInitialize(
1914 scoped_refptr<ContextProvider> offscreen_context_provider) {
1915 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
[email protected]50644642013-06-20 13:58:551916 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091917 DCHECK(output_surface_->context_provider());
[email protected]2b154b22013-06-07 09:03:271918
[email protected]50644642013-06-20 13:58:551919 ReleaseTreeResources();
1920 renderer_.reset();
[email protected]2b154b22013-06-07 09:03:271921
[email protected]d9ca99e2013-08-28 21:49:481922 bool resource_provider_success = resource_provider_->InitializeGL();
1923
1924 bool success = resource_provider_success;
1925 if (success) {
1926 bool skip_gl_renderer = false;
1927 CreateAndSetRenderer(
1928 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1929 if (!renderer_)
1930 success = false;
1931 }
1932
1933 if (success) {
1934 if (offscreen_context_provider.get() &&
1935 !offscreen_context_provider->BindToCurrentThread())
1936 success = false;
1937 }
1938
[email protected]50644642013-06-20 13:58:551939 if (success) {
1940 EnforceZeroBudget(false);
1941 client_->SetNeedsCommitOnImplThread();
[email protected]d9ca99e2013-08-28 21:49:481942 } else {
1943 if (offscreen_context_provider.get()) {
1944 if (offscreen_context_provider->BindToCurrentThread())
1945 offscreen_context_provider->VerifyContexts();
1946 offscreen_context_provider = NULL;
1947 }
1948
1949 client_->DidLoseOutputSurfaceOnImplThread();
1950
1951 if (resource_provider_success) {
1952 // If this fails the context provider will be dropped from the output
1953 // surface and destroyed. But the GLRenderer expects the output surface
1954 // to stick around - and hold onto the context3d - as long as it is alive.
1955 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1956 renderer_.reset();
1957
1958 // The resource provider can't stay in GL mode or it tries to clean up GL
1959 // stuff, but the context provider is going away on the output surface
1960 // which contradicts being in GL mode.
1961 // TODO(danakj): Remove the need for this code path: crbug.com/276411
1962 resource_provider_->InitializeSoftware();
1963 }
[email protected]50644642013-06-20 13:58:551964 }
[email protected]b5174d712013-08-28 08:10:431965
1966 SetOffscreenContextProvider(offscreen_context_provider);
[email protected]2b154b22013-06-07 09:03:271967 return success;
1968}
1969
[email protected]50af3c22013-07-13 03:50:201970void LayerTreeHostImpl::ReleaseGL() {
1971 DCHECK(output_surface_->capabilities().deferred_gl_initialization);
1972 DCHECK(settings_.impl_side_painting);
[email protected]0634cdd42013-08-16 00:46:091973 DCHECK(output_surface_->context_provider());
[email protected]50af3c22013-07-13 03:50:201974
1975 ReleaseTreeResources();
1976 renderer_.reset();
1977 tile_manager_.reset();
[email protected]7beac1b52014-04-10 23:09:561978 raster_worker_pool_.reset();
[email protected]ec7541d2014-04-09 01:23:531979 direct_raster_worker_pool_.reset();
[email protected]50af3c22013-07-13 03:50:201980 resource_provider_->InitializeSoftware();
1981
1982 bool skip_gl_renderer = true;
1983 CreateAndSetRenderer(
1984 output_surface_.get(), resource_provider_.get(), skip_gl_renderer);
1985 DCHECK(renderer_);
1986
1987 EnforceZeroBudget(true);
1988 CreateAndSetTileManager(resource_provider_.get(),
[email protected]b6eb8e332013-09-10 00:51:011989 NULL,
[email protected]4e472a42014-02-11 01:56:371990 GetRendererCapabilities().using_map_image,
1991 GetRendererCapabilities().allow_rasterize_on_demand);
[email protected]50af3c22013-07-13 03:50:201992 DCHECK(tile_manager_);
1993
[email protected]b5174d712013-08-28 08:10:431994 SetOffscreenContextProvider(NULL);
1995
[email protected]50af3c22013-07-13 03:50:201996 client_->SetNeedsCommitOnImplThread();
1997}
1998
[email protected]64348ea2014-01-29 22:58:261999void LayerTreeHostImpl::SetViewportSize(const gfx::Size& device_viewport_size) {
[email protected]18ce59702013-04-09 04:58:402000 if (device_viewport_size == device_viewport_size_)
[email protected]c1bb5af2013-03-13 19:06:272001 return;
[email protected]94f206c12012-08-25 00:09:142002
[email protected]54af03522013-09-05 00:43:282003 if (pending_tree_)
[email protected]c1bb5af2013-03-13 19:06:272004 active_tree_->SetViewportSizeInvalid();
[email protected]318822852013-02-14 00:54:272005
[email protected]c1bb5af2013-03-13 19:06:272006 device_viewport_size_ = device_viewport_size;
[email protected]94f206c12012-08-25 00:09:142007
[email protected]fef74fd2014-02-27 06:28:172008 UpdateInnerViewportContainerSize();
[email protected]c1bb5af2013-03-13 19:06:272009 client_->OnCanDrawStateChanged(CanDraw());
[email protected]59adb112013-04-09 04:48:442010 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142011}
2012
[email protected]d9083762013-03-24 01:36:402013void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) {
2014 if (overdraw_bottom_height == overdraw_bottom_height_)
2015 return;
2016 overdraw_bottom_height_ = overdraw_bottom_height;
2017
[email protected]fef74fd2014-02-27 06:28:172018 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:442019 SetFullRootLayerDamage();
[email protected]d9083762013-03-24 01:36:402020}
2021
[email protected]9f4f6a32013-09-04 21:35:122022void LayerTreeHostImpl::SetOverhangUIResource(
2023 UIResourceId overhang_ui_resource_id,
[email protected]64348ea2014-01-29 22:58:262024 const gfx::Size& overhang_ui_resource_size) {
[email protected]9f4f6a32013-09-04 21:35:122025 overhang_ui_resource_id_ = overhang_ui_resource_id;
2026 overhang_ui_resource_size_ = overhang_ui_resource_size;
2027}
2028
[email protected]c1bb5af2013-03-13 19:06:272029void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) {
2030 if (device_scale_factor == device_scale_factor_)
2031 return;
2032 device_scale_factor_ = device_scale_factor;
[email protected]c0dd24c2012-08-30 23:25:272033
[email protected]fef74fd2014-02-27 06:28:172034 UpdateInnerViewportContainerSize();
[email protected]59adb112013-04-09 04:48:442035 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142036}
2037
[email protected]54af03522013-09-05 00:43:282038gfx::Size LayerTreeHostImpl::DrawViewportSize() const {
2039 return DeviceViewport().size();
2040}
2041
[email protected]f224cc92013-06-06 23:23:322042gfx::Rect LayerTreeHostImpl::DeviceViewport() const {
2043 if (external_viewport_.IsEmpty())
2044 return gfx::Rect(device_viewport_size_);
2045
2046 return external_viewport_;
2047}
2048
[email protected]54af03522013-09-05 00:43:282049gfx::Rect LayerTreeHostImpl::DeviceClip() const {
2050 if (external_clip_.IsEmpty())
2051 return DeviceViewport();
2052
2053 return external_clip_;
2054}
2055
2056const gfx::Transform& LayerTreeHostImpl::DrawTransform() const {
[email protected]f224cc92013-06-06 23:23:322057 return external_transform_;
2058}
2059
[email protected]59adb112013-04-09 04:48:442060void LayerTreeHostImpl::DidChangeTopControlsPosition() {
[email protected]fef74fd2014-02-27 06:28:172061 UpdateInnerViewportContainerSize();
[email protected]4e4136d2013-11-29 02:22:442062 SetNeedsRedraw();
[email protected]59adb112013-04-09 04:48:442063 active_tree_->set_needs_update_draw_properties();
2064 SetFullRootLayerDamage();
[email protected]94f206c12012-08-25 00:09:142065}
2066
[email protected]c1bb5af2013-03-13 19:06:272067bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() {
[email protected]7d19dc342013-05-02 22:02:042068 active_tree_->UpdateDrawProperties();
[email protected]3209161d2013-03-29 19:17:342069 return !active_tree_->RenderSurfaceLayerList().empty();
[email protected]94f206c12012-08-25 00:09:142070}
2071
[email protected]200a9c062013-05-20 04:34:372072void LayerTreeHostImpl::BindToClient(InputHandlerClient* client) {
2073 DCHECK(input_handler_client_ == NULL);
2074 input_handler_client_ = client;
2075}
2076
[email protected]420fdf6e2013-08-26 20:36:382077static LayerImpl* NextScrollLayer(LayerImpl* layer) {
2078 if (LayerImpl* scroll_parent = layer->scroll_parent())
2079 return scroll_parent;
2080 return layer->parent();
2081}
2082
[email protected]f620b0e72013-10-01 21:38:242083LayerImpl* LayerTreeHostImpl::FindScrollLayerForDeviceViewportPoint(
[email protected]14bc5d682014-01-17 07:26:472084 const gfx::PointF& device_viewport_point,
2085 InputHandler::ScrollInputType type,
[email protected]f161ca9e2014-04-01 13:57:012086 LayerImpl* layer_impl,
2087 bool* scroll_on_main_thread,
2088 bool* has_ancestor_scroll_handler) const {
[email protected]bf1cfd9a2013-09-26 05:43:022089 DCHECK(scroll_on_main_thread);
[email protected]94f206c12012-08-25 00:09:142090
[email protected]c1bb5af2013-03-13 19:06:272091 // Walk up the hierarchy and look for a scrollable layer.
2092 LayerImpl* potentially_scrolling_layer_impl = 0;
[email protected]420fdf6e2013-08-26 20:36:382093 for (; layer_impl; layer_impl = NextScrollLayer(layer_impl)) {
[email protected]c1bb5af2013-03-13 19:06:272094 // The content layer can also block attempts to scroll outside the main
2095 // thread.
2096 ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type);
2097 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022098 *scroll_on_main_thread = true;
2099 return NULL;
[email protected]94f206c12012-08-25 00:09:142100 }
2101
[email protected]c1bb5af2013-03-13 19:06:272102 LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl);
2103 if (!scroll_layer_impl)
2104 continue;
[email protected]94f206c12012-08-25 00:09:142105
[email protected]c1bb5af2013-03-13 19:06:272106 status = scroll_layer_impl->TryScroll(device_viewport_point, type);
[email protected]c1bb5af2013-03-13 19:06:272107 // If any layer wants to divert the scroll event to the main thread, abort.
2108 if (status == ScrollOnMainThread) {
[email protected]bf1cfd9a2013-09-26 05:43:022109 *scroll_on_main_thread = true;
2110 return NULL;
[email protected]94f206c12012-08-25 00:09:142111 }
2112
[email protected]f161ca9e2014-04-01 13:57:012113 if (has_ancestor_scroll_handler &&
2114 scroll_layer_impl->have_scroll_event_handlers())
2115 *has_ancestor_scroll_handler = true;
2116
[email protected]c1bb5af2013-03-13 19:06:272117 if (status == ScrollStarted && !potentially_scrolling_layer_impl)
2118 potentially_scrolling_layer_impl = scroll_layer_impl;
2119 }
2120
[email protected]bf1cfd9a2013-09-26 05:43:022121 return potentially_scrolling_layer_impl;
2122}
2123
2124InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin(
[email protected]47a723f2014-03-05 12:42:492125 const gfx::Point& viewport_point,
2126 InputHandler::ScrollInputType type) {
[email protected]bf1cfd9a2013-09-26 05:43:022127 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin");
2128
2129 if (top_controls_manager_)
2130 top_controls_manager_->ScrollBegin();
2131
2132 DCHECK(!CurrentlyScrollingLayer());
2133 ClearCurrentlyScrollingLayer();
2134
2135 if (!EnsureRenderSurfaceLayerList())
2136 return ScrollIgnored;
2137
[email protected]f620b0e72013-10-01 21:38:242138 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2139 device_scale_factor_);
2140 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2141 device_viewport_point,
2142 active_tree_->RenderSurfaceLayerList());
[email protected]bf1cfd9a2013-09-26 05:43:022143 bool scroll_on_main_thread = false;
[email protected]f620b0e72013-10-01 21:38:242144 LayerImpl* potentially_scrolling_layer_impl =
[email protected]f161ca9e2014-04-01 13:57:012145 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2146 type,
2147 layer_impl,
2148 &scroll_on_main_thread,
2149 &scroll_affects_scroll_handler_);
[email protected]bf1cfd9a2013-09-26 05:43:022150
2151 if (scroll_on_main_thread) {
[email protected]bf1cfd9a2013-09-26 05:43:022152 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true);
2153 return ScrollOnMainThread;
2154 }
2155
[email protected]adeda572014-01-31 00:49:472156 // If we want to send a DidOverscroll for this scroll it can't be ignored.
[email protected]d948c112014-01-10 02:13:532157 if (!potentially_scrolling_layer_impl)
[email protected]adeda572014-01-31 00:49:472158 potentially_scrolling_layer_impl = OuterViewportScrollLayer()
2159 ? OuterViewportScrollLayer()
2160 : InnerViewportScrollLayer();
[email protected]34d43432013-09-26 20:04:102161
[email protected]c1bb5af2013-03-13 19:06:272162 if (potentially_scrolling_layer_impl) {
[email protected]0fc818e2013-03-18 06:45:202163 active_tree_->SetCurrentlyScrollingLayer(
[email protected]c1bb5af2013-03-13 19:06:272164 potentially_scrolling_layer_impl);
2165 should_bubble_scrolls_ = (type != NonBubblingGesture);
2166 wheel_scrolling_ = (type == Wheel);
[email protected]c1bb5af2013-03-13 19:06:272167 client_->RenewTreePriority();
2168 UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false);
2169 return ScrollStarted;
2170 }
2171 return ScrollIgnored;
[email protected]94f206c12012-08-25 00:09:142172}
2173
[email protected]c1bb5af2013-03-13 19:06:272174gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta(
2175 LayerImpl* layer_impl,
2176 float scale_from_viewport_to_screen_space,
[email protected]14bc5d682014-01-17 07:26:472177 const gfx::PointF& viewport_point,
[email protected]3244c9132014-01-23 10:39:122178 const gfx::Vector2dF& viewport_delta) {
[email protected]c1bb5af2013-03-13 19:06:272179 // Layers with non-invertible screen space transforms should not have passed
2180 // the scroll hit test in the first place.
2181 DCHECK(layer_impl->screen_space_transform().IsInvertible());
2182 gfx::Transform inverse_screen_space_transform(
2183 gfx::Transform::kSkipInitialization);
2184 bool did_invert = layer_impl->screen_space_transform().GetInverse(
2185 &inverse_screen_space_transform);
[email protected]ca2902e92013-03-28 01:45:352186 // TODO(shawnsingh): With the advent of impl-side crolling for non-root
2187 // layers, we may need to explicitly handle uninvertible transforms here.
[email protected]c1bb5af2013-03-13 19:06:272188 DCHECK(did_invert);
[email protected]94f206c12012-08-25 00:09:142189
[email protected]c1bb5af2013-03-13 19:06:272190 gfx::PointF screen_space_point =
2191 gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space);
[email protected]94f206c12012-08-25 00:09:142192
[email protected]c1bb5af2013-03-13 19:06:272193 gfx::Vector2dF screen_space_delta = viewport_delta;
2194 screen_space_delta.Scale(scale_from_viewport_to_screen_space);
2195
2196 // First project the scroll start and end points to local layer space to find
2197 // the scroll delta in layer coordinates.
2198 bool start_clipped, end_clipped;
2199 gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta;
2200 gfx::PointF local_start_point =
[email protected]fa816c62013-03-18 04:24:212201 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272202 screen_space_point,
[email protected]fa816c62013-03-18 04:24:212203 &start_clipped);
[email protected]c1bb5af2013-03-13 19:06:272204 gfx::PointF local_end_point =
[email protected]fa816c62013-03-18 04:24:212205 MathUtil::ProjectPoint(inverse_screen_space_transform,
[email protected]c1bb5af2013-03-13 19:06:272206 screen_space_end_point,
[email protected]fa816c62013-03-18 04:24:212207 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272208
2209 // In general scroll point coordinates should not get clipped.
2210 DCHECK(!start_clipped);
2211 DCHECK(!end_clipped);
2212 if (start_clipped || end_clipped)
2213 return gfx::Vector2dF();
2214
2215 // local_start_point and local_end_point are in content space but we want to
2216 // move them to layer space for scrolling.
2217 float width_scale = 1.f / layer_impl->contents_scale_x();
2218 float height_scale = 1.f / layer_impl->contents_scale_y();
2219 local_start_point.Scale(width_scale, height_scale);
2220 local_end_point.Scale(width_scale, height_scale);
2221
2222 // Apply the scroll delta.
[email protected]1960a712013-04-30 17:06:472223 gfx::Vector2dF previous_delta = layer_impl->ScrollDelta();
[email protected]c1bb5af2013-03-13 19:06:272224 layer_impl->ScrollBy(local_end_point - local_start_point);
2225
2226 // Get the end point in the layer's content space so we can apply its
2227 // ScreenSpaceTransform.
2228 gfx::PointF actual_local_end_point = local_start_point +
[email protected]1960a712013-04-30 17:06:472229 layer_impl->ScrollDelta() -
[email protected]c1bb5af2013-03-13 19:06:272230 previous_delta;
2231 gfx::PointF actual_local_content_end_point =
2232 gfx::ScalePoint(actual_local_end_point,
2233 1.f / width_scale,
2234 1.f / height_scale);
2235
2236 // Calculate the applied scroll delta in viewport space coordinates.
2237 gfx::PointF actual_screen_space_end_point =
[email protected]fa816c62013-03-18 04:24:212238 MathUtil::MapPoint(layer_impl->screen_space_transform(),
[email protected]c1bb5af2013-03-13 19:06:272239 actual_local_content_end_point,
[email protected]fa816c62013-03-18 04:24:212240 &end_clipped);
[email protected]c1bb5af2013-03-13 19:06:272241 DCHECK(!end_clipped);
2242 if (end_clipped)
2243 return gfx::Vector2dF();
2244 gfx::PointF actual_viewport_end_point =
2245 gfx::ScalePoint(actual_screen_space_end_point,
2246 1.f / scale_from_viewport_to_screen_space);
2247 return actual_viewport_end_point - viewport_point;
[email protected]94f206c12012-08-25 00:09:142248}
2249
[email protected]c1bb5af2013-03-13 19:06:272250static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl,
[email protected]3244c9132014-01-23 10:39:122251 const gfx::Vector2dF& local_delta) {
[email protected]1960a712013-04-30 17:06:472252 gfx::Vector2dF previous_delta(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272253 layer_impl->ScrollBy(local_delta);
[email protected]1960a712013-04-30 17:06:472254 return layer_impl->ScrollDelta() - previous_delta;
[email protected]c1bb5af2013-03-13 19:06:272255}
2256
[email protected]47a723f2014-03-05 12:42:492257bool LayerTreeHostImpl::ScrollBy(const gfx::Point& viewport_point,
[email protected]3244c9132014-01-23 10:39:122258 const gfx::Vector2dF& scroll_delta) {
[email protected]c1bb5af2013-03-13 19:06:272259 TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy");
2260 if (!CurrentlyScrollingLayer())
2261 return false;
2262
2263 gfx::Vector2dF pending_delta = scroll_delta;
[email protected]a2b5ded2013-05-20 21:32:532264 gfx::Vector2dF unused_root_delta;
[email protected]2bd503f2013-07-23 05:35:292265 bool did_scroll_x = false;
2266 bool did_scroll_y = false;
[email protected]f42cffb2014-03-08 18:03:252267 bool did_scroll_top_controls = false;
[email protected]adeda572014-01-31 00:49:472268 // TODO(wjmaclean) Should we guard against CurrentlyScrollingLayer() == 0
2269 // here?
2270 bool consume_by_top_controls =
2271 top_controls_manager_ &&
2272 (((CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2273 CurrentlyScrollingLayer() == OuterViewportScrollLayer()) &&
2274 InnerViewportScrollLayer()->MaxScrollOffset().y() > 0) ||
2275 scroll_delta.y() < 0);
[email protected]a91e4f82013-03-15 06:58:062276
[email protected]c1bb5af2013-03-13 19:06:272277 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2278 layer_impl;
2279 layer_impl = layer_impl->parent()) {
2280 if (!layer_impl->scrollable())
2281 continue;
2282
[email protected]adeda572014-01-31 00:49:472283 if (layer_impl == InnerViewportScrollLayer()) {
[email protected]a2b5ded2013-05-20 21:32:532284 // Only allow bubble scrolling when the scroll is in the direction to make
2285 // the top controls visible.
[email protected]adeda572014-01-31 00:49:472286 gfx::Vector2dF applied_delta;
2287 gfx::Vector2dF excess_delta;
[email protected]d948c112014-01-10 02:13:532288 if (consume_by_top_controls) {
[email protected]adeda572014-01-31 00:49:472289 excess_delta = top_controls_manager_->ScrollBy(pending_delta);
2290 applied_delta = pending_delta - excess_delta;
2291 pending_delta = excess_delta;
2292 // Force updating of vertical adjust values if needed.
[email protected]f42cffb2014-03-08 18:03:252293 if (applied_delta.y() != 0) {
2294 did_scroll_top_controls = true;
[email protected]adeda572014-01-31 00:49:472295 layer_impl->ScrollbarParametersDidChange();
[email protected]f42cffb2014-03-08 18:03:252296 }
[email protected]a2b5ded2013-05-20 21:32:532297 }
2298 // Track root layer deltas for reporting overscroll.
2299 unused_root_delta = pending_delta;
[email protected]60b4d252013-03-23 18:49:422300 }
2301
[email protected]c1bb5af2013-03-13 19:06:272302 gfx::Vector2dF applied_delta;
[email protected]c1bb5af2013-03-13 19:06:272303 // Gesture events need to be transformed from viewport coordinates to local
2304 // layer coordinates so that the scrolling contents exactly follow the
2305 // user's finger. In contrast, wheel events represent a fixed amount of
2306 // scrolling so we can just apply them directly.
2307 if (!wheel_scrolling_) {
2308 float scale_from_viewport_to_screen_space = device_scale_factor_;
2309 applied_delta =
2310 ScrollLayerWithViewportSpaceDelta(layer_impl,
2311 scale_from_viewport_to_screen_space,
2312 viewport_point, pending_delta);
2313 } else {
2314 applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta);
[email protected]94f206c12012-08-25 00:09:142315 }
[email protected]94f206c12012-08-25 00:09:142316
[email protected]9328ede2014-04-04 07:45:522317 const float kEpsilon = 0.1f;
[email protected]5b8a865c2014-04-03 00:35:512318 if (layer_impl == InnerViewportScrollLayer()) {
2319 unused_root_delta.Subtract(applied_delta);
[email protected]9328ede2014-04-04 07:45:522320 if (std::abs(unused_root_delta.x()) < kEpsilon)
[email protected]5b8a865c2014-04-03 00:35:512321 unused_root_delta.set_x(0.0f);
[email protected]9328ede2014-04-04 07:45:522322 if (std::abs(unused_root_delta.y()) < kEpsilon)
[email protected]5b8a865c2014-04-03 00:35:512323 unused_root_delta.set_y(0.0f);
[email protected]e9eb23f2014-04-11 13:42:032324 // Disable overscroll on axes which is impossible to scroll.
2325 if (settings_.report_overscroll_only_for_scrollable_axes) {
2326 if (std::abs(active_tree_->TotalMaxScrollOffset().x()) <= kEpsilon)
2327 unused_root_delta.set_x(0.0f);
2328 if (std::abs(active_tree_->TotalMaxScrollOffset().y()) <= kEpsilon)
2329 unused_root_delta.set_y(0.0f);
2330 }
[email protected]5b8a865c2014-04-03 00:35:512331 }
2332
[email protected]c1bb5af2013-03-13 19:06:272333 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]9328ede2014-04-04 07:45:522334 bool did_move_layer_x = std::abs(applied_delta.x()) > kEpsilon;
2335 bool did_move_layer_y = std::abs(applied_delta.y()) > kEpsilon;
[email protected]2bd503f2013-07-23 05:35:292336 did_scroll_x |= did_move_layer_x;
2337 did_scroll_y |= did_move_layer_y;
2338 if (!did_move_layer_x && !did_move_layer_y) {
[email protected]09a6f2c2014-02-20 20:19:142339 // Scrolls should always bubble between the outer and inner viewports
2340 if (should_bubble_scrolls_ || !did_lock_scrolling_layer_ ||
2341 layer_impl == OuterViewportScrollLayer())
[email protected]c1bb5af2013-03-13 19:06:272342 continue;
[email protected]db102612013-12-20 22:04:122343 else
2344 break;
[email protected]94f206c12012-08-25 00:09:142345 }
[email protected]a2b5ded2013-05-20 21:32:532346
[email protected]c1bb5af2013-03-13 19:06:272347 did_lock_scrolling_layer_ = true;
2348 if (!should_bubble_scrolls_) {
[email protected]0fc818e2013-03-18 06:45:202349 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
[email protected]c1bb5af2013-03-13 19:06:272350 break;
[email protected]94f206c12012-08-25 00:09:142351 }
[email protected]94f206c12012-08-25 00:09:142352
[email protected]c1bb5af2013-03-13 19:06:272353 // If the applied delta is within 45 degrees of the input delta, bail out to
2354 // make it easier to scroll just one layer in one direction without
2355 // affecting any of its parents.
2356 float angle_threshold = 45;
[email protected]fa816c62013-03-18 04:24:212357 if (MathUtil::SmallestAngleBetweenVectors(
[email protected]c1bb5af2013-03-13 19:06:272358 applied_delta, pending_delta) < angle_threshold) {
2359 pending_delta = gfx::Vector2d();
2360 break;
[email protected]4a23c374c2012-12-08 08:38:552361 }
[email protected]c1bb5af2013-03-13 19:06:272362
2363 // Allow further movement only on an axis perpendicular to the direction in
2364 // which the layer moved.
2365 gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x());
[email protected]fa816c62013-03-18 04:24:212366 pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis);
[email protected]c1bb5af2013-03-13 19:06:272367
[email protected]a2b5ded2013-05-20 21:32:532368 if (gfx::ToRoundedVector2d(pending_delta).IsZero())
[email protected]c1bb5af2013-03-13 19:06:272369 break;
2370 }
2371
[email protected]f42cffb2014-03-08 18:03:252372 bool did_scroll_content = did_scroll_x || did_scroll_y;
2373 if (did_scroll_content) {
[email protected]c1bb5af2013-03-13 19:06:272374 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442375 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272376 client_->RenewTreePriority();
2377 }
[email protected]a2b5ded2013-05-20 21:32:532378
[email protected]2bd503f2013-07-23 05:35:292379 // Scrolling along an axis resets accumulated root overscroll for that axis.
2380 if (did_scroll_x)
2381 accumulated_root_overscroll_.set_x(0);
2382 if (did_scroll_y)
2383 accumulated_root_overscroll_.set_y(0);
2384
[email protected]a2b5ded2013-05-20 21:32:532385 accumulated_root_overscroll_ += unused_root_delta;
[email protected]485a42dc2014-03-26 22:44:342386 bool did_overscroll = !unused_root_delta.IsZero();
[email protected]a2b5ded2013-05-20 21:32:532387 if (did_overscroll && input_handler_client_) {
[email protected]635b0e92014-04-02 21:48:012388 input_handler_client_->DidOverscroll(accumulated_root_overscroll_,
2389 unused_root_delta);
[email protected]a2b5ded2013-05-20 21:32:532390 }
2391
[email protected]f42cffb2014-03-08 18:03:252392 return did_scroll_content || did_scroll_top_controls;
[email protected]4a23c374c2012-12-08 08:38:552393}
2394
[email protected]be782f52013-03-23 21:36:142395// This implements scrolling by page as described here:
2396// http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel
2397// for events with WHEEL_PAGESCROLL set.
[email protected]47a723f2014-03-05 12:42:492398bool LayerTreeHostImpl::ScrollVerticallyByPage(const gfx::Point& viewport_point,
[email protected]c28df4c12013-05-22 17:36:492399 ScrollDirection direction) {
[email protected]be782f52013-03-23 21:36:142400 DCHECK(wheel_scrolling_);
2401
2402 for (LayerImpl* layer_impl = CurrentlyScrollingLayer();
2403 layer_impl;
2404 layer_impl = layer_impl->parent()) {
2405 if (!layer_impl->scrollable())
2406 continue;
2407
[email protected]adeda572014-01-31 00:49:472408 if (!layer_impl->HasScrollbar(VERTICAL))
[email protected]be782f52013-03-23 21:36:142409 continue;
2410
[email protected]adeda572014-01-31 00:49:472411 float height = layer_impl->clip_height();
[email protected]be782f52013-03-23 21:36:142412
2413 // These magical values match WebKit and are designed to scroll nearly the
2414 // entire visible content height but leave a bit of overlap.
2415 float page = std::max(height * 0.875f, 1.f);
[email protected]c28df4c12013-05-22 17:36:492416 if (direction == SCROLL_BACKWARD)
[email protected]be782f52013-03-23 21:36:142417 page = -page;
2418
2419 gfx::Vector2dF delta = gfx::Vector2dF(0.f, page);
2420
2421 gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta);
2422
2423 if (!applied_delta.IsZero()) {
[email protected]be782f52013-03-23 21:36:142424 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442425 SetNeedsRedraw();
[email protected]be782f52013-03-23 21:36:142426 client_->RenewTreePriority();
2427 return true;
2428 }
2429
2430 active_tree_->SetCurrentlyScrollingLayer(layer_impl);
2431 }
2432
2433 return false;
2434}
2435
[email protected]1960a712013-04-30 17:06:472436void LayerTreeHostImpl::SetRootLayerScrollOffsetDelegate(
2437 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
2438 root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
2439 active_tree_->SetRootLayerScrollOffsetDelegate(
2440 root_layer_scroll_offset_delegate_);
2441}
2442
2443void LayerTreeHostImpl::OnRootLayerDelegatedScrollOffsetChanged() {
2444 DCHECK(root_layer_scroll_offset_delegate_ != NULL);
2445 client_->SetNeedsCommitOnImplThread();
2446}
2447
[email protected]c1bb5af2013-03-13 19:06:272448void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() {
2449 active_tree_->ClearCurrentlyScrollingLayer();
2450 did_lock_scrolling_layer_ = false;
[email protected]f161ca9e2014-04-01 13:57:012451 scroll_affects_scroll_handler_ = false;
[email protected]a2b5ded2013-05-20 21:32:532452 accumulated_root_overscroll_ = gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:142453}
2454
[email protected]c1bb5af2013-03-13 19:06:272455void LayerTreeHostImpl::ScrollEnd() {
2456 if (top_controls_manager_)
2457 top_controls_manager_->ScrollEnd();
2458 ClearCurrentlyScrollingLayer();
[email protected]21c9dee72013-06-15 01:20:052459 StartScrollbarAnimation();
[email protected]94f206c12012-08-25 00:09:142460}
2461
[email protected]5ff3c9782013-04-29 17:35:122462InputHandler::ScrollStatus LayerTreeHostImpl::FlingScrollBegin() {
[email protected]75147f52013-08-02 19:11:462463 if (!active_tree_->CurrentlyScrollingLayer())
2464 return ScrollIgnored;
[email protected]7c45d8152013-04-23 18:27:212465
[email protected]75147f52013-08-02 19:11:462466 if (settings_.ignore_root_layer_flings &&
[email protected]adeda572014-01-31 00:49:472467 (active_tree_->CurrentlyScrollingLayer() == InnerViewportScrollLayer() ||
2468 active_tree_->CurrentlyScrollingLayer() == OuterViewportScrollLayer())) {
[email protected]75147f52013-08-02 19:11:462469 ClearCurrentlyScrollingLayer();
2470 return ScrollIgnored;
2471 }
2472
[email protected]db102612013-12-20 22:04:122473 if (!wheel_scrolling_) {
2474 // Allow the fling to lock to the first layer that moves after the initial
2475 // fling |ScrollBy()| event.
2476 did_lock_scrolling_layer_ = false;
2477 should_bubble_scrolls_ = false;
2478 }
[email protected]df0c42342013-10-08 20:52:122479
[email protected]75147f52013-08-02 19:11:462480 return ScrollStarted;
[email protected]7c45d8152013-04-23 18:27:212481}
2482
[email protected]bf1cfd9a2013-09-26 05:43:022483float LayerTreeHostImpl::DeviceSpaceDistanceToLayer(
[email protected]14bc5d682014-01-17 07:26:472484 const gfx::PointF& device_viewport_point,
[email protected]bf1cfd9a2013-09-26 05:43:022485 LayerImpl* layer_impl) {
2486 if (!layer_impl)
2487 return std::numeric_limits<float>::max();
2488
2489 gfx::Rect layer_impl_bounds(
2490 layer_impl->content_bounds());
2491
2492 gfx::RectF device_viewport_layer_impl_bounds = MathUtil::MapClippedRect(
2493 layer_impl->screen_space_transform(),
2494 layer_impl_bounds);
2495
2496 return device_viewport_layer_impl_bounds.ManhattanDistanceToPoint(
2497 device_viewport_point);
2498}
2499
[email protected]47a723f2014-03-05 12:42:492500void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) {
[email protected]bf1cfd9a2013-09-26 05:43:022501 if (!EnsureRenderSurfaceLayerList())
2502 return;
2503
[email protected]f620b0e72013-10-01 21:38:242504 gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point,
2505 device_scale_factor_);
2506
2507 LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
2508 device_viewport_point,
2509 active_tree_->RenderSurfaceLayerList());
2510 if (HandleMouseOverScrollbar(layer_impl, device_viewport_point))
2511 return;
2512
2513 if (scroll_layer_id_when_mouse_over_scrollbar_) {
2514 LayerImpl* scroll_layer_impl = active_tree_->LayerById(
2515 scroll_layer_id_when_mouse_over_scrollbar_);
2516
[email protected]5cd9ac12014-02-05 21:48:532517 // The check for a null scroll_layer_impl below was added to see if it will
2518 // eliminate the crashes described in http://crbug.com/326635.
2519 // TODO(wjmaclean) Add a unit test if this fixes the crashes.
[email protected]f620b0e72013-10-01 21:38:242520 ScrollbarAnimationController* animation_controller =
[email protected]5cd9ac12014-02-05 21:48:532521 scroll_layer_impl ? scroll_layer_impl->scrollbar_animation_controller()
2522 : NULL;
[email protected]f620b0e72013-10-01 21:38:242523 if (animation_controller) {
[email protected]1dc06162014-03-26 22:54:452524 animation_controller->DidMouseMoveOffScrollbar(CurrentFrameTimeTicks());
[email protected]f620b0e72013-10-01 21:38:242525 StartScrollbarAnimation();
2526 }
2527 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2528 }
2529
[email protected]bf1cfd9a2013-09-26 05:43:022530 bool scroll_on_main_thread = false;
[email protected]f161ca9e2014-04-01 13:57:012531 LayerImpl* scroll_layer_impl =
2532 FindScrollLayerForDeviceViewportPoint(device_viewport_point,
2533 InputHandler::Gesture,
2534 layer_impl,
2535 &scroll_on_main_thread,
2536 NULL);
[email protected]bf1cfd9a2013-09-26 05:43:022537 if (scroll_on_main_thread || !scroll_layer_impl)
2538 return;
2539
2540 ScrollbarAnimationController* animation_controller =
2541 scroll_layer_impl->scrollbar_animation_controller();
2542 if (!animation_controller)
2543 return;
2544
[email protected]adeda572014-01-31 00:49:472545 // TODO(wjmaclean) Is it ok to choose distance from more than two scrollbars?
2546 float distance_to_scrollbar = std::numeric_limits<float>::max();
2547 for (LayerImpl::ScrollbarSet::iterator it =
2548 scroll_layer_impl->scrollbars()->begin();
2549 it != scroll_layer_impl->scrollbars()->end();
2550 ++it)
2551 distance_to_scrollbar =
2552 std::min(distance_to_scrollbar,
2553 DeviceSpaceDistanceToLayer(device_viewport_point, *it));
[email protected]bf1cfd9a2013-09-26 05:43:022554
2555 bool should_animate = animation_controller->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452556 CurrentFrameTimeTicks(), distance_to_scrollbar / device_scale_factor_);
[email protected]534236b2013-10-25 21:19:202557 if (should_animate)
[email protected]bf1cfd9a2013-09-26 05:43:022558 StartScrollbarAnimation();
[email protected]bf1cfd9a2013-09-26 05:43:022559}
2560
[email protected]f620b0e72013-10-01 21:38:242561bool LayerTreeHostImpl::HandleMouseOverScrollbar(LayerImpl* layer_impl,
[email protected]14bc5d682014-01-17 07:26:472562 const gfx::PointF& device_viewport_point) {
[email protected]f620b0e72013-10-01 21:38:242563 if (layer_impl && layer_impl->ToScrollbarLayer()) {
2564 int scroll_layer_id = layer_impl->ToScrollbarLayer()->ScrollLayerId();
2565 layer_impl = active_tree_->LayerById(scroll_layer_id);
2566 if (layer_impl && layer_impl->scrollbar_animation_controller()) {
2567 scroll_layer_id_when_mouse_over_scrollbar_ = scroll_layer_id;
[email protected]534236b2013-10-25 21:19:202568 bool should_animate =
2569 layer_impl->scrollbar_animation_controller()->DidMouseMoveNear(
[email protected]1dc06162014-03-26 22:54:452570 CurrentFrameTimeTicks(), 0);
[email protected]534236b2013-10-25 21:19:202571 if (should_animate)
2572 StartScrollbarAnimation();
[email protected]f620b0e72013-10-01 21:38:242573 } else {
2574 scroll_layer_id_when_mouse_over_scrollbar_ = 0;
2575 }
2576
2577 return true;
2578 }
2579
2580 return false;
2581}
2582
[email protected]c1bb5af2013-03-13 19:06:272583void LayerTreeHostImpl::PinchGestureBegin() {
2584 pinch_gesture_active_ = true;
2585 previous_pinch_anchor_ = gfx::Point();
2586 client_->RenewTreePriority();
[email protected]2fa342b82013-09-24 03:19:132587 pinch_gesture_end_should_clear_scrolling_layer_ = !CurrentlyScrollingLayer();
[email protected]adeda572014-01-31 00:49:472588 if (active_tree_->OuterViewportScrollLayer()) {
2589 active_tree_->SetCurrentlyScrollingLayer(
2590 active_tree_->OuterViewportScrollLayer());
2591 } else {
2592 active_tree_->SetCurrentlyScrollingLayer(
2593 active_tree_->InnerViewportScrollLayer());
2594 }
[email protected]a9bda5e2013-10-25 18:43:372595 if (top_controls_manager_)
2596 top_controls_manager_->PinchBegin();
[email protected]94f206c12012-08-25 00:09:142597}
2598
[email protected]c1bb5af2013-03-13 19:06:272599void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta,
[email protected]47a723f2014-03-05 12:42:492600 const gfx::Point& anchor) {
[email protected]b93480862014-02-20 13:07:032601 if (!InnerViewportScrollLayer())
2602 return;
[email protected]d3afa112012-12-08 06:24:282603
[email protected]b93480862014-02-20 13:07:032604 TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate");
[email protected]d3afa112012-12-08 06:24:282605
[email protected]c1bb5af2013-03-13 19:06:272606 // Keep the center-of-pinch anchor specified by (x, y) in a stable
2607 // position over the course of the magnify.
2608 float page_scale_delta = active_tree_->page_scale_delta();
2609 gfx::PointF previous_scale_anchor =
2610 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2611 active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta);
2612 page_scale_delta = active_tree_->page_scale_delta();
2613 gfx::PointF new_scale_anchor =
2614 gfx::ScalePoint(anchor, 1.f / page_scale_delta);
2615 gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor;
2616
2617 previous_pinch_anchor_ = anchor;
2618
2619 move.Scale(1 / active_tree_->page_scale_factor());
[email protected]adeda572014-01-31 00:49:472620 // If clamping the inner viewport scroll offset causes a change, it should
2621 // be accounted for from the intended move.
2622 move -= InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272623
[email protected]adeda572014-01-31 00:49:472624 // We manually manage the bubbling behaviour here as it is different to that
2625 // implemented in LayerTreeHostImpl::ScrollBy(). Specifically:
2626 // 1) we want to explicit limit the bubbling to the outer/inner viewports,
2627 // 2) we don't want the directional limitations on the unused parts that
2628 // ScrollBy() implements, and
2629 // 3) pinching should not engage the top controls manager.
2630 gfx::Vector2dF unused = OuterViewportScrollLayer()
2631 ? OuterViewportScrollLayer()->ScrollBy(move)
2632 : move;
2633
2634 if (!unused.IsZero()) {
2635 InnerViewportScrollLayer()->ScrollBy(unused);
2636 InnerViewportScrollLayer()->ClampScrollToMaxScrollOffset();
2637 }
[email protected]c1bb5af2013-03-13 19:06:272638
[email protected]c1bb5af2013-03-13 19:06:272639 client_->SetNeedsCommitOnImplThread();
[email protected]4e4136d2013-11-29 02:22:442640 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272641 client_->RenewTreePriority();
[email protected]d3afa112012-12-08 06:24:282642}
2643
[email protected]c1bb5af2013-03-13 19:06:272644void LayerTreeHostImpl::PinchGestureEnd() {
2645 pinch_gesture_active_ = false;
[email protected]2fa342b82013-09-24 03:19:132646 if (pinch_gesture_end_should_clear_scrolling_layer_) {
2647 pinch_gesture_end_should_clear_scrolling_layer_ = false;
2648 ClearCurrentlyScrollingLayer();
2649 }
[email protected]a9bda5e2013-10-25 18:43:372650 if (top_controls_manager_)
2651 top_controls_manager_->PinchEnd();
[email protected]c1bb5af2013-03-13 19:06:272652 client_->SetNeedsCommitOnImplThread();
[email protected]94f206c12012-08-25 00:09:142653}
2654
[email protected]c1bb5af2013-03-13 19:06:272655static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info,
2656 LayerImpl* layer_impl) {
2657 if (!layer_impl)
2658 return;
[email protected]94f206c12012-08-25 00:09:142659
[email protected]c1bb5af2013-03-13 19:06:272660 gfx::Vector2d scroll_delta =
[email protected]1960a712013-04-30 17:06:472661 gfx::ToFlooredVector2d(layer_impl->ScrollDelta());
[email protected]c1bb5af2013-03-13 19:06:272662 if (!scroll_delta.IsZero()) {
2663 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]6ba914122013-03-22 16:26:392664 scroll.layer_id = layer_impl->id();
2665 scroll.scroll_delta = scroll_delta;
[email protected]c1bb5af2013-03-13 19:06:272666 scroll_info->scrolls.push_back(scroll);
2667 layer_impl->SetSentScrollDelta(scroll_delta);
2668 }
[email protected]94f206c12012-08-25 00:09:142669
[email protected]c1bb5af2013-03-13 19:06:272670 for (size_t i = 0; i < layer_impl->children().size(); ++i)
2671 CollectScrollDeltas(scroll_info, layer_impl->children()[i]);
[email protected]94f206c12012-08-25 00:09:142672}
2673
[email protected]c1bb5af2013-03-13 19:06:272674scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() {
2675 scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet());
[email protected]362f1e8b2013-01-21 16:54:302676
[email protected]c1bb5af2013-03-13 19:06:272677 CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer());
[email protected]6ba914122013-03-22 16:26:392678 scroll_info->page_scale_delta = active_tree_->page_scale_delta();
2679 active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta);
[email protected]362f1e8b2013-01-21 16:54:302680
[email protected]c1bb5af2013-03-13 19:06:272681 return scroll_info.Pass();
[email protected]362f1e8b2013-01-21 16:54:302682}
2683
[email protected]c1bb5af2013-03-13 19:06:272684void LayerTreeHostImpl::SetFullRootLayerDamage() {
[email protected]54af03522013-09-05 00:43:282685 SetViewportDamage(gfx::Rect(DrawViewportSize()));
[email protected]829ad972013-01-28 23:36:102686}
2687
[email protected]adeda572014-01-31 00:49:472688void LayerTreeHostImpl::ScrollViewportBy(gfx::Vector2dF scroll_delta) {
2689 DCHECK(InnerViewportScrollLayer());
2690 LayerImpl* scroll_layer = OuterViewportScrollLayer()
2691 ? OuterViewportScrollLayer()
2692 : InnerViewportScrollLayer();
2693
2694 gfx::Vector2dF unused_delta = scroll_layer->ScrollBy(scroll_delta);
2695
2696 if (!unused_delta.IsZero() && (scroll_layer == OuterViewportScrollLayer()))
2697 InnerViewportScrollLayer()->ScrollBy(unused_delta);
2698}
2699
[email protected]27152642014-03-11 20:42:002700void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks monotonic_time) {
[email protected]adeda572014-01-31 00:49:472701 if (!page_scale_animation_)
[email protected]c1bb5af2013-03-13 19:06:272702 return;
2703
[email protected]27152642014-03-11 20:42:002704 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2705 // time.
2706 double monotonic_time_for_cc_animations =
2707 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]adeda572014-01-31 00:49:472708 gfx::Vector2dF scroll_total = active_tree_->TotalScrollOffset();
[email protected]c1bb5af2013-03-13 19:06:272709
[email protected]34e1b512013-09-26 17:32:332710 if (!page_scale_animation_->IsAnimationStarted())
[email protected]27152642014-03-11 20:42:002711 page_scale_animation_->StartAnimation(monotonic_time_for_cc_animations);
[email protected]34e1b512013-09-26 17:32:332712
[email protected]27152642014-03-11 20:42:002713 active_tree_->SetPageScaleDelta(page_scale_animation_->PageScaleFactorAtTime(
2714 monotonic_time_for_cc_animations) /
2715 active_tree_->page_scale_factor());
2716 gfx::Vector2dF next_scroll = page_scale_animation_->ScrollOffsetAtTime(
2717 monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272718
[email protected]adeda572014-01-31 00:49:472719 ScrollViewportBy(next_scroll - scroll_total);
[email protected]4e4136d2013-11-29 02:22:442720 SetNeedsRedraw();
[email protected]c1bb5af2013-03-13 19:06:272721
[email protected]27152642014-03-11 20:42:002722 if (page_scale_animation_->IsAnimationCompleteAtTime(
2723 monotonic_time_for_cc_animations)) {
[email protected]c1bb5af2013-03-13 19:06:272724 page_scale_animation_.reset();
2725 client_->SetNeedsCommitOnImplThread();
2726 client_->RenewTreePriority();
2727 }
[email protected]829ad972013-01-28 23:36:102728}
2729
[email protected]ffb2720f2013-03-15 19:18:372730void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) {
[email protected]68d3fc12014-02-18 16:31:082731 if (!top_controls_manager_ || !top_controls_manager_->animation())
[email protected]ffb2720f2013-03-15 19:18:372732 return;
2733 gfx::Vector2dF scroll = top_controls_manager_->Animate(time);
[email protected]adeda572014-01-31 00:49:472734 if (active_tree_->TotalScrollOffset().y() == 0.f)
[email protected]5ef82622013-05-01 16:26:172735 return;
[email protected]5f7d9112014-01-17 21:31:422736 if (scroll.IsZero()) {
2737 // This may happen on the first animation step. Force redraw otherwise
2738 // the animation would stop because of no new frames.
2739 SetNeedsRedraw();
2740 } else {
[email protected]adeda572014-01-31 00:49:472741 ScrollViewportBy(gfx::ScaleVector2d(
[email protected]5f7d9112014-01-17 21:31:422742 scroll, 1.f / active_tree_->total_page_scale_factor()));
2743 }
[email protected]ffb2720f2013-03-15 19:18:372744}
2745
[email protected]27152642014-03-11 20:42:002746void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time) {
[email protected]8e0176d2013-03-21 03:14:522747 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362748 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272749 !active_tree_->root_layer())
2750 return;
2751
2752 TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers");
2753
[email protected]27152642014-03-11 20:42:002754 // TODO(ajuma): http://crbug.com/178171 - Animations use double for monotonic
2755 // time.
2756 double monotonic_time_for_cc_animations =
2757 (monotonic_time - base::TimeTicks()).InSecondsF();
[email protected]c1bb5af2013-03-13 19:06:272758 AnimationRegistrar::AnimationControllerMap copy =
2759 animation_registrar_->active_animation_controllers();
2760 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2761 iter != copy.end();
2762 ++iter)
[email protected]27152642014-03-11 20:42:002763 (*iter).second->Animate(monotonic_time_for_cc_animations);
[email protected]c1bb5af2013-03-13 19:06:272764
[email protected]4e4136d2013-11-29 02:22:442765 SetNeedsRedraw();
[email protected]131a0c22013-02-12 18:31:082766}
2767
[email protected]3d9f7432013-04-06 00:35:182768void LayerTreeHostImpl::UpdateAnimationState(bool start_ready_animations) {
[email protected]8e0176d2013-03-21 03:14:522769 if (!settings_.accelerated_animation_enabled ||
[email protected]cd77bb62014-03-27 05:16:362770 !needs_animate_layers() ||
[email protected]c1bb5af2013-03-13 19:06:272771 !active_tree_->root_layer())
2772 return;
2773
2774 TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState");
2775 scoped_ptr<AnimationEventsVector> events =
2776 make_scoped_ptr(new AnimationEventsVector);
2777 AnimationRegistrar::AnimationControllerMap copy =
2778 animation_registrar_->active_animation_controllers();
2779 for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin();
2780 iter != copy.end();
2781 ++iter)
[email protected]3d9f7432013-04-06 00:35:182782 (*iter).second->UpdateState(start_ready_animations, events.get());
[email protected]c1bb5af2013-03-13 19:06:272783
2784 if (!events->empty()) {
[email protected]85b57502014-03-11 15:37:482785 client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass());
[email protected]c1bb5af2013-03-13 19:06:272786 }
[email protected]131a0c22013-02-12 18:31:082787}
2788
[email protected]c1bb5af2013-03-13 19:06:272789base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const {
2790 return base::TimeDelta::FromSeconds(1);
2791}
2792
[email protected]50644642013-06-20 13:58:552793void LayerTreeHostImpl::SendReleaseResourcesRecursive(LayerImpl* current) {
[email protected]ff1211d2013-06-07 01:58:352794 DCHECK(current);
[email protected]9db24462014-01-14 02:25:502795 current->ReleaseResources();
[email protected]ff1211d2013-06-07 01:58:352796 if (current->mask_layer())
[email protected]50644642013-06-20 13:58:552797 SendReleaseResourcesRecursive(current->mask_layer());
[email protected]ff1211d2013-06-07 01:58:352798 if (current->replica_layer())
[email protected]50644642013-06-20 13:58:552799 SendReleaseResourcesRecursive(current->replica_layer());
[email protected]ff1211d2013-06-07 01:58:352800 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]50644642013-06-20 13:58:552801 SendReleaseResourcesRecursive(current->children()[i]);
[email protected]ff1211d2013-06-07 01:58:352802}
2803
[email protected]b5174d712013-08-28 08:10:432804void LayerTreeHostImpl::SetOffscreenContextProvider(
2805 const scoped_refptr<ContextProvider>& offscreen_context_provider) {
2806 if (!offscreen_context_provider.get()) {
2807 offscreen_context_provider_ = NULL;
2808 return;
2809 }
2810
2811 if (!offscreen_context_provider->BindToCurrentThread()) {
2812 offscreen_context_provider_ = NULL;
2813 return;
2814 }
2815
2816 offscreen_context_provider_ = offscreen_context_provider;
2817}
2818
[email protected]c1bb5af2013-03-13 19:06:272819std::string LayerTreeHostImpl::LayerTreeAsJson() const {
2820 std::string str;
2821 if (active_tree_->root_layer()) {
2822 scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson());
2823 base::JSONWriter::WriteWithOptions(
2824 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str);
2825 }
2826 return str;
2827}
2828
[email protected]c1bb5af2013-03-13 19:06:272829int LayerTreeHostImpl::SourceAnimationFrameNumber() const {
[email protected]9e3594522013-03-18 00:57:362830 return fps_counter_->current_frame_number();
[email protected]c1bb5af2013-03-13 19:06:272831}
2832
[email protected]c1bb5af2013-03-13 19:06:272833void LayerTreeHostImpl::SendManagedMemoryStats(
2834 size_t memory_visible_bytes,
2835 size_t memory_visible_and_nearby_bytes,
2836 size_t memory_use_bytes) {
2837 if (!renderer_)
2838 return;
2839
2840 // Round the numbers being sent up to the next 8MB, to throttle the rate
2841 // at which we spam the GPU process.
2842 static const size_t rounding_step = 8 * 1024 * 1024;
2843 memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step);
2844 memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes,
2845 rounding_step);
2846 memory_use_bytes = RoundUp(memory_use_bytes, rounding_step);
2847 if (last_sent_memory_visible_bytes_ == memory_visible_bytes &&
2848 last_sent_memory_visible_and_nearby_bytes_ ==
2849 memory_visible_and_nearby_bytes &&
2850 last_sent_memory_use_bytes_ == memory_use_bytes) {
2851 return;
2852 }
2853 last_sent_memory_visible_bytes_ = memory_visible_bytes;
2854 last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes;
2855 last_sent_memory_use_bytes_ = memory_use_bytes;
2856
2857 renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_,
2858 last_sent_memory_visible_and_nearby_bytes_,
2859 last_sent_memory_use_bytes_);
2860}
2861
2862void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) {
2863 AnimateScrollbarsRecursive(active_tree_->root_layer(), time);
2864}
2865
2866void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer,
2867 base::TimeTicks time) {
2868 if (!layer)
2869 return;
2870
2871 ScrollbarAnimationController* scrollbar_controller =
2872 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352873 if (scrollbar_controller && scrollbar_controller->Animate(time)) {
[email protected]0fc818e2013-03-18 06:45:202874 TRACE_EVENT_INSTANT0(
[email protected]c76faea2013-03-26 07:42:422875 "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars",
2876 TRACE_EVENT_SCOPE_THREAD);
[email protected]4e4136d2013-11-29 02:22:442877 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202878 }
[email protected]c1bb5af2013-03-13 19:06:272879
2880 for (size_t i = 0; i < layer->children().size(); ++i)
2881 AnimateScrollbarsRecursive(layer->children()[i], time);
2882}
2883
[email protected]21c9dee72013-06-15 01:20:052884void LayerTreeHostImpl::StartScrollbarAnimation() {
[email protected]0fc818e2013-03-18 06:45:202885 TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation");
[email protected]1dc06162014-03-26 22:54:452886 StartScrollbarAnimationRecursive(RootLayer(), CurrentFrameTimeTicks());
[email protected]0fc818e2013-03-18 06:45:202887}
2888
2889void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer,
2890 base::TimeTicks time) {
2891 if (!layer)
2892 return;
2893
2894 ScrollbarAnimationController* scrollbar_controller =
2895 layer->scrollbar_animation_controller();
[email protected]6bc09e82013-03-19 03:48:352896 if (scrollbar_controller && scrollbar_controller->IsAnimating()) {
2897 base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time);
[email protected]0fc818e2013-03-18 06:45:202898 if (delay > base::TimeDelta())
2899 client_->RequestScrollbarAnimationOnImplThread(delay);
[email protected]6bc09e82013-03-19 03:48:352900 else if (scrollbar_controller->Animate(time))
[email protected]4e4136d2013-11-29 02:22:442901 SetNeedsRedraw();
[email protected]0fc818e2013-03-18 06:45:202902 }
2903
2904 for (size_t i = 0; i < layer->children().size(); ++i)
2905 StartScrollbarAnimationRecursive(layer->children()[i], time);
2906}
2907
[email protected]c1bb5af2013-03-13 19:06:272908void LayerTreeHostImpl::SetTreePriority(TreePriority priority) {
2909 if (!tile_manager_)
2910 return;
2911
[email protected]1bcced22013-09-24 13:51:192912 if (global_tile_state_.tree_priority == priority)
[email protected]c1bb5af2013-03-13 19:06:272913 return;
[email protected]1bcced22013-09-24 13:51:192914 global_tile_state_.tree_priority = priority;
[email protected]c48536a52013-09-14 00:02:082915 DidModifyTilePriorities();
[email protected]c1bb5af2013-03-13 19:06:272916}
2917
[email protected]1dc06162014-03-26 22:54:452918void LayerTreeHostImpl::UpdateCurrentFrameTime() {
2919 DCHECK(current_frame_timeticks_.is_null());
2920 current_frame_timeticks_ = gfx::FrameTime::Now();
2921}
2922
[email protected]8347d692013-05-17 23:22:382923void LayerTreeHostImpl::ResetCurrentFrameTimeForNextFrame() {
[email protected]fb7425a2013-04-22 16:28:552924 current_frame_timeticks_ = base::TimeTicks();
[email protected]c1bb5af2013-03-13 19:06:272925}
2926
[email protected]fb7425a2013-04-22 16:28:552927base::TimeTicks LayerTreeHostImpl::CurrentFrameTimeTicks() {
[email protected]1dc06162014-03-26 22:54:452928 // Try to use the current frame time to keep animations non-jittery. But if
2929 // we're not in a frame (because this is during an input event or a delayed
2930 // task), fall back to physical time. This should still be monotonic.
2931 if (!current_frame_timeticks_.is_null())
2932 return current_frame_timeticks_;
[email protected]de2cf8c2013-10-25 19:46:462933 return gfx::FrameTime::Now();
[email protected]21c9dee72013-06-15 01:20:052934}
2935
[email protected]34806722013-08-09 23:51:212936scoped_ptr<base::Value> LayerTreeHostImpl::AsValueWithFrame(
2937 FrameData* frame) const {
[email protected]c1bb5af2013-03-13 19:06:272938 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
[email protected]f6742f52013-05-08 23:52:222939 if (this->pending_tree_)
2940 state->Set("activation_state", ActivationStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272941 state->Set("device_viewport_size",
[email protected]fa816c62013-03-18 04:24:212942 MathUtil::AsValue(device_viewport_size_).release());
[email protected]c1bb5af2013-03-13 19:06:272943 if (tile_manager_)
2944 state->Set("tiles", tile_manager_->AllTilesAsValue().release());
2945 state->Set("active_tree", active_tree_->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222946 if (pending_tree_)
2947 state->Set("pending_tree", pending_tree_->AsValue().release());
[email protected]34806722013-08-09 23:51:212948 if (frame)
2949 state->Set("frame", frame->AsValue().release());
[email protected]f6742f52013-05-08 23:52:222950 return state.PassAs<base::Value>();
2951}
2952
2953scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const {
[email protected]f6742f52013-05-08 23:52:222954 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
2955 state->Set("lthi", TracedValue::CreateIDRef(this).release());
[email protected]15cc9922013-05-24 07:31:472956 if (tile_manager_)
2957 state->Set("tile_manager", tile_manager_->BasicStateAsValue().release());
[email protected]c1bb5af2013-03-13 19:06:272958 return state.PassAs<base::Value>();
[email protected]131a0c22013-02-12 18:31:082959}
2960
[email protected]6e7fdeb2013-07-09 14:28:382961void LayerTreeHostImpl::SetDebugState(
2962 const LayerTreeDebugState& new_debug_state) {
2963 if (LayerTreeDebugState::Equal(debug_state_, new_debug_state))
2964 return;
2965 if (debug_state_.continuous_painting != new_debug_state.continuous_painting)
[email protected]c1bb5af2013-03-13 19:06:272966 paint_time_counter_->ClearHistory();
[email protected]652cf132013-02-15 21:53:242967
[email protected]6e7fdeb2013-07-09 14:28:382968 debug_state_ = new_debug_state;
[email protected]5547b4fe2013-10-01 23:14:122969 UpdateTileManagerMemoryPolicy(ActualManagedMemoryPolicy());
[email protected]6e7fdeb2013-07-09 14:28:382970 SetFullRootLayerDamage();
[email protected]d0d12192013-02-08 19:02:022971}
2972
[email protected]741fba422013-09-20 03:34:142973void LayerTreeHostImpl::CreateUIResource(UIResourceId uid,
2974 const UIResourceBitmap& bitmap) {
[email protected]c9280762013-08-01 06:28:572975 DCHECK_GT(uid, 0);
[email protected]c9280762013-08-01 06:28:572976
[email protected]efa48412013-09-05 15:30:162977 GLint wrap_mode = 0;
[email protected]741fba422013-09-20 03:34:142978 switch (bitmap.GetWrapMode()) {
[email protected]efa48412013-09-05 15:30:162979 case UIResourceBitmap::CLAMP_TO_EDGE:
2980 wrap_mode = GL_CLAMP_TO_EDGE;
2981 break;
2982 case UIResourceBitmap::REPEAT:
2983 wrap_mode = GL_REPEAT;
2984 break;
2985 }
2986
[email protected]c9280762013-08-01 06:28:572987 // Allow for multiple creation requests with the same UIResourceId. The
2988 // previous resource is simply deleted.
2989 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
2990 if (id)
2991 DeleteUIResource(uid);
[email protected]6be50ba82013-11-08 12:04:122992
2993 ResourceFormat format = resource_provider_->best_texture_format();
2994 if (bitmap.GetFormat() == UIResourceBitmap::ETC1)
2995 format = ETC1;
[email protected]c9280762013-08-01 06:28:572996 id = resource_provider_->CreateResource(
[email protected]741fba422013-09-20 03:34:142997 bitmap.GetSize(),
[email protected]efa48412013-09-05 15:30:162998 wrap_mode,
[email protected]27a41fe2013-09-19 05:05:142999 ResourceProvider::TextureUsageAny,
[email protected]6be50ba82013-11-08 12:04:123000 format);
[email protected]c9280762013-08-01 06:28:573001
[email protected]efbdb3a2013-10-04 00:35:133002 UIResourceData data;
3003 data.resource_id = id;
3004 data.size = bitmap.GetSize();
[email protected]709c9542013-10-26 01:43:513005 data.opaque = bitmap.GetOpaque();
[email protected]efbdb3a2013-10-04 00:35:133006
3007 ui_resource_map_[uid] = data;
[email protected]f6668c892013-09-26 10:05:033008
3009 AutoLockUIResourceBitmap bitmap_lock(bitmap);
[email protected]c9280762013-08-01 06:28:573010 resource_provider_->SetPixels(id,
[email protected]f6668c892013-09-26 10:05:033011 bitmap_lock.GetPixels(),
[email protected]741fba422013-09-20 03:34:143012 gfx::Rect(bitmap.GetSize()),
3013 gfx::Rect(bitmap.GetSize()),
[email protected]c9280762013-08-01 06:28:573014 gfx::Vector2d(0, 0));
[email protected]127bdc1a2013-09-11 01:44:483015 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:573016}
3017
3018void LayerTreeHostImpl::DeleteUIResource(UIResourceId uid) {
3019 ResourceProvider::ResourceId id = ResourceIdForUIResource(uid);
3020 if (id) {
3021 resource_provider_->DeleteResource(id);
3022 ui_resource_map_.erase(uid);
3023 }
[email protected]127bdc1a2013-09-11 01:44:483024 MarkUIResourceNotEvicted(uid);
[email protected]c9280762013-08-01 06:28:573025}
3026
[email protected]127bdc1a2013-09-11 01:44:483027void LayerTreeHostImpl::EvictAllUIResources() {
3028 if (ui_resource_map_.empty())
3029 return;
3030
[email protected]5f4dc4f2013-09-05 14:58:213031 for (UIResourceMap::const_iterator iter = ui_resource_map_.begin();
3032 iter != ui_resource_map_.end();
3033 ++iter) {
[email protected]127bdc1a2013-09-11 01:44:483034 evicted_ui_resources_.insert(iter->first);
[email protected]efbdb3a2013-10-04 00:35:133035 resource_provider_->DeleteResource(iter->second.resource_id);
[email protected]5f4dc4f2013-09-05 14:58:213036 }
3037 ui_resource_map_.clear();
[email protected]127bdc1a2013-09-11 01:44:483038
3039 client_->SetNeedsCommitOnImplThread();
3040 client_->OnCanDrawStateChanged(CanDraw());
3041 client_->RenewTreePriority();
[email protected]5f4dc4f2013-09-05 14:58:213042}
3043
[email protected]c9280762013-08-01 06:28:573044ResourceProvider::ResourceId LayerTreeHostImpl::ResourceIdForUIResource(
3045 UIResourceId uid) const {
3046 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3047 if (iter != ui_resource_map_.end())
[email protected]efbdb3a2013-10-04 00:35:133048 return iter->second.resource_id;
[email protected]c9280762013-08-01 06:28:573049 return 0;
3050}
3051
[email protected]709c9542013-10-26 01:43:513052bool LayerTreeHostImpl::IsUIResourceOpaque(UIResourceId uid) const {
3053 UIResourceMap::const_iterator iter = ui_resource_map_.find(uid);
3054 DCHECK(iter != ui_resource_map_.end());
3055 return iter->second.opaque;
3056}
3057
[email protected]127bdc1a2013-09-11 01:44:483058bool LayerTreeHostImpl::EvictedUIResourcesExist() const {
3059 return !evicted_ui_resources_.empty();
3060}
3061
3062void LayerTreeHostImpl::MarkUIResourceNotEvicted(UIResourceId uid) {
3063 std::set<UIResourceId>::iterator found_in_evicted =
3064 evicted_ui_resources_.find(uid);
3065 if (found_in_evicted == evicted_ui_resources_.end())
3066 return;
3067 evicted_ui_resources_.erase(found_in_evicted);
3068 if (evicted_ui_resources_.empty())
3069 client_->OnCanDrawStateChanged(CanDraw());
3070}
3071
[email protected]5e5648a2013-11-18 00:39:333072void LayerTreeHostImpl::ScheduleMicroBenchmark(
3073 scoped_ptr<MicroBenchmarkImpl> benchmark) {
3074 micro_benchmark_controller_.ScheduleRun(benchmark.Pass());
3075}
3076
[email protected]6be422b2013-12-08 06:47:313077void LayerTreeHostImpl::InsertSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3078 swap_promise_monitor_.insert(monitor);
3079}
3080
3081void LayerTreeHostImpl::RemoveSwapPromiseMonitor(SwapPromiseMonitor* monitor) {
3082 swap_promise_monitor_.erase(monitor);
3083}
3084
3085void LayerTreeHostImpl::NotifySwapPromiseMonitorsOfSetNeedsRedraw() {
3086 std::set<SwapPromiseMonitor*>::iterator it = swap_promise_monitor_.begin();
3087 for (; it != swap_promise_monitor_.end(); it++)
3088 (*it)->OnSetNeedsRedrawOnImpl();
3089}
3090
[email protected]d3143c732012-10-05 19:17:593091} // namespace cc