dip_util.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch// found in the LICENSE file.
4bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
5bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/compositor/dip_util.h"
6bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
7bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "base/command_line.h"
8bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "cc/layers/layer.h"
9bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/compositor/compositor.h"
10bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/compositor/compositor_switches.h"
114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/compositor/layer.h"
12bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/gfx/display.h"
13bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/gfx/geometry/safe_integer_conversions.h"
14bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/gfx/point.h"
15bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/gfx/point_conversions.h"
16bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/gfx/rect.h"
17#include "ui/gfx/rect_conversions.h"
18#include "ui/gfx/size.h"
19#include "ui/gfx/size_conversions.h"
20
21namespace ui {
22
23float GetDeviceScaleFactor(const Layer* layer) {
24  return layer->device_scale_factor();
25}
26
27gfx::Point ConvertPointToDIP(const Layer* layer,
28                             const gfx::Point& point_in_pixel) {
29  return gfx::ToFlooredPoint(
30      gfx::ScalePoint(point_in_pixel, 1.0f / GetDeviceScaleFactor(layer)));
31}
32
33gfx::PointF ConvertPointToDIP(const Layer* layer,
34                              const gfx::PointF& point_in_pixel) {
35  return gfx::ScalePoint(point_in_pixel, 1.0f / GetDeviceScaleFactor(layer));
36}
37
38gfx::Size ConvertSizeToDIP(const Layer* layer,
39                           const gfx::Size& size_in_pixel) {
40  return gfx::ToFlooredSize(
41      gfx::ScaleSize(size_in_pixel, 1.0f / GetDeviceScaleFactor(layer)));
42}
43
44gfx::Rect ConvertRectToDIP(const Layer* layer,
45                           const gfx::Rect& rect_in_pixel) {
46  float scale = 1.0f / GetDeviceScaleFactor(layer);
47  return gfx::ToFlooredRectDeprecated(gfx::ScaleRect(rect_in_pixel, scale));
48}
49
50gfx::Point ConvertPointToPixel(const Layer* layer,
51                               const gfx::Point& point_in_dip) {
52  return gfx::ToFlooredPoint(
53      gfx::ScalePoint(point_in_dip, GetDeviceScaleFactor(layer)));
54}
55
56gfx::Size ConvertSizeToPixel(const Layer* layer,
57                             const gfx::Size& size_in_dip) {
58  return gfx::ToFlooredSize(
59      gfx::ScaleSize(size_in_dip, GetDeviceScaleFactor(layer)));
60}
61
62gfx::Rect ConvertRectToPixel(const Layer* layer,
63                             const gfx::Rect& rect_in_dip) {
64  float scale = GetDeviceScaleFactor(layer);
65  // Use ToEnclosingRect() to ensure we paint all the possible pixels
66  // touched. ToEnclosingRect() floors the origin, and ceils the max
67  // coordinate. To do otherwise (such as flooring the size) potentially
68  // results in rounding down and not drawing all the pixels that are
69  // touched.
70  return gfx::ToEnclosingRect(
71      gfx::RectF(gfx::ScalePoint(rect_in_dip.origin(), scale),
72                 gfx::ScaleSize(rect_in_dip.size(), scale)));
73}
74
75#if !defined(NDEBUG)
76namespace {
77
78void CheckSnapped(float snapped_position) {
79  const float kEplison = 0.0001f;
80  float diff = std::abs(snapped_position - static_cast<int>(snapped_position));
81  DCHECK_LT(diff, kEplison);
82}
83
84}  // namespace
85#endif
86
87void SnapLayerToPhysicalPixelBoundary(ui::Layer* snapped_layer,
88                                      ui::Layer* layer_to_snap) {
89  DCHECK_NE(snapped_layer, layer_to_snap);
90  DCHECK(snapped_layer);
91  DCHECK(snapped_layer->Contains(layer_to_snap));
92
93  gfx::Point view_offset_dips = layer_to_snap->GetTargetBounds().origin();
94  ui::Layer::ConvertPointToLayer(
95      layer_to_snap->parent(), snapped_layer, &view_offset_dips);
96  gfx::PointF view_offset = view_offset_dips;
97
98  float scale_factor = GetDeviceScaleFactor(layer_to_snap);
99  view_offset.Scale(scale_factor);
100  gfx::PointF view_offset_snapped(gfx::ToRoundedInt(view_offset.x()),
101                                  gfx::ToRoundedInt(view_offset.y()));
102
103  gfx::Vector2dF fudge = view_offset_snapped - view_offset;
104  fudge.Scale(1.0 / scale_factor);
105  layer_to_snap->SetSubpixelPositionOffset(fudge);
106#if !defined(NDEBUG)
107  gfx::Point p;
108  Layer::ConvertPointToLayer(layer_to_snap->parent(), snapped_layer, &p);
109  cc::Layer* cc_layer = layer_to_snap->cc_layer();
110  gfx::PointF origin = cc_layer->position();
111  CheckSnapped((p.x() + origin.x()) * scale_factor);
112  CheckSnapped((p.y() + origin.y()) * scale_factor);
113#endif
114}
115
116}  // namespace ui
117