OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/paint_info.h" |
| 6 |
| 7 namespace views { |
| 8 |
| 9 // static |
| 10 PaintInfo PaintInfo::CreateRootPaintInfo(const ui::PaintContext& root_context, |
| 11 const gfx::Size& size) { |
| 12 return PaintInfo(root_context, size); |
| 13 } |
| 14 |
| 15 // static |
| 16 PaintInfo PaintInfo::CreateChildPaintInfo(const PaintInfo& parent_paint_info, |
| 17 const gfx::Rect& bounds, |
| 18 const gfx::Size& parent_size, |
| 19 ScaleType scale_type) { |
| 20 return PaintInfo(parent_paint_info, bounds, parent_size, scale_type); |
| 21 } |
| 22 |
| 23 // static |
| 24 PaintInfo PaintInfo::ClonePaintInfo(const PaintInfo& parent_paint_info) { |
| 25 return PaintInfo(parent_paint_info, |
| 26 ui::PaintContext::CLONE_WITHOUT_INVALIDATION); |
| 27 } |
| 28 |
| 29 PaintInfo::PaintInfo(const PaintInfo& other, |
| 30 ui::PaintContext::CloneWithoutInvalidation c) |
| 31 : paint_recording_scale_x_(other.paint_recording_scale_x_), |
| 32 paint_recording_scale_y_(other.paint_recording_scale_y_), |
| 33 paint_recording_bounds_(other.paint_recording_bounds_), |
| 34 offset_from_parent_(other.offset_from_parent_), |
| 35 context_(other.context(), c), |
| 36 root_context_(nullptr) {} |
| 37 |
| 38 PaintInfo::~PaintInfo() {} |
| 39 |
| 40 bool PaintInfo::IsPixelCanvas() const { |
| 41 return context().is_pixel_canvas(); |
| 42 } |
| 43 |
| 44 PaintInfo::PaintInfo(const PaintInfo& other) |
| 45 : paint_recording_scale_x_(other.paint_recording_scale_x_), |
| 46 paint_recording_scale_y_(other.paint_recording_scale_y_), |
| 47 paint_recording_bounds_(other.paint_recording_bounds_), |
| 48 offset_from_parent_(other.offset_from_parent_), |
| 49 context_(other.context(), gfx::Vector2d()), |
| 50 root_context_(nullptr) {} |
| 51 |
| 52 PaintInfo::PaintInfo(const ui::PaintContext& root_context, |
| 53 const gfx::Size& size) |
| 54 : paint_recording_scale_x_(root_context.is_pixel_canvas() |
| 55 ? root_context.device_scale_factor() |
| 56 : 1.f), |
| 57 paint_recording_scale_y_(paint_recording_scale_x_), |
| 58 paint_recording_bounds_( |
| 59 gfx::ScaleToRoundedRect(gfx::Rect(size), paint_recording_scale_x_)), |
| 60 context_(root_context, gfx::Vector2d()), |
| 61 root_context_(&root_context) {} |
| 62 |
| 63 PaintInfo::PaintInfo(const PaintInfo& parent_paint_info, |
| 64 const gfx::Rect& bounds, |
| 65 const gfx::Size& parent_size, |
| 66 ScaleType scale_type) |
| 67 : paint_recording_scale_x_(1.f), |
| 68 paint_recording_scale_y_(1.f), |
| 69 paint_recording_bounds_( |
| 70 parent_paint_info.GetSnappedRecordingBounds(parent_size, bounds)), |
| 71 offset_from_parent_( |
| 72 paint_recording_bounds_.OffsetFromOrigin() - |
| 73 parent_paint_info.paint_recording_bounds_.OffsetFromOrigin()), |
| 74 context_(parent_paint_info.context(), offset_from_parent_), |
| 75 root_context_(nullptr) { |
| 76 if (IsPixelCanvas()) { |
| 77 if (scale_type == ScaleType::kScaleToScaleFactor) { |
| 78 paint_recording_scale_x_ = paint_recording_scale_y_ = |
| 79 context().device_scale_factor(); |
| 80 } else if (scale_type == ScaleType::kScaleToFit) { |
| 81 if (bounds.size().width() > 0) { |
| 82 paint_recording_scale_x_ = |
| 83 static_cast<float>(paint_recording_bounds_.width()) / |
| 84 static_cast<float>(bounds.size().width()); |
| 85 } |
| 86 if (bounds.size().height() > 0) { |
| 87 paint_recording_scale_y_ = |
| 88 static_cast<float>(paint_recording_bounds_.height()) / |
| 89 static_cast<float>(bounds.size().height()); |
| 90 } |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 gfx::Rect PaintInfo::GetSnappedRecordingBounds( |
| 96 const gfx::Size& parent_size, |
| 97 const gfx::Rect& child_bounds) const { |
| 98 if (!IsPixelCanvas()) |
| 99 return (child_bounds + paint_recording_bounds_.OffsetFromOrigin()); |
| 100 |
| 101 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin(); |
| 102 |
| 103 int right = child_origin.x() + child_bounds.width(); |
| 104 int bottom = child_origin.y() + child_bounds.height(); |
| 105 |
| 106 int new_x = std::round(child_origin.x() * context().device_scale_factor()); |
| 107 int new_y = std::round(child_origin.y() * context().device_scale_factor()); |
| 108 |
| 109 int new_right; |
| 110 int new_bottom; |
| 111 |
| 112 if (right == parent_size.width()) |
| 113 new_right = paint_recording_bounds_.width(); |
| 114 else |
| 115 new_right = std::round(right * context().device_scale_factor()); |
| 116 |
| 117 if (bottom == parent_size.height()) |
| 118 new_bottom = paint_recording_bounds_.height(); |
| 119 else |
| 120 new_bottom = std::round(bottom * context().device_scale_factor()); |
| 121 |
| 122 return gfx::Rect(new_x + paint_recording_bounds_.x(), |
| 123 new_y + paint_recording_bounds_.y(), new_right - new_x, |
| 124 new_bottom - new_y); |
| 125 } |
| 126 |
| 127 } // namespace views |
OLD | NEW |