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

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

Issue 1053143002: Make View::Paint use ui::PaintRecorder to access PaintContext's canvas (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: paintrecorder: . Created 5 years, 8 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>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/trace_event/trace_event.h" 17 #include "base/trace_event/trace_event.h"
18 #include "third_party/skia/include/core/SkRect.h" 18 #include "third_party/skia/include/core/SkRect.h"
19 #include "ui/accessibility/ax_enums.h" 19 #include "ui/accessibility/ax_enums.h"
20 #include "ui/base/cursor/cursor.h" 20 #include "ui/base/cursor/cursor.h"
21 #include "ui/base/dragdrop/drag_drop_types.h" 21 #include "ui/base/dragdrop/drag_drop_types.h"
22 #include "ui/compositor/compositor.h" 22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/dip_util.h" 23 #include "ui/compositor/dip_util.h"
24 #include "ui/compositor/layer.h" 24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/layer_animator.h" 25 #include "ui/compositor/layer_animator.h"
26 #include "ui/compositor/paint_context.h"
27 #include "ui/compositor/paint_recorder.h"
26 #include "ui/events/event_target_iterator.h" 28 #include "ui/events/event_target_iterator.h"
27 #include "ui/gfx/canvas.h" 29 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/geometry/point3_f.h" 30 #include "ui/gfx/geometry/point3_f.h"
29 #include "ui/gfx/geometry/point_conversions.h" 31 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/interpolated_transform.h" 32 #include "ui/gfx/interpolated_transform.h"
31 #include "ui/gfx/path.h" 33 #include "ui/gfx/path.h"
32 #include "ui/gfx/scoped_canvas.h" 34 #include "ui/gfx/scoped_canvas.h"
33 #include "ui/gfx/screen.h" 35 #include "ui/gfx/screen.h"
34 #include "ui/gfx/skia_util.h" 36 #include "ui/gfx/skia_util.h"
35 #include "ui/gfx/transform.h" 37 #include "ui/gfx/transform.h"
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 726
725 if (layer()) { 727 if (layer()) {
726 layer()->SchedulePaint(rect); 728 layer()->SchedulePaint(rect);
727 } else if (parent_) { 729 } else if (parent_) {
728 // Translate the requested paint rect to the parent's coordinate system 730 // Translate the requested paint rect to the parent's coordinate system
729 // then pass this notification up to the parent. 731 // then pass this notification up to the parent.
730 parent_->SchedulePaintInRect(ConvertRectToParent(rect)); 732 parent_->SchedulePaintInRect(ConvertRectToParent(rect));
731 } 733 }
732 } 734 }
733 735
734 void View::Paint(const PaintContext& parent_context) { 736 void View::Paint(const ui::PaintContext& parent_context) {
735 if (!visible_) 737 if (!visible_)
736 return; 738 return;
737 739
738 gfx::Vector2d offset_to_parent; 740 gfx::Vector2d offset_to_parent;
739 if (!layer()) { 741 if (!layer()) {
740 // If the View has a layer() then it is a paint root. Otherwise, we need to 742 // If the View has a layer() then it is a paint root. Otherwise, we need to
741 // add the offset from the parent into the total offset from the paint root. 743 // add the offset from the parent into the total offset from the paint root.
742 DCHECK_IMPLIES(!parent(), bounds().origin() == gfx::Point()); 744 DCHECK_IMPLIES(!parent(), bounds().origin() == gfx::Point());
743 offset_to_parent = GetMirroredPosition().OffsetFromOrigin(); 745 offset_to_parent = GetMirroredPosition().OffsetFromOrigin();
744 } 746 }
745 PaintContext context = parent_context.CloneWithPaintOffset(offset_to_parent); 747 ui::PaintContext context =
748 parent_context.CloneWithPaintOffset(offset_to_parent);
746 749
747 if (context.CanCheckInvalidated()) { 750 if (context.CanCheckInvalidated()) {
748 #if DCHECK_IS_ON() 751 #if DCHECK_IS_ON()
749 gfx::Vector2d offset; 752 gfx::Vector2d offset;
750 context.Visited(this); 753 context.Visited(this);
751 View* view = this; 754 View* view = this;
752 while (view->parent() && !view->layer()) { 755 while (view->parent() && !view->layer()) {
753 DCHECK(view->GetTransform().IsIdentity()); 756 DCHECK(view->GetTransform().IsIdentity());
754 offset += view->GetMirroredPosition().OffsetFromOrigin(); 757 offset += view->GetMirroredPosition().OffsetFromOrigin();
755 view = view->parent(); 758 view = view->parent();
756 } 759 }
757 // The offset in the PaintContext should be the offset up to the paint root, 760 // The offset in the PaintContext should be the offset up to the paint root,
758 // which we compute and verify here. 761 // which we compute and verify here.
759 DCHECK_EQ(context.PaintOffset().x(), offset.x()); 762 DCHECK_EQ(context.PaintOffset().x(), offset.x());
760 DCHECK_EQ(context.PaintOffset().y(), offset.y()); 763 DCHECK_EQ(context.PaintOffset().y(), offset.y());
761 // The above loop will stop when |view| is the paint root, which should be 764 // The above loop will stop when |view| is the paint root, which should be
762 // the root of the current paint walk, as verified by storing the root in 765 // the root of the current paint walk, as verified by storing the root in
763 // the PaintContext. 766 // the PaintContext.
764 DCHECK_EQ(context.RootVisited(), view); 767 DCHECK_EQ(context.RootVisited(), view);
765 #endif 768 #endif
766 769
767 // If the View wasn't invalidated, don't waste time painting it, the output 770 // If the View wasn't invalidated, don't waste time painting it, the output
768 // would be culled. 771 // would be culled.
769 if (!context.IsRectInvalidated(GetLocalBounds())) 772 if (!context.IsRectInvalidated(GetLocalBounds()))
770 return; 773 return;
771 } 774 }
772 775
773 TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); 776 TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
774 777
775 gfx::Canvas* canvas = context.canvas();
776 gfx::ScopedCanvas scoped_canvas(canvas);
777
778 // If the view is backed by a layer, it should paint with itself as the origin 778 // If the view is backed by a layer, it should paint with itself as the origin
779 // rather than relative to its parent. 779 // rather than relative to its parent.
780 if (!layer()) { 780 bool paint_relative_to_parent = !layer();
781 // Set the clip rect to the bounds of this View and translating the origin 781 if (paint_relative_to_parent) {
782 // to the local bounds' top left point. 782 // Set the clip rect to the bounds of this View. Note that the X (or left)
783 // 783 // position we pass to ClipRect takes into consideration whether or not the
784 // Note that the X (or left) position we pass to ClipRectInt takes into 784 // View uses a right-to-left layout so that we paint the View in its
785 // consideration whether or not the view uses a right-to-left layout so that 785 // mirrored position if need be.
786 // we paint our view in its mirrored position if need be. 786 ui::PaintRecorder recorder(context);
787 gfx::Canvas* canvas = recorder.canvas();
788 canvas->Save();
789
787 gfx::Rect clip_rect = bounds(); 790 gfx::Rect clip_rect = bounds();
788 clip_rect.Inset(clip_insets_); 791 clip_rect.Inset(clip_insets_);
789 if (parent_) 792 if (parent_)
790 clip_rect.set_x(parent_->GetMirroredXForRect(clip_rect)); 793 clip_rect.set_x(parent_->GetMirroredXForRect(clip_rect));
791 canvas->ClipRect(clip_rect); 794 canvas->ClipRect(clip_rect);
792 if (canvas->IsClipEmpty())
793 return;
794 795
795 // Non-empty clip, translate the graphics such that 0,0 corresponds to where 796 // Translate the graphics such that 0,0 corresponds to where
796 // this view is located (related to its parent). 797 // this View is located relative to its parent.
797 canvas->Translate(GetMirroredPosition().OffsetFromOrigin()); 798 canvas->Translate(GetMirroredPosition().OffsetFromOrigin());
798 canvas->Transform(GetTransform()); 799 canvas->Transform(GetTransform());
799 } 800 }
800 801
801 { 802 {
803 ui::PaintRecorder recorder(context);
804 gfx::Canvas* canvas = recorder.canvas();
805 gfx::ScopedCanvas scoped_canvas(canvas);
806
802 // If the View we are about to paint requested the canvas to be flipped, we 807 // If the View we are about to paint requested the canvas to be flipped, we
803 // should change the transform appropriately. 808 // should change the transform appropriately.
804 // The canvas mirroring is undone once the View is done painting so that we 809 // The canvas mirroring is undone once the View is done painting so that we
805 // don't pass the canvas with the mirrored transform to Views that didn't 810 // don't pass the canvas with the mirrored transform to Views that didn't
806 // request the canvas to be flipped. 811 // request the canvas to be flipped.
807 gfx::ScopedCanvas scoped(canvas);
808 if (FlipCanvasOnPaintForRTLUI()) { 812 if (FlipCanvasOnPaintForRTLUI()) {
809 canvas->Translate(gfx::Vector2d(width(), 0)); 813 canvas->Translate(gfx::Vector2d(width(), 0));
810 canvas->Scale(-1, 1); 814 canvas->Scale(-1, 1);
811 } 815 }
812 816
813 // Delegate painting the contents of the View to the virtual OnPaint method. 817 // Delegate painting the contents of the View to the virtual OnPaint method.
814 OnPaint(canvas); 818 OnPaint(canvas);
815 } 819 }
816 820
821 // View::Paint recursion over the subtree.
817 PaintChildren(context); 822 PaintChildren(context);
823
824 if (paint_relative_to_parent) {
825 ui::PaintRecorder recorder(context);
sky 2015/04/02 23:43:09 Why do you need this logic?
danakj 2015/04/03 00:27:36 This matches the save on l.788. We can't use Scope
sky 2015/04/03 14:53:59 Tricky, but error prone. How about a scoped_ptr<Sc
danakj 2015/04/03 16:15:35 The Canvas* here and at l786 are going to be diffe
826 recorder.canvas()->Restore();
827 }
818 } 828 }
819 829
820 void View::set_background(Background* b) { 830 void View::set_background(Background* b) {
821 background_.reset(b); 831 background_.reset(b);
822 } 832 }
823 833
824 void View::SetBorder(scoped_ptr<Border> b) { border_ = b.Pass(); } 834 void View::SetBorder(scoped_ptr<Border> b) { border_ = b.Pass(); }
825 835
826 ui::ThemeProvider* View::GetThemeProvider() const { 836 ui::ThemeProvider* View::GetThemeProvider() const {
827 const Widget* widget = GetWidget(); 837 const Widget* widget = GetWidget();
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 if (accelerator_focus_manager_ != focus_manager) { 1342 if (accelerator_focus_manager_ != focus_manager) {
1333 UnregisterAccelerators(true); 1343 UnregisterAccelerators(true);
1334 1344
1335 if (focus_manager) 1345 if (focus_manager)
1336 RegisterPendingAccelerators(); 1346 RegisterPendingAccelerators();
1337 } 1347 }
1338 } 1348 }
1339 1349
1340 // Painting -------------------------------------------------------------------- 1350 // Painting --------------------------------------------------------------------
1341 1351
1342 void View::PaintChildren(const PaintContext& context) { 1352 void View::PaintChildren(const ui::PaintContext& context) {
1343 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); 1353 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
1344 for (int i = 0, count = child_count(); i < count; ++i) 1354 for (int i = 0, count = child_count(); i < count; ++i)
1345 if (!child_at(i)->layer()) 1355 if (!child_at(i)->layer())
1346 child_at(i)->Paint(context); 1356 child_at(i)->Paint(context);
1347 } 1357 }
1348 1358
1349 void View::OnPaint(gfx::Canvas* canvas) { 1359 void View::OnPaint(gfx::Canvas* canvas) {
1350 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); 1360 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
1351 OnPaintBackground(canvas); 1361 OnPaintBackground(canvas);
1352 OnPaintBorder(canvas); 1362 OnPaintBorder(canvas);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1442 SetLayerBounds(GetLocalBounds() + offset); 1452 SetLayerBounds(GetLocalBounds() + offset);
1443 } else { 1453 } else {
1444 for (int i = 0, count = child_count(); i < count; ++i) { 1454 for (int i = 0, count = child_count(); i < count; ++i) {
1445 View* child = child_at(i); 1455 View* child = child_at(i);
1446 child->UpdateChildLayerBounds( 1456 child->UpdateChildLayerBounds(
1447 offset + gfx::Vector2d(child->GetMirroredX(), child->y())); 1457 offset + gfx::Vector2d(child->GetMirroredX(), child->y()));
1448 } 1458 }
1449 } 1459 }
1450 } 1460 }
1451 1461
1452 void View::OnPaintLayer(gfx::Canvas* canvas) { 1462 void View::OnPaintLayer(const ui::PaintContext& context) {
1453 if (!layer()->fills_bounds_opaquely()) 1463 if (!layer()->fills_bounds_opaquely())
1454 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode); 1464 context.canvas()->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
1455 if (!visible_) 1465 if (!visible_)
1456 return; 1466 return;
1457 Paint(PaintContext(canvas, layer()->PaintRect())); 1467 Paint(context);
1458 } 1468 }
1459 1469
1460 void View::OnDelegatedFrameDamage( 1470 void View::OnDelegatedFrameDamage(
1461 const gfx::Rect& damage_rect_in_dip) { 1471 const gfx::Rect& damage_rect_in_dip) {
1462 } 1472 }
1463 1473
1464 void View::OnDeviceScaleFactorChanged(float device_scale_factor) { 1474 void View::OnDeviceScaleFactorChanged(float device_scale_factor) {
1465 snap_layer_to_pixel_boundary_ = 1475 snap_layer_to_pixel_boundary_ =
1466 (device_scale_factor - std::floor(device_scale_factor)) != 0.0f; 1476 (device_scale_factor - std::floor(device_scale_factor)) != 0.0f;
1467 SnapLayerToPixelBoundary(); 1477 SnapLayerToPixelBoundary();
(...skipping 890 matching lines...) Expand 10 before | Expand all | Expand 10 after
2358 // Message the RootView to do the drag and drop. That way if we're removed 2368 // Message the RootView to do the drag and drop. That way if we're removed
2359 // the RootView can detect it and avoid calling us back. 2369 // the RootView can detect it and avoid calling us back.
2360 gfx::Point widget_location(event.location()); 2370 gfx::Point widget_location(event.location());
2361 ConvertPointToWidget(this, &widget_location); 2371 ConvertPointToWidget(this, &widget_location);
2362 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2372 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2363 // WARNING: we may have been deleted. 2373 // WARNING: we may have been deleted.
2364 return true; 2374 return true;
2365 } 2375 }
2366 2376
2367 } // namespace views 2377 } // namespace views
OLDNEW
« ui/compositor/paint_recorder.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