Chromium Code Reviews| 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) | |
| 16 : desktop_resizer_(desktop_resizer), | |
| 17 original_size_(SkISize::Make(0, 0)) { | |
| 18 } | |
| 19 | |
| 20 ResizingHostObserver::~ResizingHostObserver() { | |
| 21 } | |
| 22 | |
| 23 void ResizingHostObserver::OnClientAuthenticated(const std::string& jid) { | |
| 24 // Resize-to-client doesn't make sense if multiple clients are connected, | |
| 25 // and this is not currently supported anyway. | |
| 26 DCHECK(client_jid_.empty()); | |
| 27 original_size_ = desktop_resizer_->GetCurrentSize(); | |
| 28 } | |
| 29 | |
| 30 void ResizingHostObserver::OnClientDisconnected(const std::string& jid) { | |
| 31 if (!original_size_.isZero()) { | |
|
Wez
2012/09/20 21:35:15
nit: isZero -> isEmpty
Jamie
2012/09/20 22:59:59
Done.
| |
| 32 desktop_resizer_->SetSize(original_size_); | |
|
Wez
2012/09/20 21:35:15
This will re-set the size the host had when the cl
Jamie
2012/09/20 22:59:59
I don't think it's obvious that the user setting a
Wez
2012/09/21 00:59:32
I can see situations where both expectations are v
Jamie
2012/09/24 22:49:15
That's a good point. I've added an explicit Restor
| |
| 33 original_size_.set(0, 0); | |
| 34 } | |
| 35 client_jid_.clear(); | |
| 36 } | |
| 37 | |
| 38 class SupportedSize { | |
|
Wez
2012/09/20 21:35:15
SupportedSize -> CandidateSize
Jamie
2012/09/20 22:59:59
Done.
| |
| 39 public: | |
| 40 SupportedSize(const SkISize& actual, const SkISize& preferred) | |
| 41 : size_(actual) { | |
| 42 // The client scale factor is the smaller of the ratios between the actual | |
| 43 // and preferred widths and heights. | |
|
Wez
2012/09/20 21:35:15
nit: Referring to ratio is confusing here, since o
Wez
2012/09/20 21:35:15
nit: You're clamping the scale factor at 1.0 to av
Jamie
2012/09/20 22:59:59
I think it's clearer to use ratio for the individu
Jamie
2012/09/20 22:59:59
(Assuming you mean "more readable NOT to cap here"
Wez
2012/09/21 00:59:32
Actually, I think swapping "actual" for "supported
Jamie
2012/09/24 22:49:15
I've gone with |candidate|, since that's the new n
| |
| 44 if ((actual.width() > preferred.width()) || | |
| 45 (actual.height() > preferred.height())) { | |
| 46 float ratio_width = | |
| 47 static_cast<float>(preferred.width()) / actual.width(); | |
| 48 float ratio_height = | |
| 49 static_cast<float>(preferred.height()) / actual.height(); | |
| 50 client_scale_factor_ = std::min(ratio_width, ratio_height); | |
| 51 } else { | |
| 52 client_scale_factor_ = 1.0; | |
| 53 } | |
| 54 // The aspect ratio "goodness" is defined as being the ratio of the smaller | |
|
Wez
2012/09/20 21:35:15
nit: blank line before this comment
Wez
2012/09/20 21:35:15
Explain that we the best aspect ratio is the one t
Jamie
2012/09/20 22:59:59
Done.
Jamie
2012/09/20 22:59:59
Done.
| |
| 55 // of the actual and preferred aspect ratios to the larger. It allows two | |
| 56 // sizes to be easily compared in terms of how well the match a requested | |
| 57 // aspect ratio. | |
| 58 float actual_aspect_ratio = | |
| 59 static_cast<float>(actual.width()) / actual.height(); | |
| 60 float preferred_aspect_ratio = | |
| 61 static_cast<float>(preferred.width()) / preferred.height(); | |
| 62 if (actual_aspect_ratio > preferred_aspect_ratio) { | |
| 63 aspect_ratio_goodness_ = preferred_aspect_ratio / actual_aspect_ratio; | |
| 64 } else { | |
| 65 aspect_ratio_goodness_ = actual_aspect_ratio / preferred_aspect_ratio; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 const SkISize& size() const { return size_; } | |
| 70 float client_scale_factor() const { return client_scale_factor_; } | |
| 71 float aspect_ratio_goodness() const { return aspect_ratio_goodness_; } | |
| 72 int64 area() const { | |
| 73 return static_cast<int64>(size_.width()) * size_.height(); | |
| 74 } | |
| 75 | |
| 76 bool IsBetterThan(const SupportedSize& other) const { | |
| 77 // First consider which size results in the largest scale factor at the | |
| 78 // client. Since the scale factor is at most 1.0, this implicitly prefers | |
| 79 // sizes with both width and height no larger than requested. | |
|
Wez
2012/09/20 21:35:15
nit: Reword: If either size would require down-sca
Jamie
2012/09/20 22:59:59
Done.
| |
| 80 if (client_scale_factor() < other.client_scale_factor()) { | |
| 81 return false; | |
| 82 } else if (client_scale_factor() > other.client_scale_factor()) { | |
| 83 return true; | |
| 84 } | |
| 85 // If the scale factors are the same, pick the size with the largest area. | |
|
Wez
2012/09/20 21:35:15
nit: blank before this comment
Jamie
2012/09/20 22:59:59
Done.
| |
| 86 if (area() < other.area()) { | |
| 87 return false; | |
| 88 } else if (area() > other.area()) { | |
| 89 return true; | |
| 90 } | |
| 91 // If the areas are equal, pick the size with the "best" aspect ratio. | |
|
Wez
2012/09/20 21:35:15
nit: blank before this comment
Jamie
2012/09/20 22:59:59
Done.
| |
| 92 if (aspect_ratio_goodness() < other.aspect_ratio_goodness()) { | |
| 93 return false; | |
| 94 } else if (aspect_ratio_goodness() > other.aspect_ratio_goodness()) { | |
| 95 return true; | |
| 96 } | |
| 97 // If the aspect ratios are equally good (for example, comparing 640x480 to | |
|
Wez
2012/09/20 21:35:15
nit: blank before this comment
Jamie
2012/09/20 22:59:59
Done.
| |
| 98 // 480x640 w.r.t. 640x640), just pick the widest size. This assumes that | |
| 99 // landscape aspect ratios are more common than portrait. | |
|
Wez
2012/09/20 21:35:15
nit: Reword: ... pick the widest, since desktop us
Jamie
2012/09/20 22:59:59
Done.
| |
| 100 return size().width() > other.size().width(); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 float client_scale_factor_; | |
| 105 float aspect_ratio_goodness_; | |
| 106 SkISize size_; | |
| 107 }; | |
| 108 | |
| 109 void ResizingHostObserver::OnClientDimensionsChanged( | |
| 110 const std::string& jid, const SkISize& preferred_size) { | |
| 111 std::list<SkISize> sizes = | |
| 112 desktop_resizer_->GetSupportedSizes(preferred_size); | |
| 113 if (sizes.empty()) { | |
| 114 return; | |
| 115 } | |
| 116 // If the implementation returned any sizes, pick the best one according to | |
|
Wez
2012/09/20 21:35:15
nit: blank line before this comment
Jamie
2012/09/20 22:59:59
Done.
| |
| 117 // the algorithm described in SupportedSize::IsBetterThen. | |
| 118 SupportedSize best_size(sizes.front(), preferred_size); | |
| 119 std::list<SkISize>::iterator i = sizes.begin(); | |
| 120 for (++i; i != sizes.end(); ++i) { | |
| 121 SupportedSize candidate_size(*i, preferred_size); | |
| 122 if (candidate_size.IsBetterThan(best_size)) { | |
| 123 best_size = candidate_size; | |
| 124 } | |
| 125 } | |
| 126 desktop_resizer_->SetSize(best_size.size()); | |
| 127 } | |
| 128 | |
| 129 } // namespace remoting | |
| OLD | NEW |