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

Unified Diff: ui/compositor/paint_info.cc

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Update tests 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 side-by-side diff with in-line comments
Download patch
Index: ui/compositor/paint_info.cc
diff --git a/ui/compositor/paint_info.cc b/ui/compositor/paint_info.cc
new file mode 100644
index 0000000000000000000000000000000000000000..425273d595fe4e3999cecca783e20c004b7c86c5
--- /dev/null
+++ b/ui/compositor/paint_info.cc
@@ -0,0 +1,109 @@
+// Copyright (c) 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/compositor/paint_info.h"
+
+#include "ui/compositor/compositor_switches.h"
+
+namespace ui {
+
+PaintInfo::PaintInfo(const PaintContext& context, const gfx::Size& size)
+ : paint_recording_scale_x_(IsPixelCanvas() ? context.device_scale_factor()
+ : 1.f),
+ paint_recording_scale_y_(paint_recording_scale_x_),
+ paint_recording_bounds_(
+ gfx::ScaleToRoundedRect(gfx::Rect(size), paint_recording_scale_x_)),
danakj 2017/07/21 17:29:39 Rounding is almost always not what you want to do
malaykeshav 2017/07/21 23:30:17 That's true. But if we round at every location in
danakj 2017/07/25 17:58:56 Not finding bugs easily isn't surprising, they wou
malaykeshav 2017/07/25 22:57:57 For pixel canvas we are only modifying the scaling
+ context_(context, gfx::Vector2d()),
+ root_context_(&context) {}
+
+PaintInfo::PaintInfo(const PaintInfo& other,
+ const gfx::Rect& bounds,
+ const gfx::Rect& parent_bounds,
+ ScaleType scale_type)
+ : paint_recording_bounds_(
+ other.GetSnappedRecordingBounds(parent_bounds, bounds)),
+ offset_from_parent_(paint_recording_bounds_.OffsetFromOrigin() -
+ other.paint_recording_bounds_.OffsetFromOrigin()),
+ context_(other.context(), offset_from_parent_),
+ root_context_(nullptr) {
+ ComputePaintRecordingScales(bounds.size(), scale_type);
+}
+
+PaintInfo::PaintInfo(const PaintInfo& other)
+ : paint_recording_scale_x_(other.paint_recording_scale_x_),
+ paint_recording_scale_y_(other.paint_recording_scale_y_),
+ paint_recording_bounds_(other.paint_recording_bounds_),
+ offset_from_parent_(other.offset_from_parent_),
+ context_(other.context(), ui::PaintContext::CLONE_WITHOUT_INVALIDATION),
+ root_context_(nullptr) {}
+
+PaintInfo::~PaintInfo() {}
+
+// static
+bool PaintInfo::IsPixelCanvas() {
+ return IsPixelCanvasRecordingEnabled();
danakj 2017/07/21 17:29:39 pass this in the constructor, instead of reading t
malaykeshav 2017/07/21 23:30:17 Done. Passing it to PaintContext should propagate
+}
+
+void PaintInfo::ComputePaintRecordingScales(const gfx::Size& size,
+ ScaleType scale_type) {
+ if (!IsPixelCanvas()) {
+ // If pixel canvas is disabled, all recordings are done in DIPs. These are
+ // later played back on to the raster canvas at |device_scale_factor_|.
+ paint_recording_scale_x_ = paint_recording_scale_y_ = 1.f;
+ return;
+ }
+
+ paint_recording_scale_x_ = paint_recording_scale_y_ =
+ context().device_scale_factor();
+ if (scale_type == ScaleType::kScaleToScaleFactor)
+ return;
+
+ if (size.width() > 0) {
+ paint_recording_scale_x_ =
+ static_cast<float>(paint_recording_bounds_.width()) /
+ static_cast<float>(size.width());
+ }
+
+ if (size.height() > 0) {
+ paint_recording_scale_y_ =
+ static_cast<float>(paint_recording_bounds_.height()) /
+ static_cast<float>(size.height());
+ }
+}
+
+gfx::Rect PaintInfo::GetSnappedRecordingBounds(
+ const gfx::Rect& parent_bounds,
+ const gfx::Rect& child_bounds) const {
+ if (!IsPixelCanvas())
+ return (child_bounds + paint_recording_bounds_.OffsetFromOrigin());
+
+ const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin();
+
+ int right = child_origin.x() + child_bounds.width();
+ int bottom = child_origin.y() + child_bounds.height();
+
+ int new_x = std::round(child_origin.x() * context().device_scale_factor());
+ int new_y = std::round(child_origin.y() * context().device_scale_factor());
+
+ int new_right;
+ int new_bottom;
+
+ if (right == parent_bounds.width()) {
+ new_right = paint_recording_bounds_.width();
+ } else {
+ new_right = std::round(right * context().device_scale_factor());
+ }
+
+ if (bottom == parent_bounds.height()) {
+ new_bottom = paint_recording_bounds_.height();
+ } else {
+ new_bottom = std::round(bottom * context().device_scale_factor());
+ }
+
+ return gfx::Rect(new_x + paint_recording_bounds_.x(),
+ new_y + paint_recording_bounds_.y(), new_right - new_x,
+ new_bottom - new_y);
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698