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 // This implementation assumes a single connected client, which is what the |
| 33 // host currently supports |
| 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 (previous_size_.isZero()) { |
| 128 return; |
| 129 } |
| 130 |
| 131 // If the host desktop size changes other than via the resize-to-client |
| 132 // mechanism, then set |previous_size_| to zero and give up. This is an |
| 133 // indication that the user doesn't want resize-to-client. |
| 134 SkISize new_size = desktop_resizer_->GetCurrentSize(); |
| 135 if (new_size != previous_size_) { |
| 136 previous_size_ = SkISize::Make(0, 0); |
| 137 return; |
| 138 } |
| 139 |
| 140 // If the implementation returns any sizes, pick the best one according to |
| 141 // the algorithm described in CandidateSize::IsBetterThen. |
| 142 std::list<SkISize> sizes = |
| 143 desktop_resizer_->GetSupportedSizes(preferred_size); |
| 144 if (sizes.empty()) { |
| 145 return; |
| 146 } |
| 147 CandidateSize best_size(sizes.front(), preferred_size); |
| 148 std::list<SkISize>::iterator i = sizes.begin(); |
| 149 for (++i; i != sizes.end(); ++i) { |
| 150 CandidateSize candidate_size(*i, preferred_size); |
| 151 if (candidate_size.IsBetterThan(best_size)) { |
| 152 best_size = candidate_size; |
| 153 } |
| 154 } |
| 155 previous_size_ = best_size.size(); |
| 156 desktop_resizer_->SetSize(previous_size_); |
| 157 } |
| 158 |
| 159 } // namespace remoting |
OLD | NEW |