OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. | 5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. |
6 | 6 |
7 #include "ui/views/view.h" | 7 #include "ui/views/view.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
838 | 838 |
839 if (layer()) { | 839 if (layer()) { |
840 layer()->SchedulePaint(rect); | 840 layer()->SchedulePaint(rect); |
841 } else if (parent_) { | 841 } else if (parent_) { |
842 // Translate the requested paint rect to the parent's coordinate system | 842 // Translate the requested paint rect to the parent's coordinate system |
843 // then pass this notification up to the parent. | 843 // then pass this notification up to the parent. |
844 parent_->SchedulePaintInRect(ConvertRectToParent(rect)); | 844 parent_->SchedulePaintInRect(ConvertRectToParent(rect)); |
845 } | 845 } |
846 } | 846 } |
847 | 847 |
848 void View::Paint(const ui::PaintContext& parent_context) { | 848 void View::Paint(const PaintInfo& parent_paint_info) { |
849 if (!ShouldPaint()) | 849 if (!ShouldPaint()) |
850 return; | 850 return; |
851 | 851 |
852 ui::PaintContext context(parent_context, GetPaintContextOffset()); | 852 const gfx::Rect& parent_bounds = !parent() |
853 ? GetPaintRecordingBounds() | |
854 : parent()->GetPaintRecordingBounds(); | |
853 | 855 |
856 PaintInfo paint_info(parent_paint_info, GetPaintRecordingBounds(), | |
857 parent_bounds, GetPaintScaleType()); | |
858 | |
859 const ui::PaintContext& context = paint_info.context(); | |
854 bool is_invalidated = true; | 860 bool is_invalidated = true; |
855 if (context.CanCheckInvalid()) { | 861 if (paint_info.context().CanCheckInvalid()) { |
856 #if DCHECK_IS_ON() | 862 #if DCHECK_IS_ON() |
857 gfx::Vector2d offset; | 863 if (!context.is_pixel_canvas()) { |
sky
2017/07/26 22:12:12
Why is this only done for !pixel_canvas?
malaykeshav
2017/07/28 01:24:38
With pixel canvas, the context.PaintOffset() retur
| |
858 context.Visited(this); | 864 gfx::Vector2d offset; |
859 View* view = this; | 865 context.Visited(this); |
860 while (view->parent() && !view->layer()) { | 866 View* view = this; |
861 DCHECK(view->GetTransform().IsIdentity()); | 867 while (view->parent() && !view->layer()) { |
862 offset += view->GetMirroredPosition().OffsetFromOrigin(); | 868 DCHECK(view->GetTransform().IsIdentity()); |
863 view = view->parent(); | 869 offset += view->GetMirroredPosition().OffsetFromOrigin(); |
870 view = view->parent(); | |
871 } | |
872 // The offset in the PaintContext should be the offset up to the paint | |
873 // root, which we compute and verify here. | |
874 DCHECK_EQ(context.PaintOffset().x(), offset.x()); | |
875 DCHECK_EQ(context.PaintOffset().y(), offset.y()); | |
876 // The above loop will stop when |view| is the paint root, which should be | |
877 // the root of the current paint walk, as verified by storing the root in | |
878 // the PaintContext. | |
879 DCHECK_EQ(context.RootVisited(), view); | |
864 } | 880 } |
865 // The offset in the PaintContext should be the offset up to the paint root, | |
866 // which we compute and verify here. | |
867 DCHECK_EQ(context.PaintOffset().x(), offset.x()); | |
868 DCHECK_EQ(context.PaintOffset().y(), offset.y()); | |
869 // The above loop will stop when |view| is the paint root, which should be | |
870 // the root of the current paint walk, as verified by storing the root in | |
871 // the PaintContext. | |
872 DCHECK_EQ(context.RootVisited(), view); | |
873 #endif | 881 #endif |
874 | 882 |
875 // If the View wasn't invalidated, don't waste time painting it, the output | 883 // If the View wasn't invalidated, don't waste time painting it, the output |
876 // would be culled. | 884 // would be culled. |
877 is_invalidated = context.IsRectInvalid(GetLocalBounds()); | 885 is_invalidated = |
886 context.IsRectInvalid(gfx::Rect(paint_info.paint_recording_size())); | |
878 } | 887 } |
879 | 888 |
880 TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); | 889 TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); |
881 | 890 |
882 // If the view is backed by a layer, it should paint with itself as the origin | 891 // If the view is backed by a layer, it should paint with itself as the origin |
883 // rather than relative to its parent. | 892 // rather than relative to its parent. |
884 // TODO(danakj): Rework clip and transform recorder usage here to use | 893 // TODO(danakj): Rework clip and transform recorder usage here to use |
885 // std::optional once we can do so. | 894 // std::optional once we can do so. |
886 ui::ClipRecorder clip_recorder(parent_context); | 895 ui::ClipRecorder clip_recorder(parent_paint_info.context()); |
887 if (!layer()) { | 896 if (!layer()) { |
888 // Set the clip rect to the bounds of this View, or |clip_path_| if it's | 897 // Set the clip rect to the bounds of this View, or |clip_path_| if it's |
889 // been set. Note that the X (or left) position we pass to ClipRect takes | 898 // been set. Note that the X (or left) position we pass to ClipRect takes |
890 // into consideration whether or not the View uses a right-to-left layout so | 899 // into consideration whether or not the View uses a right-to-left layout so |
891 // that we paint the View in its mirrored position if need be. | 900 // that we paint the View in its mirrored position if need be. |
892 if (clip_path_.isEmpty()) { | 901 if (clip_path_.isEmpty()) { |
893 clip_recorder.ClipRect(GetMirroredBounds()); | 902 clip_recorder.ClipRect(gfx::Rect(paint_info.paint_recording_size()) + |
903 paint_info.offset_from_parent()); | |
894 } else { | 904 } else { |
895 gfx::Path clip_path_in_parent = clip_path_; | 905 gfx::Path clip_path_in_parent = clip_path_; |
896 clip_path_in_parent.offset(GetMirroredX(), y()); | 906 |
907 // Transform |clip_path_| from local space to parent recording space. | |
908 gfx::Transform to_parent_recording_space; | |
909 | |
910 gfx::PointF recording_scale = paint_info.paint_recording_scale(); | |
911 to_parent_recording_space.Scale(SkFloatToScalar(recording_scale.x()), | |
912 SkFloatToScalar(recording_scale.y())); | |
913 to_parent_recording_space.Translate(paint_info.offset_from_parent()); | |
914 | |
915 clip_path_in_parent.transform(to_parent_recording_space.matrix()); | |
897 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent); | 916 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent); |
898 } | 917 } |
899 } | 918 } |
900 | 919 |
901 ui::TransformRecorder transform_recorder(context); | 920 ui::TransformRecorder transform_recorder(context); |
902 SetupTransformRecorderForPainting(&transform_recorder); | 921 SetupTransformRecorderForPainting(&transform_recorder, |
922 paint_info.offset_from_parent()); | |
903 | 923 |
904 // Note that the cache is not aware of the offset of the view | 924 // Note that the cache is not aware of the offset of the view |
905 // relative to the parent since painting is always done relative to | 925 // relative to the parent since painting is always done relative to |
906 // the top left of the individual view. | 926 // the top left of the individual view. |
907 if (is_invalidated || !paint_cache_.UseCache(context, size())) { | 927 if (is_invalidated || |
908 ui::PaintRecorder recorder(context, size(), &paint_cache_); | 928 !paint_cache_.UseCache(context, paint_info.paint_recording_size())) { |
929 ui::PaintRecorder recorder(context, paint_info.paint_recording_size(), | |
930 paint_info.paint_recording_scale(), | |
931 &paint_cache_); | |
909 gfx::Canvas* canvas = recorder.canvas(); | 932 gfx::Canvas* canvas = recorder.canvas(); |
910 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), | 933 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), |
911 flip_canvas_on_paint_for_rtl_ui_); | 934 flip_canvas_on_paint_for_rtl_ui_); |
912 | 935 |
913 // Delegate painting the contents of the View to the virtual OnPaint method. | 936 // Delegate painting the contents of the View to the virtual OnPaint method. |
914 OnPaint(canvas); | 937 OnPaint(canvas); |
915 } | 938 } |
916 | 939 |
917 // View::Paint() recursion over the subtree. | 940 // View::Paint() recursion over the subtree. |
918 PaintChildren(context); | 941 PaintChildren(paint_info); |
919 } | 942 } |
920 | 943 |
921 void View::SetBackground(std::unique_ptr<Background> b) { | 944 void View::SetBackground(std::unique_ptr<Background> b) { |
922 background_ = std::move(b); | 945 background_ = std::move(b); |
923 } | 946 } |
924 | 947 |
925 void View::SetBorder(std::unique_ptr<Border> b) { | 948 void View::SetBorder(std::unique_ptr<Border> b) { |
926 border_ = std::move(b); | 949 border_ = std::move(b); |
927 } | 950 } |
928 | 951 |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1536 RegisterPendingAccelerators(); | 1559 RegisterPendingAccelerators(); |
1537 } | 1560 } |
1538 } | 1561 } |
1539 | 1562 |
1540 void View::AddedToWidget() {} | 1563 void View::AddedToWidget() {} |
1541 | 1564 |
1542 void View::RemovedFromWidget() {} | 1565 void View::RemovedFromWidget() {} |
1543 | 1566 |
1544 // Painting -------------------------------------------------------------------- | 1567 // Painting -------------------------------------------------------------------- |
1545 | 1568 |
1546 void View::PaintChildren(const ui::PaintContext& context) { | 1569 void View::PaintChildren(const PaintInfo& paint_info) { |
1547 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); | 1570 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); |
1548 RecursivePaintHelper(&View::Paint, context); | 1571 RecursivePaintHelper(&View::Paint, paint_info); |
1549 } | 1572 } |
1550 | 1573 |
1551 void View::OnPaint(gfx::Canvas* canvas) { | 1574 void View::OnPaint(gfx::Canvas* canvas) { |
1552 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); | 1575 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); |
1553 OnPaintBackground(canvas); | 1576 OnPaintBackground(canvas); |
1554 OnPaintBorder(canvas); | 1577 OnPaintBorder(canvas); |
1555 } | 1578 } |
1556 | 1579 |
1557 void View::OnPaintBackground(gfx::Canvas* canvas) { | 1580 void View::OnPaintBackground(gfx::Canvas* canvas) { |
1558 if (background_) { | 1581 if (background_) { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1810 // and for different display density. | 1833 // and for different display density. |
1811 return kDefaultHorizontalDragThreshold; | 1834 return kDefaultHorizontalDragThreshold; |
1812 } | 1835 } |
1813 | 1836 |
1814 int View::GetVerticalDragThreshold() { | 1837 int View::GetVerticalDragThreshold() { |
1815 // TODO(jennyz): This value may need to be adjusted for different platforms | 1838 // TODO(jennyz): This value may need to be adjusted for different platforms |
1816 // and for different display density. | 1839 // and for different display density. |
1817 return kDefaultVerticalDragThreshold; | 1840 return kDefaultVerticalDragThreshold; |
1818 } | 1841 } |
1819 | 1842 |
1843 PaintInfo::ScaleType View::GetPaintScaleType() const { | |
1844 return PaintInfo::ScaleType::kScaleToFit; | |
1845 } | |
1846 | |
1820 // Debugging ------------------------------------------------------------------- | 1847 // Debugging ------------------------------------------------------------------- |
1821 | 1848 |
1822 #if !defined(NDEBUG) | 1849 #if !defined(NDEBUG) |
1823 | 1850 |
1824 std::string View::PrintViewGraph(bool first) { | 1851 std::string View::PrintViewGraph(bool first) { |
1825 return DoPrintViewGraph(first, this); | 1852 return DoPrintViewGraph(first, this); |
1826 } | 1853 } |
1827 | 1854 |
1828 std::string View::DoPrintViewGraph(bool first, View* view_with_children) { | 1855 std::string View::DoPrintViewGraph(bool first, View* view_with_children) { |
1829 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. | 1856 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1962 // Translate the requested paint rect to the parent's coordinate system | 1989 // Translate the requested paint rect to the parent's coordinate system |
1963 // then pass this notification up to the parent. | 1990 // then pass this notification up to the parent. |
1964 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); | 1991 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); |
1965 } | 1992 } |
1966 } | 1993 } |
1967 | 1994 |
1968 bool View::ShouldPaint() const { | 1995 bool View::ShouldPaint() const { |
1969 return visible_ && !size().IsEmpty(); | 1996 return visible_ && !size().IsEmpty(); |
1970 } | 1997 } |
1971 | 1998 |
1972 gfx::Vector2d View::GetPaintContextOffset() const { | 1999 gfx::Rect View::GetPaintRecordingBounds() const { |
1973 // If the View has a layer() then it is a paint root. Otherwise, we need to | 2000 // If the View has a layer() then it is a paint root and no offset information |
1974 // add the offset from the parent into the total offset from the paint root. | 2001 // is needed. Otherwise, we need bounds that includes an offset from the |
2002 // parent to add to the total offset from the paint root. | |
1975 DCHECK(layer() || parent() || origin() == gfx::Point()); | 2003 DCHECK(layer() || parent() || origin() == gfx::Point()); |
1976 return layer() ? gfx::Vector2d() : GetMirroredPosition().OffsetFromOrigin(); | 2004 return layer() ? GetLocalBounds() : GetMirroredBounds(); |
1977 } | 2005 } |
1978 | 2006 |
1979 void View::SetupTransformRecorderForPainting( | 2007 void View::SetupTransformRecorderForPainting( |
1980 ui::TransformRecorder* recorder) const { | 2008 ui::TransformRecorder* recorder, |
2009 const gfx::Vector2d& offset_from_parent) const { | |
1981 // If the view is backed by a layer, it should paint with itself as the origin | 2010 // If the view is backed by a layer, it should paint with itself as the origin |
1982 // rather than relative to its parent. | 2011 // rather than relative to its parent. |
1983 if (layer()) | 2012 if (layer()) |
1984 return; | 2013 return; |
1985 | 2014 |
1986 // Translate the graphics such that 0,0 corresponds to where this View is | 2015 // Translate the graphics such that 0,0 corresponds to where this View is |
1987 // located relative to its parent. | 2016 // located relative to its parent. |
1988 gfx::Transform transform_from_parent; | 2017 gfx::Transform transform_from_parent; |
1989 gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin(); | |
1990 transform_from_parent.Translate(offset_from_parent.x(), | 2018 transform_from_parent.Translate(offset_from_parent.x(), |
1991 offset_from_parent.y()); | 2019 offset_from_parent.y()); |
1992 transform_from_parent.PreconcatTransform(GetTransform()); | |
1993 recorder->Transform(transform_from_parent); | 2020 recorder->Transform(transform_from_parent); |
1994 } | 2021 } |
1995 | 2022 |
1996 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&), | 2023 void View::RecursivePaintHelper(void (View::*func)(const PaintInfo&), |
1997 const ui::PaintContext& context) { | 2024 const PaintInfo& info) { |
1998 View::Views children = GetChildrenInZOrder(); | 2025 View::Views children = GetChildrenInZOrder(); |
1999 DCHECK_EQ(child_count(), static_cast<int>(children.size())); | 2026 DCHECK_EQ(child_count(), static_cast<int>(children.size())); |
2000 for (auto* child : children) { | 2027 for (auto* child : children) { |
2001 if (!child->layer()) | 2028 if (!child->layer()) |
2002 (child->*func)(context); | 2029 (child->*func)(info); |
2003 } | 2030 } |
2004 } | 2031 } |
2005 | 2032 |
2006 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { | 2033 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { |
2007 Paint(parent_context); | 2034 PaintInfo paint_info(parent_context, layer() ? layer()->size() : size()); |
2035 Paint(paint_info); | |
2008 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 2036 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
2009 switches::kDrawViewBoundsRects)) | 2037 switches::kDrawViewBoundsRects)) |
2010 PaintDebugRects(parent_context); | 2038 PaintDebugRects(paint_info); |
2011 } | 2039 } |
2012 | 2040 |
2013 void View::PaintDebugRects(const ui::PaintContext& parent_context) { | 2041 void View::PaintDebugRects(const PaintInfo& parent_paint_info) { |
2014 if (!ShouldPaint()) | 2042 if (!ShouldPaint()) |
2015 return; | 2043 return; |
2016 | 2044 |
2017 ui::PaintContext context(parent_context, GetPaintContextOffset()); | 2045 const gfx::Rect& parent_bounds = (layer() || !parent()) |
2046 ? GetPaintRecordingBounds() | |
2047 : parent()->GetPaintRecordingBounds(); | |
2048 PaintInfo paint_info(parent_paint_info, GetPaintRecordingBounds(), | |
2049 parent_bounds, GetPaintScaleType()); | |
2050 | |
2051 const ui::PaintContext& context = paint_info.context(); | |
2052 | |
2018 ui::TransformRecorder transform_recorder(context); | 2053 ui::TransformRecorder transform_recorder(context); |
2019 SetupTransformRecorderForPainting(&transform_recorder); | 2054 SetupTransformRecorderForPainting(&transform_recorder, |
2055 paint_info.offset_from_parent()); | |
2020 | 2056 |
2021 RecursivePaintHelper(&View::PaintDebugRects, context); | 2057 RecursivePaintHelper(&View::PaintDebugRects, paint_info); |
2022 | 2058 |
2023 // Draw outline rects for debugging. | 2059 // Draw outline rects for debugging. |
2024 ui::PaintRecorder recorder(context, size()); | 2060 ui::PaintRecorder recorder(context, paint_info.paint_recording_size(), |
2061 paint_info.paint_recording_scale(), &paint_cache_); | |
2025 gfx::Canvas* canvas = recorder.canvas(); | 2062 gfx::Canvas* canvas = recorder.canvas(); |
2026 const float scale = canvas->UndoDeviceScaleFactor(); | 2063 const float scale = canvas->UndoDeviceScaleFactor(); |
2027 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); | 2064 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); |
2028 outline_rect.Inset(0.5f, 0.5f); | 2065 outline_rect.Inset(0.5f, 0.5f); |
2029 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); | 2066 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); |
2030 canvas->DrawRect(outline_rect, color); | 2067 canvas->DrawRect(outline_rect, color); |
2031 } | 2068 } |
2032 | 2069 |
2033 // Tree operations ------------------------------------------------------------- | 2070 // Tree operations ------------------------------------------------------------- |
2034 | 2071 |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2678 // Message the RootView to do the drag and drop. That way if we're removed | 2715 // Message the RootView to do the drag and drop. That way if we're removed |
2679 // the RootView can detect it and avoid calling us back. | 2716 // the RootView can detect it and avoid calling us back. |
2680 gfx::Point widget_location(event.location()); | 2717 gfx::Point widget_location(event.location()); |
2681 ConvertPointToWidget(this, &widget_location); | 2718 ConvertPointToWidget(this, &widget_location); |
2682 widget->RunShellDrag(this, data, widget_location, drag_operations, source); | 2719 widget->RunShellDrag(this, data, widget_location, drag_operations, source); |
2683 // WARNING: we may have been deleted. | 2720 // WARNING: we may have been deleted. |
2684 return true; | 2721 return true; |
2685 } | 2722 } |
2686 | 2723 |
2687 } // namespace views | 2724 } // namespace views |
OLD | NEW |