1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
ash / wallpaper / views / wallpaper_view.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wallpaper/views/wallpaper_view.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_animation_types.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/wallpaper/views/wallpaper_widget_controller.h"
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "cc/paint/render_surface_filters.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/display/screen.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/window_animations.h"
namespace ash {
namespace {
// A view that controls the child view's layer so that the layer always has the
// same size as the display's original, un-scaled size in DIP. The layer is then
// transformed to fit to the virtual screen size when laid-out. This is to avoid
// scaling the image at painting time, then scaling it back to the screen size
// in the compositor.
class WallpaperWidgetDelegate : public views::WidgetDelegateView {
public:
explicit WallpaperWidgetDelegate(views::View* view) {
SetCanMaximize(true);
SetCanFullscreen(true);
AddChildView(view);
view->SetPaintToLayer();
}
WallpaperWidgetDelegate(const WallpaperWidgetDelegate&) = delete;
WallpaperWidgetDelegate& operator=(const WallpaperWidgetDelegate&) = delete;
// views::View:
void Layout(PassKey) override {
aura::Window* window = GetWidget()->GetNativeWindow();
// Keep |this| at the bottom since there may be other windows on top of the
// wallpaper view such as an overview mode shield.
window->parent()->StackChildAtBottom(window);
display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(window);
for (views::View* child : children()) {
child->SetBounds(0, 0, display.size().width(), display.size().height());
gfx::Transform transform;
// Apply RTL transform explicitly becacuse Views layer code
// doesn't handle RTL. crbug.com/458753.
transform.Translate(-child->GetMirroredX(), 0);
child->SetTransform(transform);
}
}
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// WallpaperView, public:
WallpaperView::WallpaperView(float blur_sigma) : blur_sigma_(blur_sigma) {
set_context_menu_controller(this);
}
WallpaperView::~WallpaperView() = default;
void WallpaperView::ClearCachedImage() {
small_image_.reset();
}
void WallpaperView::SetLockShieldEnabled(bool enabled) {
if (enabled == !!shield_view_) {
return;
}
if (enabled) {
DCHECK(!shield_view_);
// TODO(crbug.com/374034250): Remove the shield layer once the bug is fixed and
// the compositor can fallback to solid color background for missing tiles.
shield_view_ = new views::View();
parent()->AddChildViewAt(shield_view_.get(), 0);
shield_view_->SetPaintToLayer(ui::LAYER_SOLID_COLOR);
shield_view_->layer()->SetColor(SK_ColorBLACK);
shield_view_->layer()->SetName("WallpaperViewShield");
shield_view_->SetBoundsRect(parent()->GetLocalBounds());
// Mark the layer transparent to make sure that the compositor will draw the
// solid color even if the texture for the wallpaper is missing. This will
// increase the overdraw, but the impact on the performance should be
// minimum.
layer()->SetFillsBoundsOpaquely(false);
} else {
DCHECK(shield_view_);
layer()->SetFillsBoundsOpaquely(true);
parent()->RemoveChildViewT(shield_view_.get());
shield_view_ = nullptr;
}
}
bool WallpaperView::OnMousePressed(const ui::MouseEvent& event) {
return true;
}
void WallpaperView::OnMouseReleased(const ui::MouseEvent& event) {
if (features::ShouldEnterOverviewFromWallpaper()) {
OverviewController* overview_controller =
Shell::Get()->overview_controller();
if (!overview_controller->InOverviewSession()) {
overview_controller->StartOverview(OverviewStartAction::kWallpaper);
}
}
}
void WallpaperView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
if (shield_view_) {
shield_view_->SetBoundsRect(parent()->GetLocalBounds());
}
}
void WallpaperView::ShowContextMenuForViewImpl(
views::View* source,
const gfx::Point& point,
ui::mojom::MenuSourceType source_type) {
Shell::Get()->ShowContextMenu(point, source_type);
}
void WallpaperView::DrawWallpaper(const gfx::ImageSkia& wallpaper,
const gfx::Rect& src,
const gfx::Rect& dst,
const cc::PaintFlags& flags,
gfx::Canvas* canvas) {
// The amount we downsample the original image by before applying filters to
// improve performance.
constexpr float quality = 0.3f;
gfx::Rect quality_adjusted_rect = gfx::ScaleToEnclosingRect(dst, quality);
// Draw the wallpaper to a cached image the first time it is drawn or if the
// size has changed.
if (!small_image_ || small_image_->size() != quality_adjusted_rect.size()) {
gfx::Canvas small_canvas(quality_adjusted_rect.size(),
/*image_scale=*/1.f,
/*is_opaque=*/false);
small_canvas.DrawImageInt(wallpaper, src.x(), src.y(), src.width(),
src.height(), 0, 0, quality_adjusted_rect.width(),
quality_adjusted_rect.height(), true);
small_image_ = std::make_optional(
gfx::ImageSkia::CreateFrom1xBitmap(small_canvas.GetBitmap()));
}
if (blur_sigma_ == wallpaper_constants::kClear) {
canvas->DrawImageInt(wallpaper, src.x(), src.y(), src.width(), src.height(),
dst.x(), dst.y(), dst.width(), dst.height(),
/*filter=*/true, flags);
return;
}
bool will_not_fill = width() > dst.width() || height() > dst.height();
// When not filling the view, we paint the small_image_ directly to the
// canvas.
float blur = will_not_fill ? blur_sigma_ : blur_sigma_ * quality;
// Create the blur and brightness filter to apply to the downsampled image.
cc::FilterOperations operations;
operations.Append(
cc::FilterOperation::CreateBlurFilter(blur, SkTileMode::kClamp));
sk_sp<cc::PaintFilter> filter =
cc::RenderSurfaceFilters::BuildImageFilter(operations);
// If the wallpaper can't fill the desktop, paint it directly to the
// canvas so that it can blend the image with the rest of background
// correctly.
if (blur > 0 && will_not_fill) {
cc::PaintFlags filter_flags(flags);
filter_flags.setImageFilter(filter);
canvas->DrawImageInt(*small_image_, 0, 0, small_image_->width(),
small_image_->height(), dst.x(), dst.y(), dst.width(),
dst.height(),
/*filter=*/true, filter_flags);
return;
}
cc::PaintFlags filter_flags;
filter_flags.setImageFilter(filter);
gfx::Canvas filtered_canvas(small_image_->size(),
/*image_scale=*/1.f,
/*is_opaque=*/false);
filtered_canvas.sk_canvas()->saveLayer(filter_flags);
filtered_canvas.DrawImageInt(
*small_image_, 0, 0, small_image_->width(), small_image_->height(), 0, 0,
small_image_->width(), small_image_->height(), true);
filtered_canvas.sk_canvas()->restore();
// Draw the downsampled and filtered image onto |canvas|. Draw a inseted
// version of the image to avoid drawing a blackish border caused by the blur
// filter. This is what we do on the login screen as well.
gfx::ImageSkia filtered_wallpaper =
gfx::ImageSkia::CreateFrom1xBitmap(filtered_canvas.GetBitmap());
canvas->DrawImageInt(filtered_wallpaper, blur, blur,
small_image_->width() - 2 * blur,
small_image_->height() - 2 * blur, dst.x(), dst.y(),
dst.width(), dst.height(),
/*filter=*/true, flags);
}
std::unique_ptr<views::Widget> CreateWallpaperWidget(
aura::Window* root_window,
float blur_sigma,
bool locked,
raw_ptr<WallpaperView>* out_wallpaper_view) {
int container_id = locked ? kShellWindowId_LockScreenWallpaperContainer
: kShellWindowId_WallpaperContainer;
auto* controller = Shell::Get()->wallpaper_controller();
auto wallpaper_widget = std::make_unique<views::Widget>();
views::Widget::InitParams params(
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.name = "WallpaperViewWidget";
params.layer_type = ui::LAYER_NOT_DRAWN;
params.parent = root_window->GetChildById(container_id);
WallpaperView* wallpaper_view = new WallpaperView(blur_sigma);
params.delegate = new WallpaperWidgetDelegate(wallpaper_view);
wallpaper_widget->Init(std::move(params));
// Owned by views.
*out_wallpaper_view = wallpaper_view;
int animation_type =
controller->ShouldShowInitialAnimation()
? static_cast<int>(
WINDOW_VISIBILITY_ANIMATION_TYPE_BRIGHTNESS_GRAYSCALE)
: static_cast<int>(wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
aura::Window* wallpaper_window = wallpaper_widget->GetNativeWindow();
::wm::SetWindowVisibilityAnimationType(wallpaper_window, animation_type);
// Enable wallpaper transition for the following cases:
// 1. Initial wallpaper animation after device boot.
// 2. Wallpaper fades in from a non empty background.
// 3. From an empty background, chrome transit to a logged in user session.
// 4. From an empty background, guest user logged in.
// except for the lock state.
if (!locked &&
(controller->ShouldShowInitialAnimation() ||
RootWindowController::ForWindow(root_window)
->wallpaper_widget_controller()
->IsAnimating() ||
Shell::Get()->session_controller()->NumberOfLoggedInUsers())) {
::wm::SetWindowVisibilityAnimationTransition(wallpaper_window,
::wm::ANIMATE_SHOW);
base::TimeDelta animation_duration = controller->animation_duration();
if (!animation_duration.is_zero()) {
::wm::SetWindowVisibilityAnimationDuration(wallpaper_window,
animation_duration);
}
} else {
// Disable animation if transition to login screen from an empty background.
::wm::SetWindowVisibilityAnimationTransition(wallpaper_window,
::wm::ANIMATE_NONE);
}
return wallpaper_widget;
}
BEGIN_METADATA(WallpaperView)
END_METADATA
} // namespace ash