blob: 17f1a12e8bf247e17cc01bce1415fbe3bbe5fe4c [file] [log] [blame]
dnicoara9372a7912014-12-11 01:29:061// 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
12namespace ui {
13
14namespace {
15
16// Find the next best mode after |display_mode|. If none can be found return
17// nullptr.
18const DisplayMode* FindNextMode(const DisplaySnapshot& display_state,
dnicoaraa89c2082015-01-05 16:49:0619 const DisplayMode* display_mode) {
20 if (!display_mode)
21 return nullptr;
22
dnicoara9372a7912014-12-11 01:29:0623 int best_mode_pixels = 0;
24 const DisplayMode* best_mode = nullptr;
dnicoaraa89c2082015-01-05 16:49:0625 int current_mode_pixels = display_mode->size().GetArea();
dnicoara9372a7912014-12-11 01:29:0626 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
39DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display,
40 const DisplayMode* mode,
41 const gfx::Point& origin)
42 : display(display), mode(mode), origin(origin) {
43}
44
45ConfigureDisplaysTask::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
60ConfigureDisplaysTask::~ConfigureDisplaysTask() {
61}
62
63void 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
dnicoara65d9d312014-12-13 02:30:4771 {
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 }
dnicoara9372a7912014-12-11 01:29:0681 }
82
dnicoara65d9d312014-12-13 02:30:4783 // Nothing should be modified after the |callback_| is called since the
dnicoara9372a7912014-12-11 01:29:0684 // task may be deleted in the callback.
85 if (num_displays_configured_ == requests_.size())
86 callback_.Run(task_status_);
87}
88
89void 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) {
dnicoaraa89c2082015-01-05 16:49:0696 request->mode = FindNextMode(*request->display, request->mode);
dnicoara9372a7912014-12-11 01:29:0697 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