forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_impl.cc
1599 lines (1335 loc) · 51.4 KB
/
layer_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/layers/layer_impl.h"
#include "base/debug/trace_event.h"
#include "base/debug/trace_event_argument.h"
#include "base/json/json_reader.h"
#include "base/strings/stringprintf.h"
#include "cc/animation/animation_registrar.h"
#include "cc/animation/scrollbar_animation_controller.h"
#include "cc/base/math_util.h"
#include "cc/base/simple_enclosed_region.h"
#include "cc/debug/debug_colors.h"
#include "cc/debug/layer_tree_debug_state.h"
#include "cc/debug/micro_benchmark_impl.h"
#include "cc/debug/traced_value.h"
#include "cc/input/layer_scroll_offset_delegate.h"
#include "cc/layers/layer_utils.h"
#include "cc/layers/painted_scrollbar_layer_impl.h"
#include "cc/output/copy_output_request.h"
#include "cc/quads/debug_border_draw_quad.h"
#include "cc/quads/render_pass.h"
#include "cc/trees/layer_tree_host_common.h"
#include "cc/trees/layer_tree_impl.h"
#include "cc/trees/layer_tree_settings.h"
#include "cc/trees/proxy.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
namespace cc {
LayerImpl::LayerImpl(LayerTreeImpl* tree_impl, int id)
: parent_(nullptr),
scroll_parent_(nullptr),
clip_parent_(nullptr),
mask_layer_id_(-1),
replica_layer_id_(-1),
layer_id_(id),
layer_tree_impl_(tree_impl),
scroll_offset_delegate_(nullptr),
scroll_clip_layer_(nullptr),
should_scroll_on_main_thread_(false),
have_wheel_event_handlers_(false),
have_scroll_event_handlers_(false),
user_scrollable_horizontal_(true),
user_scrollable_vertical_(true),
stacking_order_changed_(false),
double_sided_(true),
should_flatten_transform_(true),
layer_property_changed_(false),
masks_to_bounds_(false),
contents_opaque_(false),
is_root_for_isolated_group_(false),
use_parent_backface_visibility_(false),
draw_checkerboard_for_missing_tiles_(false),
draws_content_(false),
hide_layer_and_subtree_(false),
force_render_surface_(false),
transform_is_invertible_(true),
is_container_for_fixed_position_layers_(false),
background_color_(0),
opacity_(1.0),
blend_mode_(SkXfermode::kSrcOver_Mode),
num_descendants_that_draw_content_(0),
draw_depth_(0.f),
needs_push_properties_(false),
num_dependents_need_push_properties_(0),
sorting_context_id_(0),
current_draw_mode_(DRAW_MODE_NONE) {
DCHECK_GT(layer_id_, 0);
DCHECK(layer_tree_impl_);
layer_tree_impl_->RegisterLayer(this);
AnimationRegistrar* registrar = layer_tree_impl_->animationRegistrar();
layer_animation_controller_ =
registrar->GetAnimationControllerForId(layer_id_);
layer_animation_controller_->AddValueObserver(this);
if (IsActive()) {
layer_animation_controller_->set_value_provider(this);
layer_animation_controller_->set_layer_animation_delegate(this);
}
SetNeedsPushProperties();
}
LayerImpl::~LayerImpl() {
DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
layer_animation_controller_->RemoveValueObserver(this);
layer_animation_controller_->remove_value_provider(this);
layer_animation_controller_->remove_layer_animation_delegate(this);
if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
layer_tree_impl_->UnregisterLayer(this);
TRACE_EVENT_OBJECT_DELETED_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
}
void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
child->SetParent(this);
DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
children_.push_back(child.Pass());
layer_tree_impl()->set_needs_update_draw_properties();
}
scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
for (OwnedLayerImplList::iterator it = children_.begin();
it != children_.end();
++it) {
if (*it == child) {
scoped_ptr<LayerImpl> ret = children_.take(it);
children_.erase(it);
layer_tree_impl()->set_needs_update_draw_properties();
return ret.Pass();
}
}
return nullptr;
}
void LayerImpl::SetParent(LayerImpl* parent) {
if (parent_should_know_need_push_properties()) {
if (parent_)
parent_->RemoveDependentNeedsPushProperties();
if (parent)
parent->AddDependentNeedsPushProperties();
}
parent_ = parent;
}
void LayerImpl::ClearChildList() {
if (children_.empty())
return;
children_.clear();
layer_tree_impl()->set_needs_update_draw_properties();
}
bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
if (!ancestor)
return false;
for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
if (layer == ancestor)
return true;
}
return false;
}
void LayerImpl::SetScrollParent(LayerImpl* parent) {
if (scroll_parent_ == parent)
return;
// Having both a scroll parent and a scroll offset delegate is unsupported.
DCHECK(!scroll_offset_delegate_);
if (parent)
DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
scroll_parent_ = parent;
SetNeedsPushProperties();
}
void LayerImpl::SetDebugInfo(
scoped_refptr<base::debug::ConvertableToTraceFormat> other) {
debug_info_ = other;
SetNeedsPushProperties();
}
void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
if (scroll_children_.get() == children)
return;
scroll_children_.reset(children);
SetNeedsPushProperties();
}
void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
if (num_descendants_that_draw_content_ == num_descendants)
return;
num_descendants_that_draw_content_ = num_descendants;
SetNeedsPushProperties();
}
void LayerImpl::SetClipParent(LayerImpl* ancestor) {
if (clip_parent_ == ancestor)
return;
clip_parent_ = ancestor;
SetNeedsPushProperties();
}
void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
if (clip_children_.get() == children)
return;
clip_children_.reset(children);
SetNeedsPushProperties();
}
void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
if (requests->empty())
return;
bool was_empty = copy_requests_.empty();
copy_requests_.insert_and_take(copy_requests_.end(), requests);
requests->clear();
if (was_empty && layer_tree_impl()->IsActiveTree())
layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::TakeCopyRequestsAndTransformToTarget(
ScopedPtrVector<CopyOutputRequest>* requests) {
DCHECK(!copy_requests_.empty());
DCHECK(layer_tree_impl()->IsActiveTree());
size_t first_inserted_request = requests->size();
requests->insert_and_take(requests->end(), ©_requests_);
copy_requests_.clear();
for (size_t i = first_inserted_request; i < requests->size(); ++i) {
CopyOutputRequest* request = requests->at(i);
if (!request->has_area())
continue;
gfx::Rect request_in_layer_space = request->area();
gfx::Rect request_in_content_space =
LayerRectToContentRect(request_in_layer_space);
request->set_area(MathUtil::MapEnclosingClippedRect(
draw_properties_.target_space_transform, request_in_content_space));
}
layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
}
void LayerImpl::CreateRenderSurface() {
DCHECK(!draw_properties_.render_surface);
draw_properties_.render_surface =
make_scoped_ptr(new RenderSurfaceImpl(this));
draw_properties_.render_target = this;
}
void LayerImpl::ClearRenderSurface() {
draw_properties_.render_surface = nullptr;
}
void LayerImpl::ClearRenderSurfaceLayerList() {
if (draw_properties_.render_surface)
draw_properties_.render_surface->layer_list().clear();
}
void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
state->SetAll(
draw_properties_.target_space_transform, draw_properties_.content_bounds,
draw_properties_.visible_content_rect, draw_properties_.clip_rect,
draw_properties_.is_clipped, draw_properties_.opacity,
draw_properties_.blend_mode, sorting_context_id_);
}
bool LayerImpl::WillDraw(DrawMode draw_mode,
ResourceProvider* resource_provider) {
// WillDraw/DidDraw must be matched.
DCHECK_NE(DRAW_MODE_NONE, draw_mode);
DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
current_draw_mode_ = draw_mode;
return true;
}
void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
current_draw_mode_ = DRAW_MODE_NONE;
}
bool LayerImpl::ShowDebugBorders() const {
return layer_tree_impl()->debug_state().show_debug_borders;
}
void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
if (draws_content_) {
*color = DebugColors::ContentLayerBorderColor();
*width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
return;
}
if (masks_to_bounds_) {
*color = DebugColors::MaskingLayerBorderColor();
*width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
return;
}
*color = DebugColors::ContainerLayerBorderColor();
*width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
}
void LayerImpl::AppendDebugBorderQuad(
RenderPass* render_pass,
const gfx::Size& content_bounds,
const SharedQuadState* shared_quad_state,
AppendQuadsData* append_quads_data) const {
SkColor color;
float width;
GetDebugBorderProperties(&color, &width);
AppendDebugBorderQuad(render_pass,
content_bounds,
shared_quad_state,
append_quads_data,
color,
width);
}
void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
const gfx::Size& content_bounds,
const SharedQuadState* shared_quad_state,
AppendQuadsData* append_quads_data,
SkColor color,
float width) const {
if (!ShowDebugBorders())
return;
gfx::Rect quad_rect(content_bounds);
gfx::Rect visible_quad_rect(quad_rect);
DebugBorderDrawQuad* debug_border_quad =
render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
debug_border_quad->SetNew(
shared_quad_state, quad_rect, visible_quad_rect, color, width);
}
bool LayerImpl::HasDelegatedContent() const {
return false;
}
bool LayerImpl::HasContributingDelegatedRenderPasses() const {
return false;
}
RenderPassId LayerImpl::FirstContributingRenderPassId() const {
return RenderPassId(0, 0);
}
RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
return RenderPassId(0, 0);
}
void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
gfx::Size* resource_size) const {
NOTREACHED();
*resource_id = 0;
}
void LayerImpl::SetSentScrollDelta(const gfx::Vector2dF& sent_scroll_delta) {
// Pending tree never has sent scroll deltas
DCHECK(layer_tree_impl()->IsActiveTree());
if (sent_scroll_delta_ == sent_scroll_delta)
return;
sent_scroll_delta_ = sent_scroll_delta;
}
gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
gfx::Vector2dF adjusted_scroll = scroll;
if (layer_tree_impl()->settings().use_pinch_virtual_viewport) {
if (!user_scrollable_horizontal_)
adjusted_scroll.set_x(0);
if (!user_scrollable_vertical_)
adjusted_scroll.set_y(0);
}
DCHECK(scrollable());
gfx::Vector2dF min_delta = -ScrollOffsetToVector2dF(scroll_offset_);
gfx::Vector2dF max_delta = MaxScrollOffset().DeltaFrom(scroll_offset_);
// Clamp new_delta so that position + delta stays within scroll bounds.
gfx::Vector2dF new_delta = (ScrollDelta() + adjusted_scroll);
new_delta.SetToMax(min_delta);
new_delta.SetToMin(max_delta);
gfx::Vector2dF unscrolled =
ScrollDelta() + scroll - new_delta;
SetScrollDelta(new_delta);
return unscrolled;
}
void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
}
bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
: user_scrollable_vertical_;
}
void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
if (sent_scroll_delta_.IsZero())
return;
// Pending tree never has sent scroll deltas
DCHECK(layer_tree_impl()->IsActiveTree());
// The combination of pending tree and aborted commits with impl scrolls
// shouldn't happen; we don't know how to update its deltas correctly.
DCHECK(!layer_tree_impl()->FindPendingTreeLayerById(id()));
// Apply sent scroll deltas to scroll position / scroll delta as if the
// main thread had applied them and then committed those values.
SetScrollOffsetAndDelta(
scroll_offset_ + gfx::ScrollOffset(sent_scroll_delta_),
ScrollDelta() - sent_scroll_delta_);
SetSentScrollDelta(gfx::Vector2dF());
}
void LayerImpl::ApplyScrollDeltasSinceBeginMainFrame() {
// Only the pending tree can have missing scrolls.
DCHECK(layer_tree_impl()->IsPendingTree());
if (!scrollable())
return;
// Pending tree should never have sent scroll deltas.
DCHECK(sent_scroll_delta().IsZero());
LayerImpl* active_twin = layer_tree_impl()->FindActiveTreeLayerById(id());
if (active_twin) {
// Scrolls that happens after begin frame (where the sent scroll delta
// comes from) and commit need to be applied to the pending tree
// so that it is up to date with the total scroll.
SetScrollDelta(active_twin->ScrollDelta() -
active_twin->sent_scroll_delta());
}
}
InputHandler::ScrollStatus LayerImpl::TryScroll(
const gfx::PointF& screen_space_point,
InputHandler::ScrollInputType type) const {
if (should_scroll_on_main_thread()) {
TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
return InputHandler::ScrollOnMainThread;
}
if (!screen_space_transform().IsInvertible()) {
TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
return InputHandler::ScrollIgnored;
}
if (!non_fast_scrollable_region().IsEmpty()) {
bool clipped = false;
gfx::Transform inverse_screen_space_transform(
gfx::Transform::kSkipInitialization);
if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
// TODO(shawnsingh): We shouldn't be applying a projection if screen space
// transform is uninvertible here. Perhaps we should be returning
// ScrollOnMainThread in this case?
}
gfx::PointF hit_test_point_in_content_space =
MathUtil::ProjectPoint(inverse_screen_space_transform,
screen_space_point,
&clipped);
gfx::PointF hit_test_point_in_layer_space =
gfx::ScalePoint(hit_test_point_in_content_space,
1.f / contents_scale_x(),
1.f / contents_scale_y());
if (!clipped &&
non_fast_scrollable_region().Contains(
gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
TRACE_EVENT0("cc",
"LayerImpl::tryScroll: Failed NonFastScrollableRegion");
return InputHandler::ScrollOnMainThread;
}
}
if (type == InputHandler::Wheel && have_wheel_event_handlers()) {
TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
return InputHandler::ScrollOnMainThread;
}
if (!scrollable()) {
TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
return InputHandler::ScrollIgnored;
}
gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
TRACE_EVENT0("cc",
"LayerImpl::tryScroll: Ignored. Technically scrollable,"
" but has no affordance in either direction.");
return InputHandler::ScrollIgnored;
}
return InputHandler::ScrollStarted;
}
gfx::Rect LayerImpl::LayerRectToContentRect(
const gfx::RectF& layer_rect) const {
gfx::RectF content_rect =
gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
// Intersect with content rect to avoid the extra pixel because for some
// values x and y, ceil((x / y) * y) may be x + 1.
content_rect.Intersect(gfx::Rect(content_bounds()));
return gfx::ToEnclosingRect(content_rect);
}
skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
return skia::RefPtr<SkPicture>();
}
scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
return LayerImpl::Create(tree_impl, layer_id_);
}
void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
layer->SetTransformOrigin(transform_origin_);
layer->SetBackgroundColor(background_color_);
layer->SetBounds(bounds_);
layer->SetContentBounds(content_bounds());
layer->SetContentsScale(contents_scale_x(), contents_scale_y());
layer->SetDoubleSided(double_sided_);
layer->SetDrawCheckerboardForMissingTiles(
draw_checkerboard_for_missing_tiles_);
layer->SetForceRenderSurface(force_render_surface_);
layer->SetDrawsContent(DrawsContent());
layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
layer->SetFilters(filters());
layer->SetBackgroundFilters(background_filters());
layer->SetMasksToBounds(masks_to_bounds_);
layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
layer->SetContentsOpaque(contents_opaque_);
layer->SetOpacity(opacity_);
layer->SetBlendMode(blend_mode_);
layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
layer->SetPosition(position_);
layer->SetIsContainerForFixedPositionLayers(
is_container_for_fixed_position_layers_);
layer->SetPositionConstraint(position_constraint_);
layer->SetShouldFlattenTransform(should_flatten_transform_);
layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
: Layer::INVALID_ID);
layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
layer->set_user_scrollable_vertical(user_scrollable_vertical_);
// Save the difference but clear the sent delta so that we don't subtract
// it again in SetScrollOffsetAndDelta's pending twin mirroring logic.
gfx::Vector2dF remaining_delta =
layer->ScrollDelta() - layer->sent_scroll_delta();
layer->SetSentScrollDelta(gfx::Vector2dF());
layer->SetScrollOffsetAndDelta(scroll_offset_, remaining_delta);
layer->Set3dSortingContextId(sorting_context_id_);
layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
LayerImpl* scroll_parent = nullptr;
if (scroll_parent_) {
scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
DCHECK(scroll_parent);
}
layer->SetScrollParent(scroll_parent);
if (scroll_children_) {
std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
it != scroll_children_->end();
++it) {
DCHECK_EQ((*it)->scroll_parent(), this);
LayerImpl* scroll_child =
layer->layer_tree_impl()->LayerById((*it)->id());
DCHECK(scroll_child);
scroll_children->insert(scroll_child);
}
layer->SetScrollChildren(scroll_children);
} else {
layer->SetScrollChildren(nullptr);
}
LayerImpl* clip_parent = nullptr;
if (clip_parent_) {
clip_parent = layer->layer_tree_impl()->LayerById(
clip_parent_->id());
DCHECK(clip_parent);
}
layer->SetClipParent(clip_parent);
if (clip_children_) {
std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
it != clip_children_->end(); ++it)
clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
layer->SetClipChildren(clip_children);
} else {
layer->SetClipChildren(nullptr);
}
layer->PassCopyRequests(©_requests_);
// If the main thread commits multiple times before the impl thread actually
// draws, then damage tracking will become incorrect if we simply clobber the
// update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
// union) any update changes that have occurred on the main thread.
update_rect_.Union(layer->update_rect());
layer->SetUpdateRect(update_rect_);
layer->SetStackingOrderChanged(stacking_order_changed_);
layer->SetDebugInfo(debug_info_);
// Reset any state that should be cleared for the next update.
stacking_order_changed_ = false;
update_rect_ = gfx::Rect();
needs_push_properties_ = false;
num_dependents_need_push_properties_ = 0;
}
gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
if (!scroll_clip_layer_)
return gfx::Vector2dF();
gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
// In virtual-viewport mode, we don't need to compensate for pinch zoom or
// scale since the fixed container is the outer viewport, which sits below
// the page scale.
if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
return delta_from_scroll;
float scale_delta = layer_tree_impl()->page_scale_delta();
float scale = layer_tree_impl()->current_page_scale_factor() /
layer_tree_impl()->page_scale_delta();
delta_from_scroll.Scale(1.f / scale);
// The delta-from-pinch component requires some explanation: A viewport of
// size (w,h) will appear to be size (w/s,h/s) under scale s in the content
// space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
// viewport size change in the content space due to ds is:
//
// (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
//
gfx::Vector2dF delta_from_pinch =
gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
return delta_from_scroll + delta_from_pinch;
}
base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
base::DictionaryValue* result = new base::DictionaryValue;
result->SetString("LayerType", LayerTypeAsString());
base::ListValue* list = new base::ListValue;
list->AppendInteger(bounds().width());
list->AppendInteger(bounds().height());
result->Set("Bounds", list);
list = new base::ListValue;
list->AppendDouble(position_.x());
list->AppendDouble(position_.y());
result->Set("Position", list);
const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
double transform[16];
gfx_transform.matrix().asColMajord(transform);
list = new base::ListValue;
for (int i = 0; i < 16; ++i)
list->AppendDouble(transform[i]);
result->Set("DrawTransform", list);
result->SetBoolean("DrawsContent", draws_content_);
result->SetBoolean("Is3dSorted", Is3dSorted());
result->SetDouble("Opacity", opacity());
result->SetBoolean("ContentsOpaque", contents_opaque_);
if (scrollable())
result->SetBoolean("Scrollable", true);
if (have_wheel_event_handlers_)
result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
if (have_scroll_event_handlers_)
result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
if (!touch_event_handler_region_.IsEmpty()) {
scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
result->Set("TouchRegion", region.release());
}
list = new base::ListValue;
for (size_t i = 0; i < children_.size(); ++i)
list->Append(children_[i]->LayerTreeAsJson());
result->Set("Children", list);
return result;
}
void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
if (stacking_order_changed) {
stacking_order_changed_ = true;
NoteLayerPropertyChangedForSubtree();
}
}
void LayerImpl::NoteLayerPropertyChanged() {
layer_property_changed_ = true;
layer_tree_impl()->set_needs_update_draw_properties();
SetNeedsPushProperties();
}
void LayerImpl::NoteLayerPropertyChangedForSubtree() {
layer_property_changed_ = true;
layer_tree_impl()->set_needs_update_draw_properties();
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
SetNeedsPushProperties();
}
void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
layer_property_changed_ = true;
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
}
void LayerImpl::NoteLayerPropertyChangedForDescendants() {
layer_tree_impl()->set_needs_update_draw_properties();
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
SetNeedsPushProperties();
}
const char* LayerImpl::LayerTypeAsString() const {
return "cc::LayerImpl";
}
void LayerImpl::ResetAllChangeTrackingForSubtree() {
layer_property_changed_ = false;
update_rect_ = gfx::Rect();
damage_rect_ = gfx::RectF();
if (draw_properties_.render_surface)
draw_properties_.render_surface->ResetPropertyChangedFlag();
if (mask_layer_)
mask_layer_->ResetAllChangeTrackingForSubtree();
if (replica_layer_) {
// This also resets the replica mask, if it exists.
replica_layer_->ResetAllChangeTrackingForSubtree();
}
for (size_t i = 0; i < children_.size(); ++i)
children_[i]->ResetAllChangeTrackingForSubtree();
needs_push_properties_ = false;
num_dependents_need_push_properties_ = 0;
}
gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
return TotalScrollOffset();
}
void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
SetFilters(filters);
}
void LayerImpl::OnOpacityAnimated(float opacity) {
SetOpacity(opacity);
}
void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
SetTransform(transform);
}
void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
// Only layers in the active tree should need to do anything here, since
// layers in the pending tree will find out about these changes as a
// result of the call to SetScrollDelta.
if (!IsActive())
return;
SetScrollDelta(scroll_offset.DeltaFrom(scroll_offset_));
layer_tree_impl_->DidAnimateScrollOffset();
}
void LayerImpl::OnAnimationWaitingForDeletion() {}
bool LayerImpl::IsActive() const {
return layer_tree_impl_->IsActiveTree();
}
gfx::Size LayerImpl::bounds() const {
gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
return gfx::Size(bounds_.width() + delta.x(),
bounds_.height() + delta.y());
}
gfx::SizeF LayerImpl::BoundsForScrolling() const {
return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
bounds_.height() + bounds_delta_.y());
}
void LayerImpl::SetBounds(const gfx::Size& bounds) {
if (bounds_ == bounds)
return;
bounds_ = bounds;
ScrollbarParametersDidChange(true);
if (masks_to_bounds())
NoteLayerPropertyChangedForSubtree();
else
NoteLayerPropertyChanged();
}
void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
if (bounds_delta_ == bounds_delta)
return;
bounds_delta_ = bounds_delta;
ScrollbarParametersDidChange(true);
if (masks_to_bounds())
NoteLayerPropertyChangedForSubtree();
else
NoteLayerPropertyChanged();
}
void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
int new_layer_id = mask_layer ? mask_layer->id() : -1;
if (mask_layer) {
DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
DCHECK_NE(new_layer_id, mask_layer_id_);
} else if (new_layer_id == mask_layer_id_) {
return;
}
mask_layer_ = mask_layer.Pass();
mask_layer_id_ = new_layer_id;
if (mask_layer_)
mask_layer_->SetParent(this);
NoteLayerPropertyChangedForSubtree();
}
scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
mask_layer_id_ = -1;
return mask_layer_.Pass();
}
void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
int new_layer_id = replica_layer ? replica_layer->id() : -1;
if (replica_layer) {
DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
DCHECK_NE(new_layer_id, replica_layer_id_);
} else if (new_layer_id == replica_layer_id_) {
return;
}
replica_layer_ = replica_layer.Pass();
replica_layer_id_ = new_layer_id;
if (replica_layer_)
replica_layer_->SetParent(this);
NoteLayerPropertyChangedForSubtree();
}
scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
replica_layer_id_ = -1;
return replica_layer_.Pass();
}
ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
return nullptr;
}
void LayerImpl::SetDrawsContent(bool draws_content) {
if (draws_content_ == draws_content)
return;
draws_content_ = draws_content;
NoteLayerPropertyChanged();
}
void LayerImpl::SetHideLayerAndSubtree(bool hide) {
if (hide_layer_and_subtree_ == hide)
return;
hide_layer_and_subtree_ = hide;
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
if (transform_origin_ == transform_origin)
return;
transform_origin_ = transform_origin;
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::SetBackgroundColor(SkColor background_color) {
if (background_color_ == background_color)
return;
background_color_ = background_color;
NoteLayerPropertyChanged();
}
SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
SkColor color = background_color();
if (SkColorGetA(color) == 255 && !contents_opaque()) {
color = SK_ColorTRANSPARENT;
} else if (SkColorGetA(color) != 255 && contents_opaque()) {
for (const LayerImpl* layer = parent(); layer;
layer = layer->parent()) {
color = layer->background_color();
if (SkColorGetA(color) == 255)
break;
}
if (SkColorGetA(color) != 255)
color = layer_tree_impl()->background_color();
if (SkColorGetA(color) != 255)
color = SkColorSetA(color, 255);
}
return color;
}
void LayerImpl::SetFilters(const FilterOperations& filters) {
if (filters_ == filters)
return;
filters_ = filters;
NoteLayerPropertyChangedForSubtree();
}
bool LayerImpl::FilterIsAnimating() const {
return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
}
bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
Animation* filter_animation =
layer_animation_controller_->GetAnimation(Animation::Filter);
return filter_animation && filter_animation->is_impl_only();
}
void LayerImpl::SetBackgroundFilters(
const FilterOperations& filters) {
if (background_filters_ == filters)
return;
background_filters_ = filters;
NoteLayerPropertyChanged();
}
void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
if (masks_to_bounds_ == masks_to_bounds)
return;
masks_to_bounds_ = masks_to_bounds;
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::SetContentsOpaque(bool opaque) {
if (contents_opaque_ == opaque)
return;
contents_opaque_ = opaque;
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::SetOpacity(float opacity) {
if (opacity_ == opacity)
return;
opacity_ = opacity;
NoteLayerPropertyChangedForSubtree();
}
bool LayerImpl::OpacityIsAnimating() const {
return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
}
bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
Animation* opacity_animation =
layer_animation_controller_->GetAnimation(Animation::Opacity);
return opacity_animation && opacity_animation->is_impl_only();
}
void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
if (blend_mode_ == blend_mode)
return;
blend_mode_ = blend_mode;
NoteLayerPropertyChangedForSubtree();
}
void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
if (is_root_for_isolated_group_ == root)