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
3// found in the LICENSE file.
4
5#include "ui/compositor/dip_util.h"
6
7#include "base/command_line.h"
8#include "ui/compositor/compositor.h"
9#include "ui/compositor/compositor_switches.h"
10#include "ui/compositor/layer.h"
11#include "ui/gfx/display.h"
12#include "ui/gfx/point.h"
13#include "ui/gfx/point_conversions.h"
14#include "ui/gfx/rect.h"
15#include "ui/gfx/rect_conversions.h"
16#include "ui/gfx/size.h"
17#include "ui/gfx/size_conversions.h"
18
19namespace ui {
20
21float GetDeviceScaleFactor(const Layer* layer) {
22  return layer->device_scale_factor();
23}
24
25gfx::Point ConvertPointToDIP(const Layer* layer,
26                             const gfx::Point& point_in_pixel) {
27  return gfx::ToFlooredPoint(
28      gfx::ScalePoint(point_in_pixel, 1.0f / GetDeviceScaleFactor(layer)));
29}
30
31gfx::PointF ConvertPointToDIP(const Layer* layer,
32                              const gfx::PointF& point_in_pixel) {
33  return gfx::ScalePoint(point_in_pixel, 1.0f / GetDeviceScaleFactor(layer));
34}
35
36gfx::Size ConvertSizeToDIP(const Layer* layer,
37                           const gfx::Size& size_in_pixel) {
38  return gfx::ToFlooredSize(
39      gfx::ScaleSize(size_in_pixel, 1.0f / GetDeviceScaleFactor(layer)));
40}
41
42gfx::Rect ConvertRectToDIP(const Layer* layer,
43                           const gfx::Rect& rect_in_pixel) {
44  float scale = 1.0f / GetDeviceScaleFactor(layer);
45  return gfx::ToFlooredRectDeprecated(gfx::ScaleRect(rect_in_pixel, scale));
46}
47
48gfx::Point ConvertPointToPixel(const Layer* layer,
49                               const gfx::Point& point_in_dip) {
50  return gfx::ToFlooredPoint(
51      gfx::ScalePoint(point_in_dip, GetDeviceScaleFactor(layer)));
52}
53
54gfx::Size ConvertSizeToPixel(const Layer* layer,
55                             const gfx::Size& size_in_dip) {
56  return gfx::ToFlooredSize(
57      gfx::ScaleSize(size_in_dip, GetDeviceScaleFactor(layer)));
58}
59
60gfx::Rect ConvertRectToPixel(const Layer* layer,
61                             const gfx::Rect& rect_in_dip) {
62  float scale = GetDeviceScaleFactor(layer);
63  // Use ToEnclosingRect() to ensure we paint all the possible pixels
64  // touched. ToEnclosingRect() floors the origin, and ceils the max
65  // coordinate. To do otherwise (such as flooring the size) potentially
66  // results in rounding down and not drawing all the pixels that are
67  // touched.
68  return gfx::ToEnclosingRect(
69      gfx::RectF(gfx::ScalePoint(rect_in_dip.origin(), scale),
70                 gfx::ScaleSize(rect_in_dip.size(), scale)));
71}
72
73}  // namespace ui
74