1 // 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 5 #ifndef CC_TREES_LAYER_TREE_HOST_H_ 6 #define CC_TREES_LAYER_TREE_HOST_H_ 7 8 #include <limits> 9 #include <list> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/cancelable_callback.h" 14 #include "base/containers/hash_tables.h" 15 #include "base/memory/ref_counted.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/weak_ptr.h" 18 #include "base/time/time.h" 19 #include "cc/animation/animation_events.h" 20 #include "cc/base/cc_export.h" 21 #include "cc/base/scoped_ptr_vector.h" 22 #include "cc/input/input_handler.h" 23 #include "cc/input/scrollbar.h" 24 #include "cc/input/top_controls_state.h" 25 #include "cc/layers/layer_lists.h" 26 #include "cc/output/output_surface.h" 27 #include "cc/resources/ui_resource_bitmap.h" 28 #include "cc/resources/ui_resource_client.h" 29 #include "cc/scheduler/rate_limiter.h" 30 #include "cc/trees/layer_tree_host_client.h" 31 #include "cc/trees/layer_tree_host_common.h" 32 #include "cc/trees/layer_tree_settings.h" 33 #include "cc/trees/occlusion_tracker.h" 34 #include "cc/trees/proxy.h" 35 #include "third_party/skia/include/core/SkColor.h" 36 #include "ui/base/latency_info.h" 37 #include "ui/gfx/rect.h" 38 39 namespace WebKit { class WebGraphicsContext3D; } 40 41 #if defined(COMPILER_GCC) 42 namespace BASE_HASH_NAMESPACE { 43 template <> 44 struct hash<WebKit::WebGraphicsContext3D*> { 45 size_t operator()(WebKit::WebGraphicsContext3D* ptr) const { 46 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); 47 } 48 }; 49 } // namespace BASE_HASH_NAMESPACE 50 #endif // COMPILER 51 52 namespace cc { 53 54 class AnimationRegistrar; 55 class HeadsUpDisplayLayer; 56 class Layer; 57 class LayerTreeHostImpl; 58 class LayerTreeHostImplClient; 59 class PrioritizedResourceManager; 60 class PrioritizedResource; 61 class Region; 62 class RenderingStatsInstrumentation; 63 class ResourceProvider; 64 class ResourceUpdateQueue; 65 class TopControlsManager; 66 struct RenderingStats; 67 struct ScrollAndScaleSet; 68 69 // Provides information on an Impl's rendering capabilities back to the 70 // LayerTreeHost. 71 struct CC_EXPORT RendererCapabilities { 72 RendererCapabilities(); 73 ~RendererCapabilities(); 74 75 unsigned best_texture_format; 76 bool using_partial_swap; 77 bool using_set_visibility; 78 bool using_egl_image; 79 bool allow_partial_texture_updates; 80 bool using_offscreen_context3d; 81 int max_texture_size; 82 bool avoid_pow2_textures; 83 bool using_map_image; 84 bool using_shared_memory_resources; 85 }; 86 87 struct CC_EXPORT UIResourceRequest { 88 enum UIResourceRequestType { 89 UIResourceCreate, 90 UIResourceDelete, 91 UIResourceInvalidRequest 92 }; 93 94 UIResourceRequest(); 95 ~UIResourceRequest(); 96 UIResourceRequestType type; 97 UIResourceId id; 98 scoped_refptr<UIResourceBitmap> bitmap; 99 }; 100 101 class CC_EXPORT LayerTreeHost : NON_EXPORTED_BASE(public RateLimiterClient) { 102 public: 103 static scoped_ptr<LayerTreeHost> Create( 104 LayerTreeHostClient* client, 105 const LayerTreeSettings& settings, 106 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner); 107 virtual ~LayerTreeHost(); 108 109 void SetLayerTreeHostClientReady(); 110 111 // Returns true if any LayerTreeHost is alive. 112 static bool AnyLayerTreeHostInstanceExists(); 113 114 void set_needs_filter_context() { needs_filter_context_ = true; } 115 bool needs_offscreen_context() const { 116 return needs_filter_context_; 117 } 118 119 // LayerTreeHost interface to Proxy. 120 void WillBeginFrame() { client_->WillBeginFrame(); } 121 void DidBeginFrame(); 122 void UpdateClientAnimations(base::TimeTicks monotonic_frame_begin_time); 123 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time); 124 void DidStopFlinging(); 125 void Layout(); 126 void BeginCommitOnImplThread(LayerTreeHostImpl* host_impl); 127 void FinishCommitOnImplThread(LayerTreeHostImpl* host_impl); 128 void WillCommit(); 129 void CommitComplete(); 130 scoped_ptr<OutputSurface> CreateOutputSurface(); 131 virtual scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl( 132 LayerTreeHostImplClient* client); 133 void DidLoseOutputSurface(); 134 bool output_surface_lost() const { return output_surface_lost_; } 135 enum CreateResult { 136 CreateSucceeded, 137 CreateFailedButTryAgain, 138 CreateFailedAndGaveUp, 139 }; 140 CreateResult OnCreateAndInitializeOutputSurfaceAttempted(bool success); 141 void DidCommitAndDrawFrame() { client_->DidCommitAndDrawFrame(); } 142 void DidCompleteSwapBuffers() { client_->DidCompleteSwapBuffers(); } 143 void DeleteContentsTexturesOnImplThread(ResourceProvider* resource_provider); 144 virtual void AcquireLayerTextures(); 145 // Returns false if we should abort this frame due to initialization failure. 146 bool InitializeOutputSurfaceIfNeeded(); 147 bool UpdateLayers(ResourceUpdateQueue* queue, 148 size_t contents_memory_limit_bytes); 149 150 LayerTreeHostClient* client() { return client_; } 151 const base::WeakPtr<InputHandler>& GetInputHandler() { 152 return input_handler_weak_ptr_; 153 } 154 155 void NotifyInputThrottledUntilCommit(); 156 157 void Composite(base::TimeTicks frame_begin_time); 158 159 // Only used when compositing on the main thread. 160 void ScheduleComposite(); 161 162 // Composites and attempts to read back the result into the provided 163 // buffer. If it wasn't possible, e.g. due to context lost, will return 164 // false. 165 bool CompositeAndReadback(void* pixels, gfx::Rect rect_in_device_viewport); 166 167 void FinishAllRendering(); 168 169 void SetDeferCommits(bool defer_commits); 170 171 // Test only hook 172 virtual void DidDeferCommit(); 173 174 int source_frame_number() const { return source_frame_number_; } 175 176 void SetNeedsDisplayOnAllLayers(); 177 178 void CollectRenderingStats(RenderingStats* stats) const; 179 180 RenderingStatsInstrumentation* rendering_stats_instrumentation() const { 181 return rendering_stats_instrumentation_.get(); 182 } 183 184 const RendererCapabilities& GetRendererCapabilities() const; 185 186 void SetNeedsAnimate(); 187 virtual void SetNeedsUpdateLayers(); 188 virtual void SetNeedsCommit(); 189 virtual void SetNeedsFullTreeSync(); 190 void SetNeedsRedraw(); 191 void SetNeedsRedrawRect(gfx::Rect damage_rect); 192 bool CommitRequested() const; 193 194 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> events, 195 base::Time wall_clock_time); 196 197 void SetRootLayer(scoped_refptr<Layer> root_layer); 198 Layer* root_layer() { return root_layer_.get(); } 199 const Layer* root_layer() const { return root_layer_.get(); } 200 201 const LayerTreeSettings& settings() const { return settings_; } 202 203 void SetDebugState(const LayerTreeDebugState& debug_state); 204 const LayerTreeDebugState& debug_state() const { return debug_state_; } 205 206 void SetViewportSize(gfx::Size device_viewport_size); 207 void SetOverdrawBottomHeight(float overdraw_bottom_height); 208 209 gfx::Size device_viewport_size() const { return device_viewport_size_; } 210 float overdraw_bottom_height() const { return overdraw_bottom_height_; } 211 212 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta); 213 void SetPageScaleFactorAndLimits(float page_scale_factor, 214 float min_page_scale_factor, 215 float max_page_scale_factor); 216 float page_scale_factor() const { return page_scale_factor_; } 217 218 SkColor background_color() const { return background_color_; } 219 void set_background_color(SkColor color) { background_color_ = color; } 220 221 void set_has_transparent_background(bool transparent) { 222 has_transparent_background_ = transparent; 223 } 224 225 PrioritizedResourceManager* contents_texture_manager() const { 226 return contents_texture_manager_.get(); 227 } 228 229 void SetVisible(bool visible); 230 bool visible() const { return visible_; } 231 232 void StartPageScaleAnimation(gfx::Vector2d target_offset, 233 bool use_anchor, 234 float scale, 235 base::TimeDelta duration); 236 237 void ApplyScrollAndScale(const ScrollAndScaleSet& info); 238 239 void SetImplTransform(const gfx::Transform& transform); 240 void SetLatencyInfo(const ui::LatencyInfo& latency_info); 241 242 virtual void StartRateLimiter(WebKit::WebGraphicsContext3D* context3d); 243 virtual void StopRateLimiter(WebKit::WebGraphicsContext3D* context3d); 244 245 // RateLimiterClient implementation. 246 virtual void RateLimit() OVERRIDE; 247 248 bool buffered_updates() const { 249 return settings_.max_partial_texture_updates != 250 std::numeric_limits<size_t>::max(); 251 } 252 bool RequestPartialTextureUpdate(); 253 254 void SetDeviceScaleFactor(float device_scale_factor); 255 float device_scale_factor() const { return device_scale_factor_; } 256 257 void UpdateTopControlsState(TopControlsState constraints, 258 TopControlsState current, 259 bool animate); 260 261 HeadsUpDisplayLayer* hud_layer() const { return hud_layer_.get(); } 262 263 Proxy* proxy() const { return proxy_.get(); } 264 265 AnimationRegistrar* animation_registrar() const { 266 return animation_registrar_.get(); 267 } 268 269 bool BlocksPendingCommit() const; 270 271 // Obtains a thorough dump of the LayerTreeHost as a value. 272 scoped_ptr<base::Value> AsValue() const; 273 274 bool in_paint_layer_contents() const { return in_paint_layer_contents_; } 275 276 // CreateUIResource creates a resource given a bitmap. The bitmap is 277 // generated via an interface function, which is called when initializing the 278 // resource and when the resource has been lost (due to lost context). The 279 // parameter of the interface is a single boolean, which indicates whether the 280 // resource has been lost or not. CreateUIResource returns an Id of the 281 // resource, which is always positive. 282 virtual UIResourceId CreateUIResource(UIResourceClient* client); 283 // Deletes a UI resource. May safely be called more than once. 284 virtual void DeleteUIResource(UIResourceId id); 285 286 bool UsingSharedMemoryResources(); 287 int id() const { return tree_id_; } 288 289 protected: 290 LayerTreeHost(LayerTreeHostClient* client, const LayerTreeSettings& settings); 291 bool Initialize(scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner); 292 bool InitializeForTesting(scoped_ptr<Proxy> proxy_for_testing); 293 294 private: 295 bool InitializeProxy(scoped_ptr<Proxy> proxy); 296 297 void PaintLayerContents( 298 const RenderSurfaceLayerList& render_surface_layer_list, 299 ResourceUpdateQueue* queue, 300 bool* did_paint_content, 301 bool* need_more_updates); 302 void PaintMasksForRenderSurface(Layer* render_surface_layer, 303 ResourceUpdateQueue* queue, 304 bool* did_paint_content, 305 bool* need_more_updates); 306 bool UpdateLayers(Layer* root_layer, ResourceUpdateQueue* queue); 307 void UpdateHudLayer(); 308 void TriggerPrepaint(); 309 310 void ReduceMemoryUsage(); 311 312 void PrioritizeTextures( 313 const RenderSurfaceLayerList& render_surface_layer_list, 314 OverdrawMetrics* metrics); 315 void SetPrioritiesForSurfaces(size_t surface_memory_bytes); 316 void SetPrioritiesForLayers(const RenderSurfaceLayerList& update_list); 317 size_t CalculateMemoryForRenderSurfaces( 318 const RenderSurfaceLayerList& update_list); 319 320 bool AnimateLayersRecursive(Layer* current, base::TimeTicks time); 321 322 void UIResourceLost(UIResourceId id); 323 324 void DidLoseUIResources(); 325 326 typedef base::hash_map<UIResourceId, UIResourceClient*> UIResourceClientMap; 327 UIResourceClientMap ui_resource_client_map_; 328 int next_ui_resource_id_; 329 330 typedef std::list<UIResourceRequest> UIResourceRequestQueue; 331 UIResourceRequestQueue ui_resource_request_queue_; 332 333 void CalculateLCDTextMetricsCallback(Layer* layer); 334 335 bool animating_; 336 bool needs_full_tree_sync_; 337 bool needs_filter_context_; 338 339 base::CancelableClosure prepaint_callback_; 340 341 LayerTreeHostClient* client_; 342 scoped_ptr<Proxy> proxy_; 343 344 int source_frame_number_; 345 scoped_ptr<RenderingStatsInstrumentation> rendering_stats_instrumentation_; 346 347 bool output_surface_can_be_initialized_; 348 bool output_surface_lost_; 349 int num_failed_recreate_attempts_; 350 351 scoped_refptr<Layer> root_layer_; 352 scoped_refptr<HeadsUpDisplayLayer> hud_layer_; 353 354 scoped_ptr<PrioritizedResourceManager> contents_texture_manager_; 355 scoped_ptr<PrioritizedResource> surface_memory_placeholder_; 356 357 base::WeakPtr<InputHandler> input_handler_weak_ptr_; 358 base::WeakPtr<TopControlsManager> top_controls_manager_weak_ptr_; 359 360 LayerTreeSettings settings_; 361 LayerTreeDebugState debug_state_; 362 363 gfx::Size device_viewport_size_; 364 float overdraw_bottom_height_; 365 float device_scale_factor_; 366 367 bool visible_; 368 369 typedef base::hash_map<WebKit::WebGraphicsContext3D*, 370 scoped_refptr<RateLimiter> > RateLimiterMap; 371 RateLimiterMap rate_limiters_; 372 373 float page_scale_factor_; 374 float min_page_scale_factor_; 375 float max_page_scale_factor_; 376 gfx::Transform impl_transform_; 377 bool trigger_idle_updates_; 378 379 SkColor background_color_; 380 bool has_transparent_background_; 381 382 typedef ScopedPtrVector<PrioritizedResource> TextureList; 383 size_t partial_texture_update_requests_; 384 385 scoped_ptr<AnimationRegistrar> animation_registrar_; 386 387 struct PendingPageScaleAnimation { 388 gfx::Vector2d target_offset; 389 bool use_anchor; 390 float scale; 391 base::TimeDelta duration; 392 }; 393 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_; 394 395 bool in_paint_layer_contents_; 396 397 ui::LatencyInfo latency_info_; 398 399 static const int kTotalFramesToUseForLCDTextMetrics = 50; 400 int total_frames_used_for_lcd_text_metrics_; 401 402 struct LCDTextMetrics { 403 LCDTextMetrics() 404 : total_num_cc_layers(0), 405 total_num_cc_layers_can_use_lcd_text(0), 406 total_num_cc_layers_will_use_lcd_text(0) {} 407 408 int64 total_num_cc_layers; 409 int64 total_num_cc_layers_can_use_lcd_text; 410 int64 total_num_cc_layers_will_use_lcd_text; 411 }; 412 LCDTextMetrics lcd_text_metrics_; 413 int tree_id_; 414 415 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost); 416 }; 417 418 } // namespace cc 419 420 #endif // CC_TREES_LAYER_TREE_HOST_H_ 421