dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 1 | // Copyright 2014 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 "ui/display/chromeos/configure_displays_task.h" |
| 6 | |
| 7 | #include "base/auto_reset.h" |
| 8 | #include "base/bind.h" |
| 9 | #include "ui/display/types/display_snapshot.h" |
| 10 | #include "ui/display/types/native_display_delegate.h" |
| 11 | |
| 12 | namespace ui { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Find the next best mode after |display_mode|. If none can be found return |
| 17 | // nullptr. |
| 18 | const DisplayMode* FindNextMode(const DisplaySnapshot& display_state, |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame^] | 19 | const DisplayMode* display_mode) { |
| 20 | if (!display_mode) |
| 21 | return nullptr; |
| 22 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 23 | int best_mode_pixels = 0; |
| 24 | const DisplayMode* best_mode = nullptr; |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame^] | 25 | int current_mode_pixels = display_mode->size().GetArea(); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 26 | for (const DisplayMode* mode : display_state.modes()) { |
| 27 | int pixel_count = mode->size().GetArea(); |
| 28 | if (pixel_count < current_mode_pixels && pixel_count > best_mode_pixels) { |
| 29 | best_mode = mode; |
| 30 | best_mode_pixels = pixel_count; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return best_mode; |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display, |
| 40 | const DisplayMode* mode, |
| 41 | const gfx::Point& origin) |
| 42 | : display(display), mode(mode), origin(origin) { |
| 43 | } |
| 44 | |
| 45 | ConfigureDisplaysTask::ConfigureDisplaysTask( |
| 46 | NativeDisplayDelegate* delegate, |
| 47 | const std::vector<DisplayConfigureRequest>& requests, |
| 48 | const ResponseCallback& callback) |
| 49 | : delegate_(delegate), |
| 50 | requests_(requests), |
| 51 | callback_(callback), |
| 52 | is_configuring_(false), |
| 53 | num_displays_configured_(0), |
| 54 | task_status_(SUCCESS), |
| 55 | weak_ptr_factory_(this) { |
| 56 | for (size_t i = 0; i < requests_.size(); ++i) |
| 57 | pending_request_indexes_.push(i); |
| 58 | } |
| 59 | |
| 60 | ConfigureDisplaysTask::~ConfigureDisplaysTask() { |
| 61 | } |
| 62 | |
| 63 | void ConfigureDisplaysTask::Run() { |
| 64 | // Synchronous configurators will recursively call Run(). In that case just |
| 65 | // defer their call to the next iteration in the while-loop. This is done to |
| 66 | // guard against stack overflows if the display has a large list of broken |
| 67 | // modes. |
| 68 | if (is_configuring_) |
| 69 | return; |
| 70 | |
dnicoara | 65d9d31 | 2014-12-13 02:30:47 | [diff] [blame] | 71 | { |
| 72 | base::AutoReset<bool> recursivity_guard(&is_configuring_, true); |
| 73 | while (!pending_request_indexes_.empty()) { |
| 74 | size_t index = pending_request_indexes_.front(); |
| 75 | DisplayConfigureRequest* request = &requests_[index]; |
| 76 | pending_request_indexes_.pop(); |
| 77 | delegate_->Configure(*request->display, request->mode, request->origin, |
| 78 | base::Bind(&ConfigureDisplaysTask::OnConfigured, |
| 79 | weak_ptr_factory_.GetWeakPtr(), index)); |
| 80 | } |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 81 | } |
| 82 | |
dnicoara | 65d9d31 | 2014-12-13 02:30:47 | [diff] [blame] | 83 | // Nothing should be modified after the |callback_| is called since the |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 84 | // task may be deleted in the callback. |
| 85 | if (num_displays_configured_ == requests_.size()) |
| 86 | callback_.Run(task_status_); |
| 87 | } |
| 88 | |
| 89 | void ConfigureDisplaysTask::OnConfigured(size_t index, bool success) { |
| 90 | DisplayConfigureRequest* request = &requests_[index]; |
| 91 | VLOG(2) << "Configured status=" << success |
| 92 | << " display=" << request->display->display_id() |
| 93 | << " origin=" << request->origin.ToString() |
| 94 | << " mode=" << (request->mode ? request->mode->ToString() : "null"); |
| 95 | if (!success) { |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame^] | 96 | request->mode = FindNextMode(*request->display, request->mode); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 97 | if (request->mode) { |
| 98 | pending_request_indexes_.push(index); |
| 99 | if (task_status_ == SUCCESS) |
| 100 | task_status_ = PARTIAL_SUCCESS; |
| 101 | |
| 102 | Run(); |
| 103 | return; |
| 104 | } |
| 105 | } else { |
| 106 | request->display->set_current_mode(request->mode); |
| 107 | request->display->set_origin(request->origin); |
| 108 | } |
| 109 | |
| 110 | num_displays_configured_++; |
| 111 | if (!success) |
| 112 | task_status_ = ERROR; |
| 113 | |
| 114 | Run(); |
| 115 | } |
| 116 | |
| 117 | } // namespace ui |