| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/desktop_viewport.h" | 5 #include "remoting/client/desktop_viewport.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 namespace remoting { | 11 namespace remoting { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 float MAX_ZOOM_LEVEL = 100.f; | 15 float MAX_ZOOM_LEVEL = 100.f; |
| 16 | 16 |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 DesktopViewport::DesktopViewport() : desktop_to_surface_transform_() {} | 19 DesktopViewport::DesktopViewport() : desktop_to_surface_transform_() {} |
| 20 | 20 |
| 21 DesktopViewport::~DesktopViewport() {} | 21 DesktopViewport::~DesktopViewport() {} |
| 22 | 22 |
| 23 void DesktopViewport::SetDesktopSize(int desktop_width, int desktop_height) { | 23 void DesktopViewport::SetDesktopSize(int desktop_width, int desktop_height) { |
| 24 bool need_resize_to_fit = !desktop_size_ready_ && surface_size_ready_; | |
| 25 desktop_size_.x = desktop_width; | 24 desktop_size_.x = desktop_width; |
| 26 desktop_size_.y = desktop_height; | 25 desktop_size_.y = desktop_height; |
| 27 desktop_size_ready_ = true; | 26 ResizeToFit(); |
| 28 if (need_resize_to_fit) { | |
| 29 ResizeToFit(); | |
| 30 } else if (IsViewportReady()) { | |
| 31 UpdateViewport(); | |
| 32 } | |
| 33 } | 27 } |
| 34 | 28 |
| 35 void DesktopViewport::SetSurfaceSize(int surface_width, int surface_height) { | 29 void DesktopViewport::SetSurfaceSize(int surface_width, int surface_height) { |
| 36 bool need_resize_to_fit = desktop_size_ready_ && !surface_size_ready_; | |
| 37 surface_size_.x = surface_width; | 30 surface_size_.x = surface_width; |
| 38 surface_size_.y = surface_height; | 31 surface_size_.y = surface_height; |
| 39 surface_size_ready_ = true; | 32 ResizeToFit(); |
| 40 if (need_resize_to_fit) { | |
| 41 ResizeToFit(); | |
| 42 } else if (IsViewportReady()) { | |
| 43 UpdateViewport(); | |
| 44 } | |
| 45 } | 33 } |
| 46 | 34 |
| 47 void DesktopViewport::MoveDesktop(float dx, float dy) { | 35 void DesktopViewport::MoveDesktop(float dx, float dy) { |
| 48 desktop_to_surface_transform_.PostTranslate({dx, dy}); | 36 desktop_to_surface_transform_.PostTranslate({dx, dy}); |
| 49 UpdateViewport(); | 37 UpdateViewport(); |
| 50 } | 38 } |
| 51 | 39 |
| 52 void DesktopViewport::ScaleDesktop(float px, float py, float scale) { | 40 void DesktopViewport::ScaleDesktop(float px, float py, float scale) { |
| 53 desktop_to_surface_transform_.PostScale({px, py}, scale); | 41 desktop_to_surface_transform_.PostScale({px, py}, scale); |
| 54 UpdateViewport(); | 42 UpdateViewport(); |
| 55 } | 43 } |
| 56 | 44 |
| 57 void DesktopViewport::SetViewportCenter(float x, float y) { | 45 void DesktopViewport::SetViewportCenter(float x, float y) { |
| 58 ViewMatrix::Point old_center = GetViewportCenter(); | 46 ViewMatrix::Point old_center = GetViewportCenter(); |
| 59 MoveViewportCenterWithoutUpdate(x - old_center.x, y - old_center.y); | 47 MoveViewportCenterWithoutUpdate(x - old_center.x, y - old_center.y); |
| 60 UpdateViewport(); | 48 UpdateViewport(); |
| 61 } | 49 } |
| 62 | 50 |
| 63 void DesktopViewport::RegisterOnTransformationChangedCallback( | 51 void DesktopViewport::RegisterOnTransformationChangedCallback( |
| 64 const TransformationCallback& callback, | 52 const TransformationCallback& callback, |
| 65 bool run_immediately) { | 53 bool run_immediately) { |
| 66 on_transformation_changed_ = callback; | 54 on_transformation_changed_ = callback; |
| 67 if (IsViewportReady() && run_immediately) { | 55 if (IsViewportReady() && run_immediately) { |
| 68 callback.Run(desktop_to_surface_transform_); | 56 callback.Run(desktop_to_surface_transform_); |
| 69 } | 57 } |
| 70 } | 58 } |
| 71 | 59 |
| 72 void DesktopViewport::ResizeToFit() { | 60 void DesktopViewport::ResizeToFit() { |
| 73 DCHECK(IsViewportReady()); | 61 if (!IsViewportReady()) { |
| 62 return; |
| 63 } |
| 74 | 64 |
| 75 // <---Desktop Width----> | 65 // <---Desktop Width----> |
| 76 // +==========+---------+ | 66 // +==========+---------+ |
| 77 // | | | | 67 // | | | |
| 78 // | Viewport | Desktop | | 68 // | Viewport | Desktop | |
| 79 // | | | | 69 // | | | |
| 80 // +==========+---------+ | 70 // +==========+---------+ |
| 81 // | 71 // |
| 82 // +==========+ ^ | 72 // +==========+ ^ |
| 83 // | | | | 73 // | | | |
| 84 // | Viewport | | | 74 // | Viewport | | |
| 85 // | | | | 75 // | | | |
| 86 // +==========+ | Desktop | 76 // +==========+ | Desktop |
| 87 // | | | Height | 77 // | | | Height |
| 88 // | Desktop | | | 78 // | Desktop | | |
| 89 // | | | | 79 // | | | |
| 90 // +----------+ v | 80 // +----------+ v |
| 91 // resize the desktop such that it fits the viewport in one dimension. | 81 // resize the desktop such that it fits the viewport in one dimension. |
| 92 | 82 |
| 93 float scale = std::max(surface_size_.x / desktop_size_.x, | 83 float scale = std::max(surface_size_.x / desktop_size_.x, |
| 94 surface_size_.y / desktop_size_.y); | 84 surface_size_.y / desktop_size_.y); |
| 95 desktop_to_surface_transform_.SetScale(scale); | 85 desktop_to_surface_transform_.SetScale(scale); |
| 86 desktop_to_surface_transform_.SetOffset({0.f, 0.f}); |
| 96 UpdateViewport(); | 87 UpdateViewport(); |
| 97 } | 88 } |
| 98 | 89 |
| 99 bool DesktopViewport::IsViewportReady() const { | 90 bool DesktopViewport::IsViewportReady() const { |
| 100 return desktop_size_ready_ && surface_size_ready_; | 91 return desktop_size_.x != 0 && desktop_size_.y != 0 && surface_size_.x != 0 && |
| 92 surface_size_.y != 0; |
| 101 } | 93 } |
| 102 | 94 |
| 103 void DesktopViewport::UpdateViewport() { | 95 void DesktopViewport::UpdateViewport() { |
| 104 if (!IsViewportReady()) { | 96 if (!IsViewportReady()) { |
| 105 // User may attempt to zoom and pan before the desktop image is received. | 97 // User may attempt to zoom and pan before the desktop image is received. |
| 106 // This should be fine since the viewport will be reset once the image | 98 // This should be fine since the viewport will be reset once the image |
| 107 // dimension is set. | 99 // dimension is set. |
| 108 VLOG(1) << "Viewport is not ready yet."; | 100 VLOG(1) << "Viewport is not ready yet."; |
| 109 return; | 101 return; |
| 110 } | 102 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 221 |
| 230 if (new_point.y < bounds.top) { | 222 if (new_point.y < bounds.top) { |
| 231 new_point.y = bounds.top; | 223 new_point.y = bounds.top; |
| 232 } else if (new_point.y > bounds.bottom) { | 224 } else if (new_point.y > bounds.bottom) { |
| 233 new_point.y = bounds.bottom; | 225 new_point.y = bounds.bottom; |
| 234 } | 226 } |
| 235 return new_point; | 227 return new_point; |
| 236 } | 228 } |
| 237 | 229 |
| 238 } // namespace remoting | 230 } // namespace remoting |
| OLD | NEW |