Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: ui/views/view.cc

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Update tests Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 ui::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 ui::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 (!paint_info.IsPixelCanvas()) {
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(paint_info, &paint_cache_);
909 gfx::Canvas* canvas = recorder.canvas(); 930 gfx::Canvas* canvas = recorder.canvas();
910 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), 931 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(),
911 flip_canvas_on_paint_for_rtl_ui_); 932 flip_canvas_on_paint_for_rtl_ui_);
912 933
913 // Delegate painting the contents of the View to the virtual OnPaint method. 934 // Delegate painting the contents of the View to the virtual OnPaint method.
914 OnPaint(canvas); 935 OnPaint(canvas);
915 } 936 }
916 937
917 // View::Paint() recursion over the subtree. 938 // View::Paint() recursion over the subtree.
918 PaintChildren(context); 939 PaintChildren(paint_info);
919 } 940 }
920 941
921 void View::SetBackground(std::unique_ptr<Background> b) { 942 void View::SetBackground(std::unique_ptr<Background> b) {
922 background_ = std::move(b); 943 background_ = std::move(b);
923 } 944 }
924 945
925 void View::SetBorder(std::unique_ptr<Border> b) { 946 void View::SetBorder(std::unique_ptr<Border> b) {
926 border_ = std::move(b); 947 border_ = std::move(b);
927 } 948 }
928 949
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1536 RegisterPendingAccelerators(); 1557 RegisterPendingAccelerators();
1537 } 1558 }
1538 } 1559 }
1539 1560
1540 void View::AddedToWidget() {} 1561 void View::AddedToWidget() {}
1541 1562
1542 void View::RemovedFromWidget() {} 1563 void View::RemovedFromWidget() {}
1543 1564
1544 // Painting -------------------------------------------------------------------- 1565 // Painting --------------------------------------------------------------------
1545 1566
1546 void View::PaintChildren(const ui::PaintContext& context) { 1567 void View::PaintChildren(const ui::PaintInfo& paint_info) {
1547 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); 1568 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1548 RecursivePaintHelper(&View::Paint, context); 1569 RecursivePaintHelper(&View::Paint, paint_info);
1549 } 1570 }
1550 1571
1551 void View::OnPaint(gfx::Canvas* canvas) { 1572 void View::OnPaint(gfx::Canvas* canvas) {
1552 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); 1573 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1553 OnPaintBackground(canvas); 1574 OnPaintBackground(canvas);
1554 OnPaintBorder(canvas); 1575 OnPaintBorder(canvas);
1555 } 1576 }
1556 1577
1557 void View::OnPaintBackground(gfx::Canvas* canvas) { 1578 void View::OnPaintBackground(gfx::Canvas* canvas) {
1558 if (background_) { 1579 if (background_) {
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1810 // and for different display density. 1831 // and for different display density.
1811 return kDefaultHorizontalDragThreshold; 1832 return kDefaultHorizontalDragThreshold;
1812 } 1833 }
1813 1834
1814 int View::GetVerticalDragThreshold() { 1835 int View::GetVerticalDragThreshold() {
1815 // TODO(jennyz): This value may need to be adjusted for different platforms 1836 // TODO(jennyz): This value may need to be adjusted for different platforms
1816 // and for different display density. 1837 // and for different display density.
1817 return kDefaultVerticalDragThreshold; 1838 return kDefaultVerticalDragThreshold;
1818 } 1839 }
1819 1840
1841 ui::PaintInfo::ScaleType View::GetPaintScaleType() const {
1842 return ui::PaintInfo::ScaleType::kScaleToFit;
1843 }
1844
1820 // Debugging ------------------------------------------------------------------- 1845 // Debugging -------------------------------------------------------------------
1821 1846
1822 #if !defined(NDEBUG) 1847 #if !defined(NDEBUG)
1823 1848
1824 std::string View::PrintViewGraph(bool first) { 1849 std::string View::PrintViewGraph(bool first) {
1825 return DoPrintViewGraph(first, this); 1850 return DoPrintViewGraph(first, this);
1826 } 1851 }
1827 1852
1828 std::string View::DoPrintViewGraph(bool first, View* view_with_children) { 1853 std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
1829 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. 1854 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1962 // Translate the requested paint rect to the parent's coordinate system 1987 // Translate the requested paint rect to the parent's coordinate system
1963 // then pass this notification up to the parent. 1988 // then pass this notification up to the parent.
1964 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); 1989 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds()));
1965 } 1990 }
1966 } 1991 }
1967 1992
1968 bool View::ShouldPaint() const { 1993 bool View::ShouldPaint() const {
1969 return visible_ && !size().IsEmpty(); 1994 return visible_ && !size().IsEmpty();
1970 } 1995 }
1971 1996
1972 gfx::Vector2d View::GetPaintContextOffset() const { 1997 gfx::Rect View::GetPaintRecordingBounds() const {
1973 // If the View has a layer() then it is a paint root. Otherwise, we need to 1998 // 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. 1999 // is needed. Otherwise, we need bounds that includes an offset from the
2000 // parent to add to the total offset from the paint root.
1975 DCHECK(layer() || parent() || origin() == gfx::Point()); 2001 DCHECK(layer() || parent() || origin() == gfx::Point());
1976 return layer() ? gfx::Vector2d() : GetMirroredPosition().OffsetFromOrigin(); 2002 return layer() ? GetLocalBounds() : GetMirroredBounds();
1977 } 2003 }
1978 2004
1979 void View::SetupTransformRecorderForPainting( 2005 void View::SetupTransformRecorderForPainting(
1980 ui::TransformRecorder* recorder) const { 2006 ui::TransformRecorder* recorder,
2007 const gfx::Vector2d& offset_from_parent) const {
1981 // If the view is backed by a layer, it should paint with itself as the origin 2008 // If the view is backed by a layer, it should paint with itself as the origin
1982 // rather than relative to its parent. 2009 // rather than relative to its parent.
1983 if (layer()) 2010 if (layer())
1984 return; 2011 return;
1985 2012
1986 // Translate the graphics such that 0,0 corresponds to where this View is 2013 // Translate the graphics such that 0,0 corresponds to where this View is
1987 // located relative to its parent. 2014 // located relative to its parent.
1988 gfx::Transform transform_from_parent; 2015 gfx::Transform transform_from_parent;
1989 gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin();
1990 transform_from_parent.Translate(offset_from_parent.x(), 2016 transform_from_parent.Translate(offset_from_parent.x(),
1991 offset_from_parent.y()); 2017 offset_from_parent.y());
1992 transform_from_parent.PreconcatTransform(GetTransform());
1993 recorder->Transform(transform_from_parent); 2018 recorder->Transform(transform_from_parent);
1994 } 2019 }
1995 2020
1996 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&), 2021 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintInfo&),
1997 const ui::PaintContext& context) { 2022 const ui::PaintInfo& info) {
1998 View::Views children = GetChildrenInZOrder(); 2023 View::Views children = GetChildrenInZOrder();
1999 DCHECK_EQ(child_count(), static_cast<int>(children.size())); 2024 DCHECK_EQ(child_count(), static_cast<int>(children.size()));
2000 for (auto* child : children) { 2025 for (auto* child : children) {
2001 if (!child->layer()) 2026 if (!child->layer())
2002 (child->*func)(context); 2027 (child->*func)(info);
2003 } 2028 }
2004 } 2029 }
2005 2030
2006 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { 2031 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) {
2007 Paint(parent_context); 2032 ui::PaintInfo paint_info(parent_context, layer() ? layer()->size() : size());
2033 Paint(paint_info);
2008 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 2034 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2009 switches::kDrawViewBoundsRects)) 2035 switches::kDrawViewBoundsRects))
2010 PaintDebugRects(parent_context); 2036 PaintDebugRects(paint_info);
2011 } 2037 }
2012 2038
2013 void View::PaintDebugRects(const ui::PaintContext& parent_context) { 2039 void View::PaintDebugRects(const ui::PaintInfo& parent_paint_info) {
2014 if (!ShouldPaint()) 2040 if (!ShouldPaint())
2015 return; 2041 return;
2016 2042
2017 ui::PaintContext context(parent_context, GetPaintContextOffset()); 2043 const gfx::Rect& parent_bounds = (layer() || !parent())
2044 ? GetPaintRecordingBounds()
2045 : parent()->GetPaintRecordingBounds();
2046 ui::PaintInfo paint_info(parent_paint_info, GetPaintRecordingBounds(),
2047 parent_bounds, GetPaintScaleType());
2048
2049 const ui::PaintContext& context = paint_info.context();
2050
2018 ui::TransformRecorder transform_recorder(context); 2051 ui::TransformRecorder transform_recorder(context);
2019 SetupTransformRecorderForPainting(&transform_recorder); 2052 SetupTransformRecorderForPainting(&transform_recorder,
2053 paint_info.offset_from_parent());
2020 2054
2021 RecursivePaintHelper(&View::PaintDebugRects, context); 2055 RecursivePaintHelper(&View::PaintDebugRects, paint_info);
2022 2056
2023 // Draw outline rects for debugging. 2057 // Draw outline rects for debugging.
2024 ui::PaintRecorder recorder(context, size()); 2058 ui::PaintRecorder recorder(paint_info, &paint_cache_);
2025 gfx::Canvas* canvas = recorder.canvas(); 2059 gfx::Canvas* canvas = recorder.canvas();
2026 const float scale = canvas->UndoDeviceScaleFactor(); 2060 const float scale = canvas->UndoDeviceScaleFactor();
2027 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); 2061 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale));
2028 outline_rect.Inset(0.5f, 0.5f); 2062 outline_rect.Inset(0.5f, 0.5f);
2029 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); 2063 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0);
2030 canvas->DrawRect(outline_rect, color); 2064 canvas->DrawRect(outline_rect, color);
2031 } 2065 }
2032 2066
2033 // Tree operations ------------------------------------------------------------- 2067 // Tree operations -------------------------------------------------------------
2034 2068
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
2678 // Message the RootView to do the drag and drop. That way if we're removed 2712 // 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. 2713 // the RootView can detect it and avoid calling us back.
2680 gfx::Point widget_location(event.location()); 2714 gfx::Point widget_location(event.location());
2681 ConvertPointToWidget(this, &widget_location); 2715 ConvertPointToWidget(this, &widget_location);
2682 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2716 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2683 // WARNING: we may have been deleted. 2717 // WARNING: we may have been deleted.
2684 return true; 2718 return true;
2685 } 2719 }
2686 2720
2687 } // namespace views 2721 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698