1 // Copyright (c) 2013 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 "remoting/host/screen_resolution.h"
6 
7 #include <stdint.h>
8 
9 #include <algorithm>
10 #include <limits>
11 
12 #include "base/check_op.h"
13 
14 namespace remoting {
15 
ScreenResolution()16 ScreenResolution::ScreenResolution()
17     : dimensions_(webrtc::DesktopSize(0, 0)),
18       dpi_(webrtc::DesktopVector(0, 0)) {
19 }
20 
ScreenResolution(const webrtc::DesktopSize & dimensions,const webrtc::DesktopVector & dpi)21 ScreenResolution::ScreenResolution(const webrtc::DesktopSize& dimensions,
22                                    const webrtc::DesktopVector& dpi)
23     : dimensions_(dimensions),
24       dpi_(dpi) {
25   // Check that dimensions are not negative.
26   DCHECK(!dimensions.is_empty() || dimensions.equals(webrtc::DesktopSize()));
27   DCHECK_GE(dpi.x(), 0);
28   DCHECK_GE(dpi.y(), 0);
29 }
30 
ScaleDimensionsToDpi(const webrtc::DesktopVector & new_dpi) const31 webrtc::DesktopSize ScreenResolution::ScaleDimensionsToDpi(
32     const webrtc::DesktopVector& new_dpi) const {
33   int64_t width = dimensions_.width();
34   int64_t height = dimensions_.height();
35 
36   // Scale the screen dimensions to new DPI.
37   width = std::min(width * new_dpi.x() / dpi_.x(),
38                    static_cast<int64_t>(std::numeric_limits<int32_t>::max()));
39   height = std::min(height * new_dpi.y() / dpi_.y(),
40                     static_cast<int64_t>(std::numeric_limits<int32_t>::max()));
41   return webrtc::DesktopSize(width, height);
42 }
43 
IsEmpty() const44 bool ScreenResolution::IsEmpty() const {
45   return dimensions_.is_empty() || dpi_.is_zero();
46 }
47 
Equals(const ScreenResolution & other) const48 bool ScreenResolution::Equals(const ScreenResolution& other) const {
49   return dimensions_.equals(other.dimensions()) && dpi_.equals(other.dpi());
50 }
51 
52 }  // namespace remoting
53