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()) { | |
| 32 desktop_resizer_->SetSize(original_size_); | |
| 33 original_size_.set(0, 0); | |
| 34 } | |
| 35 client_jid_.clear(); | |
| 36 } | |
| 37 | |
| 38 class SupportedSize { | |
| 39 public: | |
| 40 SupportedSize(const SkISize& actual, const SkISize& preferred) | |
| 41 : size_(actual) { | |
| 42 // The client scale factor is the smaller of the actual:preferred ratios | |
| 43 // for width and height. | |
| 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 // Since clients do not scale up, 1.0 is the maximum. | |
|
Wez
2012/09/21 00:59:32
nit: up-scale (for consistency w/ use of "down-sca
| |
| 53 client_scale_factor_ = 1.0; | |
| 54 } | |
| 55 | |
| 56 // The aspect ratio "goodness" is defined as being the ratio of the smaller | |
| 57 // of the actual and preferred aspect ratios to the larger. The best aspect | |
| 58 // ratio is the one that most closely matches the preferred aspect ratio | |
| 59 // (in other words, the ideal aspect ratio "goodness" is 1.0). By keeping | |
| 60 // the values < 1.0, it allows ratios the differ in opposite directions to | |
|
Wez
2012/09/21 00:59:32
typo: the -> that
| |
| 61 // be compared numerically. | |
| 62 float actual_aspect_ratio = | |
| 63 static_cast<float>(actual.width()) / actual.height(); | |
| 64 float preferred_aspect_ratio = | |
| 65 static_cast<float>(preferred.width()) / preferred.height(); | |
| 66 if (actual_aspect_ratio > preferred_aspect_ratio) { | |
| 67 aspect_ratio_goodness_ = preferred_aspect_ratio / actual_aspect_ratio; | |
| 68 } else { | |
| 69 aspect_ratio_goodness_ = actual_aspect_ratio / preferred_aspect_ratio; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 const SkISize& size() const { return size_; } | |
| 74 float client_scale_factor() const { return client_scale_factor_; } | |
| 75 float aspect_ratio_goodness() const { return aspect_ratio_goodness_; } | |
| 76 int64 area() const { | |
| 77 return static_cast<int64>(size_.width()) * size_.height(); | |
| 78 } | |
| 79 | |
| 80 bool IsBetterThan(const SupportedSize& other) const { | |
| 81 // If either size would require down-scaling, prefer the one that down- | |
| 82 // scales the least (since the client scale factor is at most 1.0, this | |
| 83 // does not differentiate between sizes that don't require down-scaling). | |
| 84 if (client_scale_factor() < other.client_scale_factor()) { | |
| 85 return false; | |
| 86 } else if (client_scale_factor() > other.client_scale_factor()) { | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 // If the scale factors are the same, pick the size with the largest area. | |
| 91 if (area() < other.area()) { | |
| 92 return false; | |
| 93 } else if (area() > other.area()) { | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 // If the areas are equal, pick the size with the "best" aspect ratio. | |
| 98 if (aspect_ratio_goodness() < other.aspect_ratio_goodness()) { | |
| 99 return false; | |
| 100 } else if (aspect_ratio_goodness() > other.aspect_ratio_goodness()) { | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 // If the aspect ratios are equally good (for example, comparing 640x480 | |
| 105 // to 480x640 w.r.t. 640x640), just pick the widest, since desktop UIs | |
| 106 // are typically design for landscape aspect ratios. | |
|
Wez
2012/09/21 00:59:32
typo: design -> designed
| |
| 107 return size().width() > other.size().width(); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 float client_scale_factor_; | |
| 112 float aspect_ratio_goodness_; | |
| 113 SkISize size_; | |
| 114 }; | |
| 115 | |
| 116 void ResizingHostObserver::OnClientDimensionsChanged( | |
| 117 const std::string& jid, const SkISize& preferred_size) { | |
| 118 std::list<SkISize> sizes = | |
| 119 desktop_resizer_->GetSupportedSizes(preferred_size); | |
| 120 if (sizes.empty()) { | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 // If the implementation returned any sizes, pick the best one according to | |
| 125 // the algorithm described in SupportedSize::IsBetterThen. | |
| 126 SupportedSize best_size(sizes.front(), preferred_size); | |
| 127 std::list<SkISize>::iterator i = sizes.begin(); | |
| 128 for (++i; i != sizes.end(); ++i) { | |
| 129 SupportedSize candidate_size(*i, preferred_size); | |
| 130 if (candidate_size.IsBetterThan(best_size)) { | |
| 131 best_size = candidate_size; | |
| 132 } | |
| 133 } | |
| 134 desktop_resizer_->SetSize(best_size.size()); | |
| 135 } | |
| 136 | |
| 137 } // namespace remoting | |
| OLD | NEW |