OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/desktop_background/desktop_background_controller.h" | 5 #include "ash/desktop_background/desktop_background_controller.h" |
6 | 6 |
7 #include "ash/desktop_background/desktop_background_view.h" | 7 #include "ash/desktop_background/desktop_background_view.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/shell_factory.h" | 9 #include "ash/shell_factory.h" |
10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
11 #include "ash/wm/root_window_layout_manager.h" | 11 #include "ash/wm/root_window_layout_manager.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/synchronization/cancellation_flag.h" | 14 #include "base/synchronization/cancellation_flag.h" |
15 #include "base/threading/worker_pool.h" | 15 #include "base/threading/worker_pool.h" |
16 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
17 #include "ui/aura/root_window.h" | 17 #include "ui/aura/root_window.h" |
18 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/compositor/layer.h" | 20 #include "ui/compositor/layer.h" |
21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
22 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
23 | 23 |
24 namespace ash { | 24 namespace ash { |
25 namespace { | 25 namespace { |
26 | |
27 const int kSmallWallpaperMaximalWidth = 1366; | |
28 const int kSmallWallpaperMaximalHeight = 800; | |
29 | |
30 internal::RootWindowLayoutManager* GetRootWindowLayoutManager( | 26 internal::RootWindowLayoutManager* GetRootWindowLayoutManager( |
31 aura::RootWindow* root_window) { | 27 aura::RootWindow* root_window) { |
32 return static_cast<internal::RootWindowLayoutManager*>( | 28 return static_cast<internal::RootWindowLayoutManager*>( |
33 root_window->layout_manager()); | 29 root_window->layout_manager()); |
34 } | 30 } |
35 } // namespace | 31 } // namespace |
36 | 32 |
37 // Stores the current wallpaper data. | 33 // Stores the current wallpaper data. |
38 struct DesktopBackgroundController::WallpaperData { | 34 struct DesktopBackgroundController::WallpaperData { |
39 WallpaperData(int index, WallpaperResolution resolution) | 35 explicit WallpaperData(int index) |
40 : wallpaper_index(index), | 36 : wallpaper_index(index), |
41 wallpaper_layout(GetWallpaperViewInfo(index, resolution).layout), | 37 wallpaper_layout(GetWallpaperInfo(index).layout), |
42 wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 38 wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
43 GetWallpaperViewInfo(index, resolution).id).ToImageSkia())) { | 39 GetWallpaperInfo(index).id).ToImageSkia())) { |
44 } | 40 } |
45 WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image) | 41 WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image) |
46 : wallpaper_index(-1), | 42 : wallpaper_index(-1), |
47 wallpaper_layout(layout), | 43 wallpaper_layout(layout), |
48 wallpaper_image(image) { | 44 wallpaper_image(image) { |
49 } | 45 } |
50 const int wallpaper_index; | 46 const int wallpaper_index; |
51 const WallpaperLayout wallpaper_layout; | 47 const WallpaperLayout wallpaper_layout; |
52 const gfx::ImageSkia wallpaper_image; | 48 const gfx::ImageSkia wallpaper_image; |
53 }; | 49 }; |
54 | 50 |
55 // DesktopBackgroundController::WallpaperOperation wraps background wallpaper | 51 // DesktopBackgroundController::WallpaperOperation wraps background wallpaper |
56 // loading. | 52 // loading. |
57 class DesktopBackgroundController::WallpaperOperation | 53 class DesktopBackgroundController::WallpaperOperation |
58 : public base::RefCountedThreadSafe< | 54 : public base::RefCountedThreadSafe< |
59 DesktopBackgroundController::WallpaperOperation> { | 55 DesktopBackgroundController::WallpaperOperation> { |
60 public: | 56 public: |
61 WallpaperOperation(int index, WallpaperResolution resolution) | 57 explicit WallpaperOperation(int index) : index_(index) { |
62 : index_(index), | |
63 resolution_(resolution) { | |
64 } | 58 } |
65 | 59 |
66 static void Run(scoped_refptr<WallpaperOperation> wo) { | 60 static void Run(scoped_refptr<WallpaperOperation> wo) { |
67 wo->LoadingWallpaper(); | 61 wo->LoadingWallpaper(); |
68 } | 62 } |
69 | 63 |
70 void LoadingWallpaper() { | 64 void LoadingWallpaper() { |
71 if (cancel_flag_.IsSet()) | 65 if (cancel_flag_.IsSet()) |
72 return; | 66 return; |
73 wallpaper_data_.reset(new WallpaperData(index_, resolution_)); | 67 wallpaper_data_.reset(new WallpaperData(index_)); |
74 } | 68 } |
75 | 69 |
76 void Cancel() { | 70 void Cancel() { |
77 cancel_flag_.Set(); | 71 cancel_flag_.Set(); |
78 } | 72 } |
79 | 73 |
80 WallpaperData* ReleaseWallpaperData() { | 74 WallpaperData* ReleaseWallpaperData() { |
81 return wallpaper_data_.release(); | 75 return wallpaper_data_.release(); |
82 } | 76 } |
83 | 77 |
84 private: | 78 private: |
85 friend class base::RefCountedThreadSafe< | 79 friend class base::RefCountedThreadSafe< |
86 DesktopBackgroundController::WallpaperOperation>; | 80 DesktopBackgroundController::WallpaperOperation>; |
87 | 81 |
88 ~WallpaperOperation() {} | 82 ~WallpaperOperation() {} |
89 | 83 |
90 base::CancellationFlag cancel_flag_; | 84 base::CancellationFlag cancel_flag_; |
91 | 85 |
92 scoped_ptr<WallpaperData> wallpaper_data_; | 86 scoped_ptr<WallpaperData> wallpaper_data_; |
93 | 87 |
94 const int index_; | 88 int index_; |
95 | |
96 const WallpaperResolution resolution_; | |
97 | 89 |
98 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); | 90 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); |
99 }; | 91 }; |
100 | 92 |
101 DesktopBackgroundController::DesktopBackgroundController() | 93 DesktopBackgroundController::DesktopBackgroundController() |
102 : desktop_background_mode_(BACKGROUND_IMAGE), | 94 : desktop_background_mode_(BACKGROUND_IMAGE), |
103 background_color_(SK_ColorGRAY), | 95 background_color_(SK_ColorGRAY), |
104 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 96 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
105 } | 97 } |
106 | 98 |
(...skipping 17 matching lines...) Expand all Loading... |
124 if (desktop_background_mode_ != BACKGROUND_IMAGE) | 116 if (desktop_background_mode_ != BACKGROUND_IMAGE) |
125 return SkBitmap(); | 117 return SkBitmap(); |
126 return GetWallpaper(); | 118 return GetWallpaper(); |
127 } | 119 } |
128 | 120 |
129 void DesktopBackgroundController::OnRootWindowAdded( | 121 void DesktopBackgroundController::OnRootWindowAdded( |
130 aura::RootWindow* root_window) { | 122 aura::RootWindow* root_window) { |
131 switch (desktop_background_mode_) { | 123 switch (desktop_background_mode_) { |
132 case BACKGROUND_IMAGE: | 124 case BACKGROUND_IMAGE: |
133 if (current_wallpaper_.get()) { | 125 if (current_wallpaper_.get()) { |
134 gfx::Size root_window_size = root_window->GetHostSize(); | 126 SetDesktopBackgroundImage(root_window); |
135 int wallpaper_width = current_wallpaper_->wallpaper_image.width(); | |
136 int wallpaper_height = current_wallpaper_->wallpaper_image.height(); | |
137 // Loads a higher resolution wallpaper if needed. | |
138 if ((wallpaper_width < root_window_size.width() || | |
139 wallpaper_height < root_window_size.height()) && | |
140 current_wallpaper_->wallpaper_index != -1 && | |
141 current_wallpaper_->wallpaper_layout != TILE) | |
142 SetDefaultWallpaper(current_wallpaper_->wallpaper_index, true); | |
143 else | |
144 SetDesktopBackgroundImage(root_window); | |
145 } else { | 127 } else { |
146 internal::CreateDesktopBackground(root_window); | 128 internal::CreateDesktopBackground(root_window); |
147 } | 129 } |
148 break; | 130 break; |
149 case BACKGROUND_SOLID_COLOR: | 131 case BACKGROUND_SOLID_COLOR: |
150 SetDesktopBackgroundSolidColorMode(background_color_); | 132 SetDesktopBackgroundSolidColorMode(background_color_); |
151 break; | 133 break; |
152 } | 134 } |
153 } | 135 } |
154 | 136 |
155 void DesktopBackgroundController::SetDefaultWallpaper(int index, | 137 void DesktopBackgroundController::SetDefaultWallpaper(int index) { |
156 bool force_reload) { | |
157 // We should not change background when index is invalid. For instance, at | 138 // We should not change background when index is invalid. For instance, at |
158 // login screen or stub_user login. | 139 // login screen or stub_user login. |
159 if (index == ash::GetInvalidWallpaperIndex()) { | 140 if (index == ash::GetInvalidWallpaperIndex()) { |
160 CreateEmptyWallpaper(); | 141 CreateEmptyWallpaper(); |
161 return; | 142 return; |
162 } else if (index == ash::GetSolidColorIndex()) { | 143 } else if (index == ash::GetSolidColorIndex()) { |
163 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); | 144 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); |
164 return; | 145 return; |
165 } | 146 } |
166 | 147 |
167 if (!force_reload && current_wallpaper_.get() && | 148 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) |
168 current_wallpaper_->wallpaper_index == index) | |
169 return; | 149 return; |
170 | 150 |
171 CancelPendingWallpaperOperation(); | 151 CancelPendingWallpaperOperation(); |
172 | 152 |
173 WallpaperResolution resolution = SMALL; | 153 wallpaper_op_ = new WallpaperOperation(index); |
174 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
175 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
176 iter != root_windows.end(); ++iter) { | |
177 gfx::Size root_window_size = (*iter)->GetHostSize(); | |
178 if (root_window_size.width() > kSmallWallpaperMaximalWidth || | |
179 root_window_size.height() > kSmallWallpaperMaximalHeight) | |
180 resolution = LARGE; | |
181 } | |
182 | |
183 wallpaper_op_ = new WallpaperOperation(index, resolution); | |
184 base::WorkerPool::PostTaskAndReply( | 154 base::WorkerPool::PostTaskAndReply( |
185 FROM_HERE, | 155 FROM_HERE, |
186 base::Bind(&WallpaperOperation::Run, wallpaper_op_), | 156 base::Bind(&WallpaperOperation::Run, wallpaper_op_), |
187 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, | 157 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, |
188 weak_ptr_factory_.GetWeakPtr(), | 158 weak_ptr_factory_.GetWeakPtr(), |
189 wallpaper_op_), | 159 wallpaper_op_), |
190 true /* task_is_slow */); | 160 true /* task_is_slow */); |
191 } | 161 } |
192 | 162 |
193 void DesktopBackgroundController::SetCustomWallpaper( | 163 void DesktopBackgroundController::SetCustomWallpaper( |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 current_wallpaper_.reset(NULL); | 235 current_wallpaper_.reset(NULL); |
266 desktop_background_mode_ = BACKGROUND_IMAGE; | 236 desktop_background_mode_ = BACKGROUND_IMAGE; |
267 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | 237 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
268 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | 238 for (Shell::RootWindowList::iterator iter = root_windows.begin(); |
269 iter != root_windows.end(); ++iter) { | 239 iter != root_windows.end(); ++iter) { |
270 internal::CreateDesktopBackground(*iter); | 240 internal::CreateDesktopBackground(*iter); |
271 } | 241 } |
272 } | 242 } |
273 | 243 |
274 } // namespace ash | 244 } // namespace ash |
OLD | NEW |