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 "content/browser/renderer_host/dip_util.h"
6
7 #include "content/public/browser/render_widget_host_view.h"
8 #include "ui/base/layout.h"
9 #include "ui/gfx/display.h"
10 #include "ui/gfx/point.h"
11 #include "ui/gfx/point_conversions.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/rect_conversions.h"
14 #include "ui/gfx/screen.h"
15 #include "ui/gfx/size.h"
16 #include "ui/gfx/size_conversions.h"
17
18 namespace content {
19
GetScaleFactorForView(const RenderWidgetHostView * view)20 float GetScaleFactorForView(const RenderWidgetHostView* view) {
21 return ui::GetScaleFactorForNativeView(view ? view->GetNativeView() : NULL);
22 }
23
ConvertViewPointToDIP(const RenderWidgetHostView * view,const gfx::Point & point_in_pixel)24 gfx::Point ConvertViewPointToDIP(const RenderWidgetHostView* view,
25 const gfx::Point& point_in_pixel) {
26 return gfx::ToFlooredPoint(
27 gfx::ScalePoint(point_in_pixel, 1.0f / GetScaleFactorForView(view)));
28 }
29
ConvertViewSizeToPixel(const RenderWidgetHostView * view,const gfx::Size & size_in_dip)30 gfx::Size ConvertViewSizeToPixel(const RenderWidgetHostView* view,
31 const gfx::Size& size_in_dip) {
32 return ConvertSizeToPixel(GetScaleFactorForView(view), size_in_dip);
33 }
34
ConvertViewRectToPixel(const RenderWidgetHostView * view,const gfx::Rect & rect_in_dip)35 gfx::Rect ConvertViewRectToPixel(const RenderWidgetHostView* view,
36 const gfx::Rect& rect_in_dip) {
37 return ConvertRectToPixel(GetScaleFactorForView(view), rect_in_dip);
38 }
39
ConvertSizeToDIP(float scale_factor,const gfx::Size & size_in_pixel)40 gfx::Size ConvertSizeToDIP(float scale_factor,
41 const gfx::Size& size_in_pixel) {
42 return gfx::ToFlooredSize(
43 gfx::ScaleSize(size_in_pixel, 1.0f / scale_factor));
44 }
45
ConvertRectToDIP(float scale_factor,const gfx::Rect & rect_in_pixel)46 gfx::Rect ConvertRectToDIP(float scale_factor,
47 const gfx::Rect& rect_in_pixel) {
48 return gfx::ToFlooredRectDeprecated(
49 gfx::ScaleRect(rect_in_pixel, 1.0f / scale_factor));
50 }
51
ConvertSizeToPixel(float scale_factor,const gfx::Size & size_in_dip)52 gfx::Size ConvertSizeToPixel(float scale_factor,
53 const gfx::Size& size_in_dip) {
54 return gfx::ToFlooredSize(gfx::ScaleSize(size_in_dip, scale_factor));
55 }
56
ConvertRectToPixel(float scale_factor,const gfx::Rect & rect_in_dip)57 gfx::Rect ConvertRectToPixel(float scale_factor,
58 const gfx::Rect& rect_in_dip) {
59 return gfx::ToFlooredRectDeprecated(
60 gfx::ScaleRect(rect_in_dip, scale_factor));
61 }
62
63 } // namespace content
64