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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: nits 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.offset(GetMirroredX(), y());
889 clip_path_in_parent.transform(SkMatrix::MakeScale(
890 SkFloatToScalar(parent_context.effective_scale_factor_x()),
891 SkFloatToScalar(parent_context.effective_scale_factor_y())));
887 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent); 892 clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent);
888 } 893 }
889 } 894 }
890 895
891 ui::TransformRecorder transform_recorder(context); 896 ui::TransformRecorder transform_recorder(context);
892 SetupTransformRecorderForPainting(&transform_recorder); 897 SetupTransformRecorderForPainting(&transform_recorder, context);
893 898
894 // Note that the cache is not aware of the offset of the view 899 // 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 900 // relative to the parent since painting is always done relative to
896 // the top left of the individual view. 901 // the top left of the individual view.
897 if (is_invalidated || !paint_cache_.UseCache(context, size())) { 902 if (is_invalidated || !paint_cache_.UseCache(context, context.pixel_size())) {
898 ui::PaintRecorder recorder(context, size(), &paint_cache_); 903 ui::PaintRecorder recorder(context, context.pixel_size(), &paint_cache_);
899 gfx::Canvas* canvas = recorder.canvas(); 904 gfx::Canvas* canvas = recorder.canvas();
900 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(), 905 gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(),
901 flip_canvas_on_paint_for_rtl_ui_); 906 flip_canvas_on_paint_for_rtl_ui_);
902 907
903 // Delegate painting the contents of the View to the virtual OnPaint method. 908 // Delegate painting the contents of the View to the virtual OnPaint method.
904 OnPaint(canvas); 909 OnPaint(canvas);
905 } 910 }
906 911
907 // View::Paint() recursion over the subtree. 912 // View::Paint() recursion over the subtree.
908 PaintChildren(context); 913 PaintChildren(context);
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
1475 } 1480 }
1476 1481
1477 void View::RemoveObserver(ViewObserver* observer) { 1482 void View::RemoveObserver(ViewObserver* observer) {
1478 observers_.RemoveObserver(observer); 1483 observers_.RemoveObserver(observer);
1479 } 1484 }
1480 1485
1481 bool View::HasObserver(const ViewObserver* observer) const { 1486 bool View::HasObserver(const ViewObserver* observer) const {
1482 return observers_.HasObserver(observer); 1487 return observers_.HasObserver(observer);
1483 } 1488 }
1484 1489
1490 int View::GetContextScaleType() const {
1491 return ui::PaintContext::SCALE_TO_FIT;
1492 }
1493
1485 //////////////////////////////////////////////////////////////////////////////// 1494 ////////////////////////////////////////////////////////////////////////////////
1486 // View, protected: 1495 // View, protected:
1487 1496
1488 // Size and disposition -------------------------------------------------------- 1497 // Size and disposition --------------------------------------------------------
1489 1498
1490 gfx::Size View::CalculatePreferredSize() const { 1499 gfx::Size View::CalculatePreferredSize() const {
1491 if (layout_manager_.get()) 1500 if (layout_manager_.get())
1492 return layout_manager_->GetPreferredSize(this); 1501 return layout_manager_->GetPreferredSize(this);
1493 return gfx::Size(); 1502 return gfx::Size();
1494 } 1503 }
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 // Translate the requested paint rect to the parent's coordinate system 1961 // Translate the requested paint rect to the parent's coordinate system
1953 // then pass this notification up to the parent. 1962 // then pass this notification up to the parent.
1954 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds())); 1963 parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds()));
1955 } 1964 }
1956 } 1965 }
1957 1966
1958 bool View::ShouldPaint() const { 1967 bool View::ShouldPaint() const {
1959 return visible_ && !size().IsEmpty(); 1968 return visible_ && !size().IsEmpty();
1960 } 1969 }
1961 1970
1962 gfx::Vector2d View::GetPaintContextOffset() const { 1971 gfx::Rect View::GetPaintContextBounds() const {
1963 // If the View has a layer() then it is a paint root. Otherwise, we need to 1972 // 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. 1973 // add the offset from the parent into the total offset from the paint root.
1965 DCHECK(layer() || parent() || origin() == gfx::Point()); 1974 DCHECK(layer() || parent() || origin() == gfx::Point());
1966 return layer() ? gfx::Vector2d() : GetMirroredPosition().OffsetFromOrigin(); 1975 return layer() ? GetLocalBounds() : GetMirroredBounds();
1967 } 1976 }
1968 1977
1969 void View::SetupTransformRecorderForPainting( 1978 void View::SetupTransformRecorderForPainting(
1970 ui::TransformRecorder* recorder) const { 1979 ui::TransformRecorder* recorder,
1980 const ui::PaintContext& context) const {
1971 // If the view is backed by a layer, it should paint with itself as the origin 1981 // If the view is backed by a layer, it should paint with itself as the origin
1972 // rather than relative to its parent. 1982 // rather than relative to its parent.
1973 if (layer()) 1983 if (layer())
1974 return; 1984 return;
1975 1985
1976 // Translate the graphics such that 0,0 corresponds to where this View is 1986 // Translate the graphics such that 0,0 corresponds to where this View is
1977 // located relative to its parent. 1987 // located relative to its parent.
1978 gfx::Transform transform_from_parent; 1988 gfx::Transform transform_from_parent;
1979 gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin(); 1989 gfx::Vector2d offset_from_parent = context.pixel_bounds().OffsetFromOrigin();
1980 transform_from_parent.Translate(offset_from_parent.x(), 1990 transform_from_parent.Translate(offset_from_parent.x(),
1981 offset_from_parent.y()); 1991 offset_from_parent.y());
1982 transform_from_parent.PreconcatTransform(GetTransform());
1983 recorder->Transform(transform_from_parent); 1992 recorder->Transform(transform_from_parent);
1984 } 1993 }
1985 1994
1986 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&), 1995 void View::RecursivePaintHelper(void (View::*func)(const ui::PaintContext&),
1987 const ui::PaintContext& context) { 1996 const ui::PaintContext& context) {
1988 View::Views children = GetChildrenInZOrder(); 1997 View::Views children = GetChildrenInZOrder();
1989 DCHECK_EQ(child_count(), static_cast<int>(children.size())); 1998 DCHECK_EQ(child_count(), static_cast<int>(children.size()));
1990 for (auto* child : children) { 1999 for (auto* child : children) {
1991 if (!child->layer()) 2000 if (!child->layer())
1992 (child->*func)(context); 2001 (child->*func)(context);
1993 } 2002 }
1994 } 2003 }
1995 2004
1996 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) { 2005 void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) {
1997 Paint(parent_context); 2006 Paint(parent_context);
1998 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 2007 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1999 switches::kDrawViewBoundsRects)) 2008 switches::kDrawViewBoundsRects))
2000 PaintDebugRects(parent_context); 2009 PaintDebugRects(parent_context);
2001 } 2010 }
2002 2011
2003 void View::PaintDebugRects(const ui::PaintContext& parent_context) { 2012 void View::PaintDebugRects(const ui::PaintContext& parent_context) {
2004 if (!ShouldPaint()) 2013 if (!ShouldPaint())
2005 return; 2014 return;
2006 2015
2007 ui::PaintContext context(parent_context, GetPaintContextOffset()); 2016 ui::PaintContext context(parent_context, GetPaintContextBounds(),
2017 GetContextScaleType());
2008 ui::TransformRecorder transform_recorder(context); 2018 ui::TransformRecorder transform_recorder(context);
2009 SetupTransformRecorderForPainting(&transform_recorder); 2019 SetupTransformRecorderForPainting(&transform_recorder, context);
2010 2020
2011 RecursivePaintHelper(&View::PaintDebugRects, context); 2021 RecursivePaintHelper(&View::PaintDebugRects, context);
2012 2022
2013 // Draw outline rects for debugging. 2023 // Draw outline rects for debugging.
2014 ui::PaintRecorder recorder(context, size()); 2024 ui::PaintRecorder recorder(context, context.pixel_size(), &paint_cache_);
2015 gfx::Canvas* canvas = recorder.canvas(); 2025 gfx::Canvas* canvas = recorder.canvas();
2016 const float scale = canvas->UndoDeviceScaleFactor(); 2026 const float scale = canvas->UndoDeviceScaleFactor();
2017 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale)); 2027 gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale));
2018 outline_rect.Inset(0.5f, 0.5f); 2028 outline_rect.Inset(0.5f, 0.5f);
2019 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0); 2029 const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0);
2020 canvas->DrawRect(outline_rect, color); 2030 canvas->DrawRect(outline_rect, color);
2021 } 2031 }
2022 2032
2023 // Tree operations ------------------------------------------------------------- 2033 // Tree operations -------------------------------------------------------------
2024 2034
(...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 2678 // 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. 2679 // the RootView can detect it and avoid calling us back.
2670 gfx::Point widget_location(event.location()); 2680 gfx::Point widget_location(event.location());
2671 ConvertPointToWidget(this, &widget_location); 2681 ConvertPointToWidget(this, &widget_location);
2672 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2682 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2673 // WARNING: we may have been deleted. 2683 // WARNING: we may have been deleted.
2674 return true; 2684 return true;
2675 } 2685 }
2676 2686
2677 } // namespace views 2687 } // 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