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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: nit Created 3 years, 6 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 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 } else if (parent_) { 831 } else if (parent_) {
832 // Translate the requested paint rect to the parent's coordinate system 832 // Translate the requested paint rect to the parent's coordinate system
833 // then pass this notification up to the parent. 833 // then pass this notification up to the parent.
834 parent_->SchedulePaintInRect(ConvertRectToParent(rect)); 834 parent_->SchedulePaintInRect(ConvertRectToParent(rect));
835 } 835 }
836 } 836 }
837 837
838 void View::Paint(const ui::PaintContext& parent_context) { 838 void View::Paint(const ui::PaintContext& parent_context) {
839 if (!ShouldPaint()) 839 if (!ShouldPaint())
840 return; 840 return;
841 841 ui::PaintContext context(parent_context, GetPaintContextBounds(),
842 ui::PaintContext context(parent_context, GetPaintContextOffset()); 842 GetContextScaleType());
843 843
844 bool is_invalidated = true; 844 bool is_invalidated = true;
845 if (context.CanCheckInvalid()) { 845 if (context.CanCheckRepaint()) {
846 #if DCHECK_IS_ON() 846 #if DCHECK_IS_ON()
847 gfx::Vector2d offset; 847 if (!context.IsPixelCanvas()) {
848 context.Visited(this); 848 gfx::Vector2d offset;
849 View* view = this; 849 context.Visited(this);
850 while (view->parent() && !view->layer()) { 850 View* view = this;
851 DCHECK(view->GetTransform().IsIdentity()); 851 while (view->parent() && !view->layer()) {
852 offset += view->GetMirroredPosition().OffsetFromOrigin(); 852 DCHECK(view->GetTransform().IsIdentity());
853 view = view->parent(); 853 offset += view->GetMirroredPosition().OffsetFromOrigin();
854 view = view->parent();
855 }
856 // The offset in the PaintContext should be the offset up to the paint
857 // root, which we compute and verify here.
858 DCHECK_EQ(context.PaintOffset().x(), offset.x());
859 DCHECK_EQ(context.PaintOffset().y(), offset.y());
860 // The above loop will stop when |view| is the paint root, which should be
861 // the root of the current paint walk, as verified by storing the root in
862 // the PaintContext.
863 DCHECK_EQ(context.RootVisited(), view);
854 } 864 }
855 // The offset in the PaintContext should be the offset up to the paint root,
856 // which we compute and verify here.
857 DCHECK_EQ(context.PaintOffset().x(), offset.x());
858 DCHECK_EQ(context.PaintOffset().y(), offset.y());
859 // The above loop will stop when |view| is the paint root, which should be
860 // the root of the current paint walk, as verified by storing the root in
861 // the PaintContext.
862 DCHECK_EQ(context.RootVisited(), view);
863 #endif 865 #endif
864 866
865 // If the View wasn't invalidated, don't waste time painting it, the output 867 // If the View wasn't invalidated, don't waste time painting it, the output
866 // would be culled. 868 // would be culled.
867 is_invalidated = context.IsRectInvalid(GetLocalBounds()); 869 is_invalidated = context.ShouldRepaint();
868 } 870 }
869 871
870 TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); 872 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
871 873
872 // If the view is backed by a layer, it should paint with itself as the origin 874 // If the view is backed by a layer, it should paint with itself as the origin
873 // rather than relative to its parent. 875 // rather than relative to its parent.
874 // TODO(danakj): Rework clip and transform recorder usage here to use 876 // TODO(danakj): Rework clip and transform recorder usage here to use
875 // std::optional once we can do so. 877 // std::optional once we can do so.
876 ui::ClipRecorder clip_recorder(parent_context); 878 ui::ClipRecorder clip_recorder(parent_context);
877 if (!layer()) { 879 if (!layer()) {
878 // Set the clip rect to the bounds of this View, or |clip_path_| if it's 880 // Set the clip rect to the bounds of this View, or |clip_path_| if it's
879 // been set. Note that the X (or left) position we pass to ClipRect takes 881 // been set. Note that the X (or left) position we pass to ClipRect takes
880 // into consideration whether or not the View uses a right-to-left layout so 882 // into consideration whether or not the View uses a right-to-left layout so
881 // that we paint the View in its mirrored position if need be. 883 // that we paint the View in its mirrored position if need be.
882 if (clip_path_.isEmpty()) { 884 if (clip_path_.isEmpty()) {
883 clip_recorder.ClipRect(GetMirroredBounds()); 885 clip_recorder.ClipRect(context.pixel_bounds());
884 } else { 886 } else {
885 gfx::Path clip_path_in_parent = clip_path_; 887 gfx::Path clip_path_in_parent = clip_path_;
886 clip_path_in_parent.offset(GetMirroredX(), y()); 888 clip_path_in_parent.transform(
889 context.TransformToParentPixelSpace().matrix());
887 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent); 890 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent);
888 } 891 }
889 } 892 }
890 893
891 ui::TransformRecorder transform_recorder(context); 894 ui::TransformRecorder transform_recorder(context);
892 SetupTransformRecorderForPainting(&transform_recorder); 895 SetupTransformRecorderForPainting(&transform_recorder, context);
893 896
894 // Note that the cache is not aware of the offset of the view 897 // Note that the cache is not aware of the offset of the view
895 // relative to the parent since painting is always done relative to 898 // relative to the parent since painting is always done relative to
896 // the top left of the individual view. 899 // the top left of the individual view.
897 if (is_invalidated || !paint_cache_.UseCache(context, size())) { 900 if (is_invalidated || !paint_cache_.UseCache(context, context.pixel_size())) {
898 ui::PaintRecorder recorder(context, size(), &paint_cache_); 901 ui::PaintRecorder recorder(context, context.pixel_size(), &paint_cache_);
899 gfx::Canvas* canvas = recorder.canvas(); 902 gfx::Canvas* canvas = recorder.canvas();
900 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), 903 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(),
901 flip_canvas_on_paint_for_rtl_ui_); 904 flip_canvas_on_paint_for_rtl_ui_);
902 905
903 // Delegate painting the contents of the View to the virtual OnPaint method. 906 // Delegate painting the contents of the View to the virtual OnPaint method.
904 OnPaint(canvas); 907 OnPaint(canvas);
905 } 908 }
906 909
907 // View::Paint() recursion over the subtree. 910 // View::Paint() recursion over the subtree.
908 PaintChildren(context); 911 PaintChildren(context);
(...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 // and for different display density. 1803 // and for different display density.
1801 return kDefaultHorizontalDragThreshold; 1804 return kDefaultHorizontalDragThreshold;
1802 } 1805 }
1803 1806
1804 int View::GetVerticalDragThreshold() { 1807 int View::GetVerticalDragThreshold() {
1805 // TODO(jennyz): This value may need to be adjusted for different platforms 1808 // TODO(jennyz): This value may need to be adjusted for different platforms
1806 // and for different display density. 1809 // and for different display density.
1807 return kDefaultVerticalDragThreshold; 1810 return kDefaultVerticalDragThreshold;
1808 } 1811 }
1809 1812
1813 int View::GetContextScaleType() const {
1814 return ui::PaintContext::SCALE_TO_FIT;
1815 }
1816
1810 // Debugging ------------------------------------------------------------------- 1817 // Debugging -------------------------------------------------------------------
1811 1818
1812 #if !defined(NDEBUG) 1819 #if !defined(NDEBUG)
1813 1820
1814 std::string View::PrintViewGraph(bool first) { 1821 std::string View::PrintViewGraph(bool first) {
1815 return DoPrintViewGraph(first, this); 1822 return DoPrintViewGraph(first, this);
1816 } 1823 }
1817 1824
1818 std::string View::DoPrintViewGraph(bool first, View* view_with_children) { 1825 std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
1819 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. 1826 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 // Translate the requested paint rect to the parent's coordinate system 1959 // Translate the requested paint rect to the parent's coordinate system
1953 // then pass this notification up to the parent. 1960 // then pass this notification up to the parent.
1954 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); 1961 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds()));
1955 } 1962 }
1956 } 1963 }
1957 1964
1958 bool View::ShouldPaint() const { 1965 bool View::ShouldPaint() const {
1959 return visible_ && !size().IsEmpty(); 1966 return visible_ && !size().IsEmpty();
1960 } 1967 }
1961 1968
1962 gfx::Vector2d View::GetPaintContextOffset() const { 1969 gfx::Rect View::GetPaintContextBounds() const {
1963 // If the View has a layer() then it is a paint root. Otherwise, we need to 1970 // If the View has a layer() then it is a paint root. Otherwise, we need to
1964 // add the offset from the parent into the total offset from the paint root. 1971 // add the offset from the parent into the total offset from the paint root.
sky 2017/06/23 23:40:23 Update comment. I'm not clear on why this is GetLo
malaykeshav 2017/06/24 00:30:03 In case of a layer, the context is the root contex
1965 DCHECK(layer() || parent() || origin() == gfx::Point()); 1972 DCHECK(layer() || parent() || origin() == gfx::Point());
1966 return layer() ? gfx::Vector2d() : GetMirroredPosition().OffsetFromOrigin(); 1973 return layer() ? GetLocalBounds() : GetMirroredBounds();
1967 } 1974 }
1968 1975
1969 void View::SetupTransformRecorderForPainting( 1976 void View::SetupTransformRecorderForPainting(
1970 ui::TransformRecorder* recorder) const { 1977 ui::TransformRecorder* recorder,
1978 const ui::PaintContext& context) const {
1971 // If the view is backed by a layer, it should paint with itself as the origin 1979 // If the view is backed by a layer, it should paint with itself as the origin
1972 // rather than relative to its parent. 1980 // rather than relative to its parent.
1973 if (layer()) 1981 if (layer())
1974 return; 1982 return;
1975 1983
1976 // Translate the graphics such that 0,0 corresponds to where this View is 1984 // Translate the graphics such that 0,0 corresponds to where this View is
1977 // located relative to its parent. 1985 // located relative to its parent.
1978 gfx::Transform transform_from_parent; 1986 gfx::Transform transform_from_parent;
1979 gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin(); 1987 gfx::Vector2d offset_from_parent = context.pixel_bounds().OffsetFromOrigin();
1980 transform_from_parent.Translate(offset_from_parent.x(), 1988 transform_from_parent.Translate(offset_from_parent.x(),
1981 offset_from_parent.y()); 1989 offset_from_parent.y());
1982 transform_from_parent.PreconcatTransform(GetTransform());
1983 recorder->Transform(transform_from_parent); 1990 recorder->Transform(transform_from_parent);
1984 } 1991 }
1985 1992
1986 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&), 1993 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&),
1987 const ui::PaintContext& context) { 1994 const ui::PaintContext& context) {
1988 View::Views children = GetChildrenInZOrder(); 1995 View::Views children = GetChildrenInZOrder();
1989 DCHECK_EQ(child_count(), static_cast<int>(children.size())); 1996 DCHECK_EQ(child_count(), static_cast<int>(children.size()));
1990 for (auto* child : children) { 1997 for (auto* child : children) {
1991 if (!child->layer()) 1998 if (!child->layer())
1992 (child->*func)(context); 1999 (child->*func)(context);
1993 } 2000 }
1994 } 2001 }
1995 2002
1996 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { 2003 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) {
1997 Paint(parent_context); 2004 Paint(parent_context);
1998 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 2005 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1999 switches::kDrawViewBoundsRects)) 2006 switches::kDrawViewBoundsRects))
2000 PaintDebugRects(parent_context); 2007 PaintDebugRects(parent_context);
2001 } 2008 }
2002 2009
2003 void View::PaintDebugRects(const ui::PaintContext& parent_context) { 2010 void View::PaintDebugRects(const ui::PaintContext& parent_context) {
2004 if (!ShouldPaint()) 2011 if (!ShouldPaint())
2005 return; 2012 return;
2006 2013
2007 ui::PaintContext context(parent_context, GetPaintContextOffset()); 2014 ui::PaintContext context(parent_context, GetPaintContextBounds(),
2015 GetContextScaleType());
2008 ui::TransformRecorder transform_recorder(context); 2016 ui::TransformRecorder transform_recorder(context);
2009 SetupTransformRecorderForPainting(&transform_recorder); 2017 SetupTransformRecorderForPainting(&transform_recorder, context);
2010 2018
2011 RecursivePaintHelper(&View::PaintDebugRects, context); 2019 RecursivePaintHelper(&View::PaintDebugRects, context);
2012 2020
2013 // Draw outline rects for debugging. 2021 // Draw outline rects for debugging.
2014 ui::PaintRecorder recorder(context, size()); 2022 ui::PaintRecorder recorder(context, context.pixel_size(), &paint_cache_);
2015 gfx::Canvas* canvas = recorder.canvas(); 2023 gfx::Canvas* canvas = recorder.canvas();
2016 const float scale = canvas->UndoDeviceScaleFactor(); 2024 const float scale = canvas->UndoDeviceScaleFactor();
2017 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); 2025 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale));
2018 outline_rect.Inset(0.5f, 0.5f); 2026 outline_rect.Inset(0.5f, 0.5f);
2019 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); 2027 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0);
2020 canvas->DrawRect(outline_rect, color); 2028 canvas->DrawRect(outline_rect, color);
2021 } 2029 }
2022 2030
2023 // Tree operations ------------------------------------------------------------- 2031 // Tree operations -------------------------------------------------------------
2024 2032
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
2668 // Message the RootView to do the drag and drop. That way if we're removed 2676 // Message the RootView to do the drag and drop. That way if we're removed
2669 // the RootView can detect it and avoid calling us back. 2677 // the RootView can detect it and avoid calling us back.
2670 gfx::Point widget_location(event.location()); 2678 gfx::Point widget_location(event.location());
2671 ConvertPointToWidget(this, &widget_location); 2679 ConvertPointToWidget(this, &widget_location);
2672 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2680 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2673 // WARNING: we may have been deleted. 2681 // WARNING: we may have been deleted.
2674 return true; 2682 return true;
2675 } 2683 }
2676 2684
2677 } // namespace views 2685 } // namespace views
OLDNEW
« ui/views/view.h ('K') | « ui/views/view.h ('k') | ui/views/view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698