OLD | NEW |
| (Empty) |
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_LAYER_IMPL_H_ | |
6 #define CC_LAYER_IMPL_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/values.h" | |
13 #include "cc/animation/layer_animation_controller.h" | |
14 #include "cc/animation/layer_animation_value_observer.h" | |
15 #include "cc/base/cc_export.h" | |
16 #include "cc/base/region.h" | |
17 #include "cc/base/scoped_ptr_vector.h" | |
18 #include "cc/draw_properties.h" | |
19 #include "cc/input/input_handler.h" | |
20 #include "cc/quads/render_pass.h" | |
21 #include "cc/quads/shared_quad_state.h" | |
22 #include "cc/render_surface_impl.h" | |
23 #include "cc/resources/resource_provider.h" | |
24 #include "skia/ext/refptr.h" | |
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations
.h" | |
26 #include "third_party/skia/include/core/SkColor.h" | |
27 #include "third_party/skia/include/core/SkImageFilter.h" | |
28 #include "third_party/skia/include/core/SkPicture.h" | |
29 #include "ui/gfx/rect.h" | |
30 #include "ui/gfx/rect_f.h" | |
31 #include "ui/gfx/transform.h" | |
32 | |
33 namespace base { | |
34 class DictionaryValue; | |
35 } | |
36 | |
37 namespace cc { | |
38 | |
39 class LayerTreeHostImpl; | |
40 class LayerTreeImpl; | |
41 class QuadSink; | |
42 class Renderer; | |
43 class ScrollbarAnimationController; | |
44 class ScrollbarLayerImpl; | |
45 class Layer; | |
46 | |
47 struct AppendQuadsData; | |
48 | |
49 class CC_EXPORT LayerImpl : LayerAnimationValueObserver { | |
50 public: | |
51 typedef ScopedPtrVector<LayerImpl> LayerList; | |
52 | |
53 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) { | |
54 return make_scoped_ptr(new LayerImpl(tree_impl, id)); | |
55 } | |
56 | |
57 virtual ~LayerImpl(); | |
58 | |
59 int id() const { return layer_id_; } | |
60 | |
61 // LayerAnimationValueObserver implementation. | |
62 virtual void OnOpacityAnimated(float opacity) OVERRIDE; | |
63 virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE; | |
64 virtual bool IsActive() const OVERRIDE; | |
65 | |
66 // Tree structure. | |
67 LayerImpl* parent() { return parent_; } | |
68 const LayerImpl* parent() const { return parent_; } | |
69 const LayerList& children() const { return children_; } | |
70 LayerList& children() { return children_; } | |
71 LayerImpl* child_at(size_t index) const { return children_[index]; } | |
72 void AddChild(scoped_ptr<LayerImpl> child); | |
73 scoped_ptr<LayerImpl> RemoveChild(LayerImpl* child); | |
74 void set_parent(LayerImpl* parent) { parent_ = parent; } | |
75 // Warning: This does not preserve tree structure invariants. | |
76 void ClearChildList(); | |
77 | |
78 void SetMaskLayer(scoped_ptr<LayerImpl> mask_layer); | |
79 LayerImpl* mask_layer() { return mask_layer_.get(); } | |
80 const LayerImpl* mask_layer() const { return mask_layer_.get(); } | |
81 scoped_ptr<LayerImpl> TakeMaskLayer(); | |
82 | |
83 void SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer); | |
84 LayerImpl* replica_layer() { return replica_layer_.get(); } | |
85 const LayerImpl* replica_layer() const { return replica_layer_.get(); } | |
86 scoped_ptr<LayerImpl> TakeReplicaLayer(); | |
87 | |
88 bool has_mask() const { return mask_layer_; } | |
89 bool has_replica() const { return replica_layer_; } | |
90 bool replica_has_mask() const { | |
91 return replica_layer_ && (mask_layer_ || replica_layer_->mask_layer_); | |
92 } | |
93 | |
94 LayerTreeImpl* layer_tree_impl() const { return layer_tree_impl_; } | |
95 | |
96 scoped_ptr<SharedQuadState> CreateSharedQuadState() const; | |
97 // willDraw must be called before appendQuads. If willDraw is called, | |
98 // didDraw is guaranteed to be called before another willDraw or before | |
99 // the layer is destroyed. To enforce this, any class that overrides | |
100 // willDraw/didDraw must call the base class version. | |
101 virtual void WillDraw(ResourceProvider* resource_provider); | |
102 virtual void AppendQuads(QuadSink* quad_sink, | |
103 AppendQuadsData* append_quads_data) {} | |
104 virtual void DidDraw(ResourceProvider* resource_provider); | |
105 | |
106 virtual ResourceProvider::ResourceId ContentsResourceId() const; | |
107 | |
108 virtual bool HasDelegatedContent() const; | |
109 virtual bool HasContributingDelegatedRenderPasses() const; | |
110 virtual RenderPass::Id FirstContributingRenderPassId() const; | |
111 virtual RenderPass::Id NextContributingRenderPassId(RenderPass::Id id) const; | |
112 | |
113 virtual void UpdateTilePriorities() {} | |
114 | |
115 virtual ScrollbarLayerImpl* ToScrollbarLayer(); | |
116 | |
117 // Returns true if this layer has content to draw. | |
118 void SetDrawsContent(bool draws_content); | |
119 bool DrawsContent() const { return draws_content_; } | |
120 | |
121 bool force_render_surface() const { return force_render_surface_; } | |
122 void SetForceRenderSurface(bool force) { force_render_surface_ = force; } | |
123 | |
124 void SetAnchorPoint(gfx::PointF anchor_point); | |
125 gfx::PointF anchor_point() const { return anchor_point_; } | |
126 | |
127 void SetAnchorPointZ(float anchor_point_z); | |
128 float anchor_point_z() const { return anchor_point_z_; } | |
129 | |
130 void SetBackgroundColor(SkColor background_color); | |
131 SkColor background_color() const { return background_color_; } | |
132 | |
133 void SetFilters(const WebKit::WebFilterOperations& filters); | |
134 const WebKit::WebFilterOperations& filters() const { return filters_; } | |
135 | |
136 void SetBackgroundFilters(const WebKit::WebFilterOperations& filters); | |
137 const WebKit::WebFilterOperations& background_filters() const { | |
138 return background_filters_; | |
139 } | |
140 | |
141 void SetFilter(const skia::RefPtr<SkImageFilter>& filter); | |
142 skia::RefPtr<SkImageFilter> filter() const { return filter_; } | |
143 | |
144 void SetMasksToBounds(bool masks_to_bounds); | |
145 bool masks_to_bounds() const { return masks_to_bounds_; } | |
146 | |
147 void SetContentsOpaque(bool opaque); | |
148 bool contents_opaque() const { return contents_opaque_; } | |
149 | |
150 void SetOpacity(float opacity); | |
151 float opacity() const { return opacity_; } | |
152 bool OpacityIsAnimating() const; | |
153 bool OpacityIsAnimatingOnImplOnly() const; | |
154 | |
155 void SetPosition(gfx::PointF position); | |
156 gfx::PointF position() const { return position_; } | |
157 | |
158 void SetIsContainerForFixedPositionLayers(bool container) { | |
159 is_container_for_fixed_position_layers_ = container; | |
160 } | |
161 bool is_container_for_fixed_position_layers() const { | |
162 return is_container_for_fixed_position_layers_; | |
163 } | |
164 | |
165 void SetFixedToContainerLayer(bool fixed) { | |
166 fixed_to_container_layer_ = fixed; | |
167 } | |
168 bool fixed_to_container_layer() const { return fixed_to_container_layer_; } | |
169 | |
170 void SetPreserves3d(bool preserves_3d); | |
171 bool preserves_3d() const { return preserves_3d_; } | |
172 | |
173 void SetUseParentBackfaceVisibility(bool use) { | |
174 use_parent_backface_visibility_ = use; | |
175 } | |
176 bool use_parent_backface_visibility() const { | |
177 return use_parent_backface_visibility_; | |
178 } | |
179 | |
180 void SetSublayerTransform(const gfx::Transform& sublayer_transform); | |
181 const gfx::Transform& sublayer_transform() const { | |
182 return sublayer_transform_; | |
183 } | |
184 | |
185 // Debug layer name. | |
186 void SetDebugName(const std::string& debug_name) { debug_name_ = debug_name; } | |
187 std::string debug_name() const { return debug_name_; } | |
188 | |
189 bool ShowDebugBorders() const; | |
190 | |
191 // These invalidate the host's render surface layer list. The caller | |
192 // is responsible for calling setNeedsUpdateDrawProperties on the host | |
193 // so that its list can be recreated. | |
194 void CreateRenderSurface(); | |
195 void ClearRenderSurface() { draw_properties_.render_surface.reset(); } | |
196 | |
197 DrawProperties<LayerImpl, RenderSurfaceImpl>& draw_properties() { | |
198 return draw_properties_; | |
199 } | |
200 const DrawProperties<LayerImpl, RenderSurfaceImpl>& draw_properties() const { | |
201 return draw_properties_; | |
202 } | |
203 | |
204 // The following are shortcut accessors to get various information from | |
205 // draw_properties_ | |
206 const gfx::Transform& draw_transform() const { | |
207 return draw_properties_.target_space_transform; | |
208 } | |
209 const gfx::Transform& screen_space_transform() const { | |
210 return draw_properties_.screen_space_transform; | |
211 } | |
212 float draw_opacity() const { return draw_properties_.opacity; } | |
213 bool draw_opacity_is_animating() const { | |
214 return draw_properties_.opacity_is_animating; | |
215 } | |
216 bool draw_transform_is_animating() const { | |
217 return draw_properties_.target_space_transform_is_animating; | |
218 } | |
219 bool screen_space_transform_is_animating() const { | |
220 return draw_properties_.screen_space_transform_is_animating; | |
221 } | |
222 bool screen_space_opacity_is_animating() const { | |
223 return draw_properties_.screen_space_opacity_is_animating; | |
224 } | |
225 bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; } | |
226 bool is_clipped() const { return draw_properties_.is_clipped; } | |
227 gfx::Rect clip_rect() const { return draw_properties_.clip_rect; } | |
228 gfx::Rect drawable_content_rect() const { | |
229 return draw_properties_.drawable_content_rect; | |
230 } | |
231 gfx::Rect visible_content_rect() const { | |
232 return draw_properties_.visible_content_rect; | |
233 } | |
234 LayerImpl* render_target() { | |
235 DCHECK(!draw_properties_.render_target || | |
236 draw_properties_.render_target->render_surface()); | |
237 return draw_properties_.render_target; | |
238 } | |
239 const LayerImpl* render_target() const { | |
240 DCHECK(!draw_properties_.render_target || | |
241 draw_properties_.render_target->render_surface()); | |
242 return draw_properties_.render_target; | |
243 } | |
244 RenderSurfaceImpl* render_surface() const { | |
245 return draw_properties_.render_surface.get(); | |
246 } | |
247 | |
248 // The client should be responsible for setting bounds, contentBounds and | |
249 // contentsScale to appropriate values. LayerImpl doesn't calculate any of | |
250 // them from the other values. | |
251 | |
252 void SetBounds(gfx::Size bounds); | |
253 gfx::Size bounds() const { return bounds_; } | |
254 | |
255 void SetContentBounds(gfx::Size content_bounds); | |
256 gfx::Size content_bounds() const { return draw_properties_.content_bounds; } | |
257 | |
258 float contents_scale_x() const { return draw_properties_.contents_scale_x; } | |
259 float contents_scale_y() const { return draw_properties_.contents_scale_y; } | |
260 void SetContentsScale(float contents_scale_x, float contents_scale_y); | |
261 | |
262 virtual void CalculateContentsScale(float ideal_contents_scale, | |
263 bool animating_transform_to_screen, | |
264 float* contents_scale_x, | |
265 float* contents_scale_y, | |
266 gfx::Size* contentBounds); | |
267 | |
268 void SetScrollOffset(gfx::Vector2d scroll_offset); | |
269 gfx::Vector2d scroll_offset() const { return scroll_offset_; } | |
270 | |
271 void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset); | |
272 gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; } | |
273 | |
274 void SetScrollDelta(gfx::Vector2dF scroll_delta); | |
275 gfx::Vector2dF scroll_delta() const { return scroll_delta_; } | |
276 | |
277 gfx::Vector2dF TotalScrollOffset() const; | |
278 | |
279 void SetImplTransform(const gfx::Transform& transform); | |
280 const gfx::Transform& impl_transform() const { return impl_transform_; } | |
281 | |
282 void SetSentScrollDelta(gfx::Vector2d sent_scroll_delta); | |
283 gfx::Vector2d sent_scroll_delta() const { return sent_scroll_delta_; } | |
284 | |
285 // Returns the delta of the scroll that was outside of the bounds of the | |
286 // initial scroll | |
287 gfx::Vector2dF ScrollBy(gfx::Vector2dF scroll); | |
288 | |
289 void SetScrollable(bool scrollable) { scrollable_ = scrollable; } | |
290 bool scrollable() const { return scrollable_; } | |
291 | |
292 void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) { | |
293 should_scroll_on_main_thread_ = should_scroll_on_main_thread; | |
294 } | |
295 bool should_scroll_on_main_thread() const { | |
296 return should_scroll_on_main_thread_; | |
297 } | |
298 | |
299 void SetHaveWheelEventHandlers(bool have_wheel_event_handlers) { | |
300 have_wheel_event_handlers_ = have_wheel_event_handlers; | |
301 } | |
302 bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; } | |
303 | |
304 void SetNonFastScrollableRegion(const Region& region) { | |
305 non_fast_scrollable_region_ = region; | |
306 } | |
307 const Region& non_fast_scrollable_region() const { | |
308 return non_fast_scrollable_region_; | |
309 } | |
310 | |
311 void SetTouchEventHandlerRegion(const Region& region) { | |
312 touch_event_handler_region_ = region; | |
313 } | |
314 const Region& touch_event_handler_region() const { | |
315 return touch_event_handler_region_; | |
316 } | |
317 | |
318 void SetDrawCheckerboardForMissingTiles(bool checkerboard) { | |
319 draw_checkerboard_for_missing_tiles_ = checkerboard; | |
320 } | |
321 bool DrawCheckerboardForMissingTiles() const; | |
322 | |
323 InputHandlerClient::ScrollStatus TryScroll( | |
324 gfx::PointF screen_space_point, | |
325 InputHandlerClient::ScrollInputType type) const; | |
326 | |
327 void SetDoubleSided(bool double_sided); | |
328 bool double_sided() const { return double_sided_; } | |
329 | |
330 void SetTransform(const gfx::Transform& transform); | |
331 const gfx::Transform& transform() const { return transform_; } | |
332 bool TransformIsAnimating() const; | |
333 bool TransformIsAnimatingOnImplOnly() const; | |
334 | |
335 void set_update_rect(const gfx::RectF& update_rect) { | |
336 update_rect_ = update_rect; | |
337 } | |
338 const gfx::RectF& update_rect() const { return update_rect_; } | |
339 | |
340 std::string LayerTreeAsText() const; | |
341 virtual base::DictionaryValue* LayerTreeAsJson() const; | |
342 | |
343 void SetStackingOrderChanged(bool stacking_order_changed); | |
344 | |
345 bool LayerPropertyChanged() const { | |
346 return layer_property_changed_ || LayerIsAlwaysDamaged(); | |
347 } | |
348 bool LayerSurfacePropertyChanged() const; | |
349 | |
350 void ResetAllChangeTrackingForSubtree(); | |
351 | |
352 virtual bool LayerIsAlwaysDamaged() const; | |
353 | |
354 LayerAnimationController* layer_animation_controller() { | |
355 return layer_animation_controller_.get(); | |
356 } | |
357 | |
358 virtual Region VisibleContentOpaqueRegion() const; | |
359 | |
360 virtual void DidBecomeActive(); | |
361 | |
362 // Indicates that the surface previously used to render this layer | |
363 // was lost and that a new one has been created. Won't be called | |
364 // until the new surface has been created successfully. | |
365 virtual void DidLoseOutputSurface(); | |
366 | |
367 ScrollbarAnimationController* scrollbar_animation_controller() const { | |
368 return scrollbar_animation_controller_.get(); | |
369 } | |
370 | |
371 void SetScrollbarOpacity(float opacity); | |
372 | |
373 void SetHorizontalScrollbarLayer(ScrollbarLayerImpl* scrollbar_layer); | |
374 ScrollbarLayerImpl* horizontal_scrollbar_layer() { | |
375 return horizontal_scrollbar_layer_; | |
376 } | |
377 | |
378 void SetVerticalScrollbarLayer(ScrollbarLayerImpl* scrollbar_layer); | |
379 ScrollbarLayerImpl* vertical_scrollbar_layer() { | |
380 return vertical_scrollbar_layer_; | |
381 } | |
382 | |
383 gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const; | |
384 | |
385 virtual skia::RefPtr<SkPicture> GetPicture(); | |
386 | |
387 virtual bool CanClipSelf() const; | |
388 | |
389 virtual bool AreVisibleResourcesReady() const; | |
390 | |
391 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl); | |
392 virtual void PushPropertiesTo(LayerImpl* layer); | |
393 | |
394 virtual scoped_ptr<base::Value> AsValue() const; | |
395 | |
396 protected: | |
397 LayerImpl(LayerTreeImpl* layer_impl, int id); | |
398 | |
399 // Get the color and size of the layer's debug border. | |
400 virtual void GetDebugBorderProperties(SkColor* color, float* width) const; | |
401 | |
402 void AppendDebugBorderQuad(QuadSink* quad_sink, | |
403 const SharedQuadState* shared_quad_state, | |
404 AppendQuadsData* append_quads_data) const; | |
405 | |
406 virtual void DumpLayerProperties(std::string* str, int indent) const; | |
407 static std::string IndentString(int indent); | |
408 | |
409 void AsValueInto(base::DictionaryValue* dict) const; | |
410 | |
411 void NoteLayerSurfacePropertyChanged(); | |
412 void NoteLayerPropertyChanged(); | |
413 void NoteLayerPropertyChangedForSubtree(); | |
414 | |
415 // Note carefully this does not affect the current layer. | |
416 void NoteLayerPropertyChangedForDescendants(); | |
417 | |
418 private: | |
419 void UpdateScrollbarPositions(); | |
420 | |
421 virtual const char* LayerTypeAsString() const; | |
422 | |
423 void DumpLayer(std::string* str, int indent) const; | |
424 | |
425 // Properties internal to LayerImpl | |
426 LayerImpl* parent_; | |
427 LayerList children_; | |
428 // mask_layer_ can be temporarily stolen during tree sync, we need this ID to | |
429 // confirm newly assigned layer is still the previous one | |
430 int mask_layer_id_; | |
431 scoped_ptr<LayerImpl> mask_layer_; | |
432 int replica_layer_id_; // ditto | |
433 scoped_ptr<LayerImpl> replica_layer_; | |
434 int layer_id_; | |
435 LayerTreeImpl* layer_tree_impl_; | |
436 | |
437 // Properties synchronized from the associated Layer. | |
438 gfx::PointF anchor_point_; | |
439 float anchor_point_z_; | |
440 gfx::Size bounds_; | |
441 gfx::Vector2d scroll_offset_; | |
442 bool scrollable_; | |
443 bool should_scroll_on_main_thread_; | |
444 bool have_wheel_event_handlers_; | |
445 Region non_fast_scrollable_region_; | |
446 Region touch_event_handler_region_; | |
447 SkColor background_color_; | |
448 bool stacking_order_changed_; | |
449 | |
450 // Whether the "back" of this layer should draw. | |
451 bool double_sided_; | |
452 | |
453 // Tracks if drawing-related properties have changed since last redraw. | |
454 bool layer_property_changed_; | |
455 | |
456 // Indicates that a property has changed on this layer that would not | |
457 // affect the pixels on its target surface, but would require redrawing | |
458 // the targetSurface onto its ancestor targetSurface. | |
459 // For layers that do not own a surface this flag acts as | |
460 // layer_property_changed_. | |
461 bool layer_surface_property_changed_; | |
462 | |
463 bool masks_to_bounds_; | |
464 bool contents_opaque_; | |
465 float opacity_; | |
466 gfx::PointF position_; | |
467 bool preserves_3d_; | |
468 bool use_parent_backface_visibility_; | |
469 bool draw_checkerboard_for_missing_tiles_; | |
470 gfx::Transform sublayer_transform_; | |
471 gfx::Transform transform_; | |
472 | |
473 bool draws_content_; | |
474 bool force_render_surface_; | |
475 | |
476 // Set for the layer that other layers are fixed to. | |
477 bool is_container_for_fixed_position_layers_; | |
478 // This is true if the layer should be fixed to the closest ancestor | |
479 // container. | |
480 bool fixed_to_container_layer_; | |
481 | |
482 gfx::Vector2dF scroll_delta_; | |
483 gfx::Vector2d sent_scroll_delta_; | |
484 gfx::Vector2d max_scroll_offset_; | |
485 gfx::Transform impl_transform_; | |
486 gfx::Vector2dF last_scroll_offset_; | |
487 | |
488 // The global depth value of the center of the layer. This value is used | |
489 // to sort layers from back to front. | |
490 float draw_depth_; | |
491 | |
492 // Debug layer name. | |
493 std::string debug_name_; | |
494 | |
495 WebKit::WebFilterOperations filters_; | |
496 WebKit::WebFilterOperations background_filters_; | |
497 skia::RefPtr<SkImageFilter> filter_; | |
498 | |
499 #ifndef NDEBUG | |
500 bool between_will_draw_and_did_draw_; | |
501 #endif | |
502 | |
503 // Rect indicating what was repainted/updated during update. | |
504 // Note that plugin layers bypass this and leave it empty. | |
505 // Uses layer's content space. | |
506 gfx::RectF update_rect_; | |
507 | |
508 // Manages animations for this layer. | |
509 scoped_refptr<LayerAnimationController> layer_animation_controller_; | |
510 | |
511 // Manages scrollbars for this layer | |
512 scoped_ptr<ScrollbarAnimationController> scrollbar_animation_controller_; | |
513 | |
514 // Weak pointers to this layer's scrollbars, if it has them. Updated during | |
515 // tree synchronization. | |
516 ScrollbarLayerImpl* horizontal_scrollbar_layer_; | |
517 ScrollbarLayerImpl* vertical_scrollbar_layer_; | |
518 | |
519 // Group of properties that need to be computed based on the layer tree | |
520 // hierarchy before layers can be drawn. | |
521 DrawProperties<LayerImpl, RenderSurfaceImpl> draw_properties_; | |
522 | |
523 DISALLOW_COPY_AND_ASSIGN(LayerImpl); | |
524 }; | |
525 | |
526 } | |
527 | |
528 #endif // CC_LAYER_IMPL_H_ | |
OLD | NEW |