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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Adds RasterSource Unittest 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 15 matching lines...) Expand all
26 #include "ui/accessibility/ax_action_data.h" 26 #include "ui/accessibility/ax_action_data.h"
27 #include "ui/accessibility/ax_enums.h" 27 #include "ui/accessibility/ax_enums.h"
28 #include "ui/base/cursor/cursor.h" 28 #include "ui/base/cursor/cursor.h"
29 #include "ui/base/dragdrop/drag_drop_types.h" 29 #include "ui/base/dragdrop/drag_drop_types.h"
30 #include "ui/base/ime/input_method.h" 30 #include "ui/base/ime/input_method.h"
31 #include "ui/compositor/clip_recorder.h" 31 #include "ui/compositor/clip_recorder.h"
32 #include "ui/compositor/compositor.h" 32 #include "ui/compositor/compositor.h"
33 #include "ui/compositor/dip_util.h" 33 #include "ui/compositor/dip_util.h"
34 #include "ui/compositor/layer.h" 34 #include "ui/compositor/layer.h"
35 #include "ui/compositor/layer_animator.h" 35 #include "ui/compositor/layer_animator.h"
36 #include "ui/compositor/paint_context.h"
37 #include "ui/compositor/paint_recorder.h" 36 #include "ui/compositor/paint_recorder.h"
38 #include "ui/compositor/transform_recorder.h" 37 #include "ui/compositor/transform_recorder.h"
39 #include "ui/display/screen.h" 38 #include "ui/display/screen.h"
40 #include "ui/events/base_event_utils.h" 39 #include "ui/events/base_event_utils.h"
41 #include "ui/events/event_target_iterator.h" 40 #include "ui/events/event_target_iterator.h"
42 #include "ui/gfx/canvas.h" 41 #include "ui/gfx/canvas.h"
43 #include "ui/gfx/geometry/point3_f.h" 42 #include "ui/gfx/geometry/point3_f.h"
44 #include "ui/gfx/geometry/point_conversions.h" 43 #include "ui/gfx/geometry/point_conversions.h"
45 #include "ui/gfx/interpolated_transform.h" 44 #include "ui/gfx/interpolated_transform.h"
46 #include "ui/gfx/path.h" 45 #include "ui/gfx/path.h"
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 } else if (parent_) { 840 } else if (parent_) {
842 // Translate the requested paint rect to the parent's coordinate system 841 // Translate the requested paint rect to the parent's coordinate system
843 // then pass this notification up to the parent. 842 // then pass this notification up to the parent.
844 parent_->SchedulePaintInRect(ConvertRectToParent(rect)); 843 parent_->SchedulePaintInRect(ConvertRectToParent(rect));
845 } 844 }
846 } 845 }
847 846
848 void View::Paint(const ui::PaintContext& parent_context) { 847 void View::Paint(const ui::PaintContext& parent_context) {
849 if (!ShouldPaint()) 848 if (!ShouldPaint())
850 return; 849 return;
851 850 const gfx::Rect& parent_bounds =
852 ui::PaintContext context(parent_context, GetPaintContextOffset()); 851 !parent() ? GetPaintContextBounds() : parent()->GetPaintContextBounds();
852 ui::PaintContext context(parent_context, GetPaintContextBounds(),
853 parent_bounds, GetContextScaleType());
853 854
854 bool is_invalidated = true; 855 bool is_invalidated = true;
855 if (context.CanCheckInvalid()) { 856 if (context.CanCheckRepaint()) {
856 #if DCHECK_IS_ON() 857 #if DCHECK_IS_ON()
857 gfx::Vector2d offset; 858 if (!context.IsPixelCanvas()) {
858 context.Visited(this); 859 gfx::Vector2d offset;
859 View* view = this; 860 context.Visited(this);
860 while (view->parent() && !view->layer()) { 861 View* view = this;
861 DCHECK(view->GetTransform().IsIdentity()); 862 while (view->parent() && !view->layer()) {
862 offset += view->GetMirroredPosition().OffsetFromOrigin(); 863 DCHECK(view->GetTransform().IsIdentity());
863 view = view->parent(); 864 offset += view->GetMirroredPosition().OffsetFromOrigin();
865 view = view->parent();
866 }
867 // The offset in the PaintContext should be the offset up to the paint
868 // root, which we compute and verify here.
869 DCHECK_EQ(context.PaintOffset().x(), offset.x());
870 DCHECK_EQ(context.PaintOffset().y(), offset.y());
871 // The above loop will stop when |view| is the paint root, which should be
872 // the root of the current paint walk, as verified by storing the root in
873 // the PaintContext.
874 DCHECK_EQ(context.RootVisited(), view);
864 } 875 }
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 876 #endif
874 877
875 // If the View wasn't invalidated, don't waste time painting it, the output 878 // If the View wasn't invalidated, don't waste time painting it, the output
876 // would be culled. 879 // would be culled.
877 is_invalidated = context.IsRectInvalid(GetLocalBounds()); 880 is_invalidated = context.ShouldRepaint();
878 } 881 }
879 882
880 TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); 883 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
881 884
882 // If the view is backed by a layer, it should paint with itself as the origin 885 // If the view is backed by a layer, it should paint with itself as the origin
883 // rather than relative to its parent. 886 // rather than relative to its parent.
884 // TODO(danakj): Rework clip and transform recorder usage here to use 887 // TODO(danakj): Rework clip and transform recorder usage here to use
885 // std::optional once we can do so. 888 // std::optional once we can do so.
886 ui::ClipRecorder clip_recorder(parent_context); 889 ui::ClipRecorder clip_recorder(parent_context);
887 if (!layer()) { 890 if (!layer()) {
888 // Set the clip rect to the bounds of this View, or |clip_path_| if it's 891 // 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 892 // 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 893 // 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. 894 // that we paint the View in its mirrored position if need be.
892 if (clip_path_.isEmpty()) { 895 if (clip_path_.isEmpty()) {
893 clip_recorder.ClipRect(GetMirroredBounds()); 896 clip_recorder.ClipRect(context.paint_recording_bounds());
894 } else { 897 } else {
895 gfx::Path clip_path_in_parent = clip_path_; 898 gfx::Path clip_path_in_parent = clip_path_;
896 clip_path_in_parent.offset(GetMirroredX(), y()); 899 clip_path_in_parent.transform(
900 context.TransformToParentRecordingSpace().matrix());
897 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent); 901 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent);
898 } 902 }
899 } 903 }
900 904
901 ui::TransformRecorder transform_recorder(context); 905 ui::TransformRecorder transform_recorder(context);
902 SetupTransformRecorderForPainting(&transform_recorder); 906 SetupTransformRecorderForPainting(&transform_recorder, context);
903 907
904 // Note that the cache is not aware of the offset of the view 908 // 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 909 // relative to the parent since painting is always done relative to
906 // the top left of the individual view. 910 // the top left of the individual view.
907 if (is_invalidated || !paint_cache_.UseCache(context, size())) { 911 if (is_invalidated ||
908 ui::PaintRecorder recorder(context, size(), &paint_cache_); 912 !paint_cache_.UseCache(context, context.paint_recording_size())) {
913 ui::PaintRecorder recorder(context, context.paint_recording_size(),
914 &paint_cache_);
909 gfx::Canvas* canvas = recorder.canvas(); 915 gfx::Canvas* canvas = recorder.canvas();
910 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), 916 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(),
911 flip_canvas_on_paint_for_rtl_ui_); 917 flip_canvas_on_paint_for_rtl_ui_);
912 918
913 // Delegate painting the contents of the View to the virtual OnPaint method. 919 // Delegate painting the contents of the View to the virtual OnPaint method.
914 OnPaint(canvas); 920 OnPaint(canvas);
915 } 921 }
916 922
917 // View::Paint() recursion over the subtree. 923 // View::Paint() recursion over the subtree.
918 PaintChildren(context); 924 PaintChildren(context);
(...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after
1810 // and for different display density. 1816 // and for different display density.
1811 return kDefaultHorizontalDragThreshold; 1817 return kDefaultHorizontalDragThreshold;
1812 } 1818 }
1813 1819
1814 int View::GetVerticalDragThreshold() { 1820 int View::GetVerticalDragThreshold() {
1815 // TODO(jennyz): This value may need to be adjusted for different platforms 1821 // TODO(jennyz): This value may need to be adjusted for different platforms
1816 // and for different display density. 1822 // and for different display density.
1817 return kDefaultVerticalDragThreshold; 1823 return kDefaultVerticalDragThreshold;
1818 } 1824 }
1819 1825
1826 ui::PaintContext::ScaleType View::GetContextScaleType() const {
1827 return ui::PaintContext::ScaleType::SCALE_TO_FIT;
1828 }
1829
1820 // Debugging ------------------------------------------------------------------- 1830 // Debugging -------------------------------------------------------------------
1821 1831
1822 #if !defined(NDEBUG) 1832 #if !defined(NDEBUG)
1823 1833
1824 std::string View::PrintViewGraph(bool first) { 1834 std::string View::PrintViewGraph(bool first) {
1825 return DoPrintViewGraph(first, this); 1835 return DoPrintViewGraph(first, this);
1826 } 1836 }
1827 1837
1828 std::string View::DoPrintViewGraph(bool first, View* view_with_children) { 1838 std::string View::DoPrintViewGraph(bool first, View* view_with_children) {
1829 // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19. 1839 // 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 1972 // Translate the requested paint rect to the parent's coordinate system
1963 // then pass this notification up to the parent. 1973 // then pass this notification up to the parent.
1964 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); 1974 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds()));
1965 } 1975 }
1966 } 1976 }
1967 1977
1968 bool View::ShouldPaint() const { 1978 bool View::ShouldPaint() const {
1969 return visible_ && !size().IsEmpty(); 1979 return visible_ && !size().IsEmpty();
1970 } 1980 }
1971 1981
1972 gfx::Vector2d View::GetPaintContextOffset() const { 1982 gfx::Rect View::GetPaintContextBounds() const {
1973 // If the View has a layer() then it is a paint root. Otherwise, we need to 1983 // If the View has a layer() then it is a paint root. Otherwise, we need to
1974 // add the offset from the parent into the total offset from the paint root. 1984 // add the offset from the parent into the total offset from the paint root.
1975 DCHECK(layer() || parent() || origin() == gfx::Point()); 1985 DCHECK(layer() || parent() || origin() == gfx::Point());
1976 return layer() ? gfx::Vector2d() : GetMirroredPosition().OffsetFromOrigin(); 1986 return layer() ? GetLocalBounds() : GetMirroredBounds();
1977 } 1987 }
1978 1988
1979 void View::SetupTransformRecorderForPainting( 1989 void View::SetupTransformRecorderForPainting(
1980 ui::TransformRecorder* recorder) const { 1990 ui::TransformRecorder* recorder,
1991 const ui::PaintContext& context) const {
1981 // If the view is backed by a layer, it should paint with itself as the origin 1992 // If the view is backed by a layer, it should paint with itself as the origin
1982 // rather than relative to its parent. 1993 // rather than relative to its parent.
1983 if (layer()) 1994 if (layer())
1984 return; 1995 return;
1985 1996
1986 // Translate the graphics such that 0,0 corresponds to where this View is 1997 // Translate the graphics such that 0,0 corresponds to where this View is
1987 // located relative to its parent. 1998 // located relative to its parent.
1988 gfx::Transform transform_from_parent; 1999 gfx::Transform transform_from_parent;
1989 gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin(); 2000 gfx::Vector2d offset_from_parent =
2001 context.paint_recording_bounds().OffsetFromOrigin();
1990 transform_from_parent.Translate(offset_from_parent.x(), 2002 transform_from_parent.Translate(offset_from_parent.x(),
1991 offset_from_parent.y()); 2003 offset_from_parent.y());
1992 transform_from_parent.PreconcatTransform(GetTransform());
1993 recorder->Transform(transform_from_parent); 2004 recorder->Transform(transform_from_parent);
1994 } 2005 }
1995 2006
1996 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&), 2007 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&),
1997 const ui::PaintContext& context) { 2008 const ui::PaintContext& context) {
1998 View::Views children = GetChildrenInZOrder(); 2009 View::Views children = GetChildrenInZOrder();
1999 DCHECK_EQ(child_count(), static_cast<int>(children.size())); 2010 DCHECK_EQ(child_count(), static_cast<int>(children.size()));
2000 for (auto* child : children) { 2011 for (auto* child : children) {
2001 if (!child->layer()) 2012 if (!child->layer())
2002 (child->*func)(context); 2013 (child->*func)(context);
2003 } 2014 }
2004 } 2015 }
2005 2016
2006 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { 2017 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) {
2007 Paint(parent_context); 2018 Paint(parent_context);
2008 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 2019 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
2009 switches::kDrawViewBoundsRects)) 2020 switches::kDrawViewBoundsRects))
2010 PaintDebugRects(parent_context); 2021 PaintDebugRects(parent_context);
2011 } 2022 }
2012 2023
2013 void View::PaintDebugRects(const ui::PaintContext& parent_context) { 2024 void View::PaintDebugRects(const ui::PaintContext& parent_context) {
2014 if (!ShouldPaint()) 2025 if (!ShouldPaint())
2015 return; 2026 return;
2016 2027
2017 ui::PaintContext context(parent_context, GetPaintContextOffset()); 2028 const gfx::Rect& parent_bounds = (layer() || !parent())
2029 ? GetPaintContextBounds()
2030 : parent()->GetPaintContextBounds();
2031 ui::PaintContext context(parent_context, GetPaintContextBounds(),
2032 parent_bounds, GetContextScaleType());
2018 ui::TransformRecorder transform_recorder(context); 2033 ui::TransformRecorder transform_recorder(context);
2019 SetupTransformRecorderForPainting(&transform_recorder); 2034 SetupTransformRecorderForPainting(&transform_recorder, context);
2020 2035
2021 RecursivePaintHelper(&View::PaintDebugRects, context); 2036 RecursivePaintHelper(&View::PaintDebugRects, context);
2022 2037
2023 // Draw outline rects for debugging. 2038 // Draw outline rects for debugging.
2024 ui::PaintRecorder recorder(context, size()); 2039 ui::PaintRecorder recorder(context, context.paint_recording_size(),
2040 &paint_cache_);
2025 gfx::Canvas* canvas = recorder.canvas(); 2041 gfx::Canvas* canvas = recorder.canvas();
2026 const float scale = canvas->UndoDeviceScaleFactor(); 2042 const float scale = canvas->UndoDeviceScaleFactor();
2027 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); 2043 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale));
2028 outline_rect.Inset(0.5f, 0.5f); 2044 outline_rect.Inset(0.5f, 0.5f);
2029 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); 2045 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0);
2030 canvas->DrawRect(outline_rect, color); 2046 canvas->DrawRect(outline_rect, color);
2031 } 2047 }
2032 2048
2033 // Tree operations ------------------------------------------------------------- 2049 // Tree operations -------------------------------------------------------------
2034 2050
(...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 2694 // 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. 2695 // the RootView can detect it and avoid calling us back.
2680 gfx::Point widget_location(event.location()); 2696 gfx::Point widget_location(event.location());
2681 ConvertPointToWidget(this, &widget_location); 2697 ConvertPointToWidget(this, &widget_location);
2682 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2698 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2683 // WARNING: we may have been deleted. 2699 // WARNING: we may have been deleted.
2684 return true; 2700 return true;
2685 } 2701 }
2686 2702
2687 } // namespace views 2703 } // namespace views
OLDNEW
« cc/trees/layer_tree_host.cc ('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