OLD | NEW |
---|---|
(Empty) | |
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 "remoting/host/resizing_host_observer.h" | |
6 #include "remoting/host/desktop_resizer.h" | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/logging.h" | |
11 | |
12 namespace remoting { | |
13 | |
14 ResizingHostObserver::ResizingHostObserver( | |
15 DesktopResizer* desktop_resizer, ChromotingHost* host) | |
16 : desktop_resizer_(desktop_resizer), | |
17 host_(host), | |
18 original_size_(SkISize::Make(0, 0)), | |
19 previous_size_(SkISize::Make(0, 0)) { | |
20 if (host_) { | |
21 host_->AddStatusObserver(this); | |
22 } | |
23 } | |
24 | |
25 ResizingHostObserver::~ResizingHostObserver() { | |
26 if (host_) { | |
27 host_->RemoveStatusObserver(this); | |
28 } | |
29 } | |
30 | |
31 void ResizingHostObserver::OnClientAuthenticated(const std::string& jid) { | |
32 // Resize-to-client doesn't make sense if multiple clients are connected, | |
33 // and this is not currently supported anyway. | |
Wez
2012/09/25 20:48:29
nit: Suggest "This implementation assumes a single
Jamie
2012/09/25 23:20:20
Done.
| |
34 DCHECK(client_jid_.empty()); | |
35 original_size_ = desktop_resizer_->GetCurrentSize(); | |
36 previous_size_ = original_size_; | |
37 } | |
38 | |
39 void ResizingHostObserver::OnClientDisconnected(const std::string& jid) { | |
40 if (!original_size_.isZero()) { | |
41 desktop_resizer_->RestoreSize(original_size_); | |
42 original_size_.set(0, 0); | |
43 } | |
44 client_jid_.clear(); | |
45 } | |
46 | |
47 class CandidateSize { | |
48 public: | |
49 CandidateSize(const SkISize& candidate, const SkISize& preferred) | |
50 : size_(candidate) { | |
51 // The client scale factor is the smaller of the candidate:preferred ratios | |
52 // for width and height. | |
53 if ((candidate.width() > preferred.width()) || | |
54 (candidate.height() > preferred.height())) { | |
55 float ratio_width = | |
56 static_cast<float>(preferred.width()) / candidate.width(); | |
57 float ratio_height = | |
58 static_cast<float>(preferred.height()) / candidate.height(); | |
59 client_scale_factor_ = std::min(ratio_width, ratio_height); | |
60 } else { | |
61 // Since clients do not scale up, 1.0 is the maximum. | |
62 client_scale_factor_ = 1.0; | |
63 } | |
64 | |
65 // The aspect ratio "goodness" is defined as being the ratio of the smaller | |
66 // of the two aspect ratios (candidate and preferred) to the larger. The | |
67 // best aspect ratio is the one that most closely matches the preferred | |
68 // aspect ratio (in other words, the ideal aspect ratio "goodness" is 1.0). | |
69 // By keeping the values < 1.0, it allows ratios the differ in opposite | |
70 // directions to be compared numerically. | |
71 float candidate_aspect_ratio = | |
72 static_cast<float>(candidate.width()) / candidate.height(); | |
73 float preferred_aspect_ratio = | |
74 static_cast<float>(preferred.width()) / preferred.height(); | |
75 if (candidate_aspect_ratio > preferred_aspect_ratio) { | |
76 aspect_ratio_goodness_ = preferred_aspect_ratio / candidate_aspect_ratio; | |
77 } else { | |
78 aspect_ratio_goodness_ = candidate_aspect_ratio / preferred_aspect_ratio; | |
79 } | |
80 } | |
81 | |
82 const SkISize& size() const { return size_; } | |
83 float client_scale_factor() const { return client_scale_factor_; } | |
84 float aspect_ratio_goodness() const { return aspect_ratio_goodness_; } | |
85 int64 area() const { | |
86 return static_cast<int64>(size_.width()) * size_.height(); | |
87 } | |
88 | |
89 bool IsBetterThan(const CandidateSize& other) const { | |
90 // If either size would require down-scaling, prefer the one that down- | |
91 // scales the least (since the client scale factor is at most 1.0, this | |
92 // does not differentiate between sizes that don't require down-scaling). | |
93 if (client_scale_factor() < other.client_scale_factor()) { | |
94 return false; | |
95 } else if (client_scale_factor() > other.client_scale_factor()) { | |
96 return true; | |
97 } | |
98 | |
99 // If the scale factors are the same, pick the size with the largest area. | |
100 if (area() < other.area()) { | |
101 return false; | |
102 } else if (area() > other.area()) { | |
103 return true; | |
104 } | |
105 | |
106 // If the areas are equal, pick the size with the "best" aspect ratio. | |
107 if (aspect_ratio_goodness() < other.aspect_ratio_goodness()) { | |
108 return false; | |
109 } else if (aspect_ratio_goodness() > other.aspect_ratio_goodness()) { | |
110 return true; | |
111 } | |
112 | |
113 // If the aspect ratios are equally good (for example, comparing 640x480 | |
114 // to 480x640 w.r.t. 640x640), just pick the widest, since desktop UIs | |
115 // are typically design for landscape aspect ratios. | |
116 return size().width() > other.size().width(); | |
117 } | |
118 | |
119 private: | |
120 float client_scale_factor_; | |
121 float aspect_ratio_goodness_; | |
122 SkISize size_; | |
123 }; | |
124 | |
125 void ResizingHostObserver::OnClientDimensionsChanged( | |
126 const std::string& jid, const SkISize& preferred_size) { | |
127 // If the host desktop size changes other than via the resize-to-client | |
128 // mechanism, then set |previous_size_| to zero and give up. This is an | |
129 // indication that the user doesn't want resize-to-client. | |
130 if (previous_size_.isZero()) { | |
Wez
2012/09/25 20:48:29
nit: I'd prefer that this check be above this comm
Jamie
2012/09/25 23:20:20
I want to do this check first because it avoids an
| |
131 return; | |
132 } | |
133 SkISize new_size = desktop_resizer_->GetCurrentSize(); | |
134 if (new_size != previous_size_) { | |
135 previous_size_ = SkISize::Make(0, 0); | |
136 return; | |
137 } | |
138 | |
139 // If the implementation returns any sizes, pick the best one according to | |
140 // the algorithm described in CandidateSize::IsBetterThen. | |
141 std::list<SkISize> sizes = | |
142 desktop_resizer_->GetSupportedSizes(preferred_size); | |
143 if (sizes.empty()) { | |
144 return; | |
145 } | |
146 CandidateSize best_size(sizes.front(), preferred_size); | |
147 std::list<SkISize>::iterator i = sizes.begin(); | |
148 for (++i; i != sizes.end(); ++i) { | |
149 CandidateSize candidate_size(*i, preferred_size); | |
150 if (candidate_size.IsBetterThan(best_size)) { | |
151 best_size = candidate_size; | |
152 } | |
153 } | |
154 previous_size_ = best_size.size(); | |
155 desktop_resizer_->SetSize(previous_size_); | |
156 } | |
157 | |
158 } // namespace remoting | |
OLD | NEW |