Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: services/ui/demo/mus_demo.cc

Issue 2622103004: Mus Demo: Demonstrate external window mode (Closed)
Patch Set: Rebase on top of 2679213003 and excluding 2645093003 Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/ui/demo/mus_demo.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "services/ui/demo/mus_demo.h" 5 #include "services/ui/demo/mus_demo.h"
6 6
7 #include "base/command_line.h"
7 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
8 #include "base/time/time.h" 9 #include "base/time/time.h"
9 #include "services/service_manager/public/cpp/connector.h" 10 #include "services/service_manager/public/cpp/connector.h"
10 #include "services/service_manager/public/cpp/service_context.h" 11 #include "services/service_manager/public/cpp/service_context.h"
11 #include "services/ui/public/cpp/gpu/gpu.h" 12 #include "services/ui/public/cpp/gpu/gpu.h"
13 #include "services/ui/public/interfaces/constants.mojom.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 14 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkCanvas.h" 15 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkColor.h" 16 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkImageInfo.h" 17 #include "third_party/skia/include/core/SkImageInfo.h"
16 #include "third_party/skia/include/core/SkPaint.h" 18 #include "third_party/skia/include/core/SkPaint.h"
17 #include "third_party/skia/include/core/SkRect.h" 19 #include "third_party/skia/include/core/SkRect.h"
18 #include "ui/aura/client/default_capture_client.h" 20 #include "ui/aura/client/default_capture_client.h"
19 #include "ui/aura/env.h" 21 #include "ui/aura/env.h"
20 #include "ui/aura/mus/property_converter.h" 22 #include "ui/aura/mus/property_converter.h"
21 #include "ui/aura/mus/window_tree_client.h" 23 #include "ui/aura/mus/window_tree_client.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 canvas->rotate(angle); 62 canvas->rotate(angle);
61 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), 63 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f),
62 -SkFloatToScalar(canvas_size.height() * 0.5f)); 64 -SkFloatToScalar(canvas_size.height() * 0.5f));
63 } 65 }
64 66
65 canvas->drawRect(rect, paint); 67 canvas->drawRect(rect, paint);
66 } 68 }
67 69
68 } // namespace 70 } // namespace
69 71
72 class MusDemo::WindowTreeData {
73 public:
74 explicit WindowTreeData(
75 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
76 Init(std::move(window_tree_host));
77 }
78 explicit WindowTreeData(mojom::WindowTreeHostFactory* factory,
79 mojom::WindowTreeClientPtr tree_client) {
80 factory->CreateWindowTreeHost(MakeRequest(&host_), std::move(tree_client));
81 }
82 ~WindowTreeData();
83
84 // Initializes the window tree host and start drawing frames.
85 void Init(std::unique_ptr<aura::WindowTreeHostMus> window_tree_host);
86
87 private:
88 bool IsInitialized() { return !!window_tree_host_; }
89
90 // Draws one frame, incrementing the rotation angle.
91 void DrawFrame();
92
93 // The Window tree host corresponding to this data.
94 // When the WindowTreeData is created with WindowTreeHostFactory, it remains
95 // null until an explicit call to Init.
96 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_;
97
98 // When the WindowTreeData is created with WindowTreeHostFactory, this holds
99 // the Mojo pointer to the window tree host.
100 mojom::WindowTreeHostPtr host_;
101
102 // Root window of the window tree host.
103 aura::Window* root_window_ = nullptr;
104
105 // Window to which we draw the bitmap.
106 std::unique_ptr<aura::Window> bitmap_window_;
107
108 // Destroys itself when the window gets destroyed.
109 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr;
110
111 // Timer for calling DrawFrame().
112 base::RepeatingTimer timer_;
113
114 // Current rotation angle for drawing.
115 double angle_ = 0.0;
116
117 // Last time a frame was drawn.
118 base::TimeTicks last_draw_frame_time_;
119 };
120
70 MusDemo::MusDemo() {} 121 MusDemo::MusDemo() {}
71 122
72 MusDemo::~MusDemo() { 123 MusDemo::~MusDemo() {
73 display::Screen::SetScreenInstance(nullptr); 124 display::Screen::SetScreenInstance(nullptr);
74 } 125 }
75 126
76 void MusDemo::OnStart() { 127 void MusDemo::OnStart() {
128 external_window_mode_ =
129 base::CommandLine::ForCurrentProcess()->HasSwitch("external-window-mode");
77 screen_ = base::MakeUnique<display::ScreenBase>(); 130 screen_ = base::MakeUnique<display::ScreenBase>();
78 display::Screen::SetScreenInstance(screen_.get()); 131 display::Screen::SetScreenInstance(screen_.get());
79 132
80 env_ = aura::Env::CreateInstance(aura::Env::Mode::MUS); 133 env_ = aura::Env::CreateInstance(aura::Env::Mode::MUS);
81 capture_client_ = base::MakeUnique<aura::client::DefaultCaptureClient>(); 134 capture_client_ = base::MakeUnique<aura::client::DefaultCaptureClient>();
82 property_converter_ = base::MakeUnique<aura::PropertyConverter>(); 135 property_converter_ = base::MakeUnique<aura::PropertyConverter>();
83 wm_state_ = base::MakeUnique<::wm::WMState>(); 136 wm_state_ = base::MakeUnique<::wm::WMState>();
84 137
85 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>( 138 if (external_window_mode_) {
86 context()->connector(), this, this); 139 context()->connector()->BindInterface(ui::mojom::kServiceName,
87 window_tree_client_->ConnectAsWindowManager(); 140 &window_tree_host_factory_);
141 mojom::WindowTreeClientPtr tree_client;
142 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>(
rjkroege 2017/02/09 18:03:53 I would like mus demo to not depend on aura in eit
fwang 2017/02/09 18:50:38 I'm not sure I understand this comment. Mus demo h
143 context()->connector(), this, this, MakeRequest(&tree_client));
144 window_tree_data_ = base::MakeUnique<WindowTreeData>(
145 window_tree_host_factory_.get(), std::move(tree_client));
146 // TODO: Management of display (CL 2645093003 and 2684623002)?
rjkroege 2017/02/09 18:03:53 Need a ScreenManagerOzoneExternal I would think. P
147 // TODO: Demo of two external windows.
148 } else {
149 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>(
150 context()->connector(), this, this);
151 window_tree_client_->ConnectAsWindowManager();
152 }
88 153
89 env_->SetWindowTreeClient(window_tree_client_.get()); 154 env_->SetWindowTreeClient(window_tree_client_.get());
90 } 155 }
91 156
92 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, 157 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info,
93 service_manager::InterfaceRegistry* registry) { 158 service_manager::InterfaceRegistry* registry) {
94 return true; 159 return true;
95 } 160 }
96 161
97 void MusDemo::OnEmbed( 162 void MusDemo::OnEmbed(
98 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { 163 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
99 // Not called for the WindowManager. 164 DCHECK(external_window_mode_);
100 NOTREACHED(); 165 DCHECK(window_tree_data_);
166 window_tree_data_->Init(std::move(window_tree_host));
101 } 167 }
102 168
103 void MusDemo::OnUnembed(aura::Window* root) { 169 void MusDemo::OnUnembed(aura::Window* root) {
104 NOTREACHED(); 170 NOTREACHED();
105 } 171 }
106 172
107 void MusDemo::OnEmbedRootDestroyed(aura::WindowTreeHostMus* window_tree_host) { 173 void MusDemo::OnEmbedRootDestroyed(aura::WindowTreeHostMus* window_tree_host) {
108 // Not called for the WindowManager. 174 DCHECK(external_window_mode_);
109 NOTREACHED(); 175 window_tree_data_.reset();
110 } 176 }
111 177
112 void MusDemo::OnLostConnection(aura::WindowTreeClient* client) { 178 void MusDemo::OnLostConnection(aura::WindowTreeClient* client) {
113 root_window_ = nullptr;
114 window_tree_client_.reset(); 179 window_tree_client_.reset();
115 timer_.Stop(); 180 window_tree_data_.reset();
116 } 181 }
117 182
118 void MusDemo::OnPointerEventObserved(const PointerEvent& event, 183 void MusDemo::OnPointerEventObserved(const PointerEvent& event,
119 aura::Window* target) {} 184 aura::Window* target) {}
120 185
121 aura::PropertyConverter* MusDemo::GetPropertyConverter() { 186 aura::PropertyConverter* MusDemo::GetPropertyConverter() {
122 return property_converter_.get(); 187 return property_converter_.get();
123 } 188 }
124 189
125 void MusDemo::SetWindowManagerClient(aura::WindowManagerClient* client) {} 190 void MusDemo::SetWindowManagerClient(aura::WindowManagerClient* client) {}
(...skipping 24 matching lines...) Expand all
150 } 215 }
151 216
152 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) { 217 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) {
153 screen_->display_list().AddDisplay(display, 218 screen_->display_list().AddDisplay(display,
154 display::DisplayList::Type::PRIMARY); 219 display::DisplayList::Type::PRIMARY);
155 } 220 }
156 221
157 void MusDemo::OnWmNewDisplay( 222 void MusDemo::OnWmNewDisplay(
158 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host, 223 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host,
159 const display::Display& display) { 224 const display::Display& display) {
160 DCHECK(!root_window_); // Only support one display. 225 DCHECK(!external_window_mode_);
226 DCHECK(!window_tree_data_); // Only support one display.
227 window_tree_data_ =
228 base::MakeUnique<WindowTreeData>(std::move(window_tree_host));
229 }
161 230
231 void MusDemo::WindowTreeData::Init(
232 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
162 window_tree_host->InitHost(); 233 window_tree_host->InitHost();
163 window_tree_host->Show(); 234 window_tree_host->Show();
164 root_window_ = window_tree_host->window(); 235 root_window_ = window_tree_host->window();
165 // Take ownership of the WTH. 236 // Take ownership of the WTH.
166 window_tree_host_ = std::move(window_tree_host); 237 window_tree_host_ = std::move(window_tree_host);
167 238
168 // Initialize the window for the bitmap. 239 // Initialize the window for the bitmap.
169 window_delegate_ = new aura_extra::ImageWindowDelegate(); 240 window_delegate_ = new aura_extra::ImageWindowDelegate();
170 bitmap_window_ = base::MakeUnique<aura::Window>(window_delegate_); 241 bitmap_window_ = base::MakeUnique<aura::Window>(window_delegate_);
171 bitmap_window_->Init(LAYER_TEXTURED); 242 bitmap_window_->Init(LAYER_TEXTURED);
172 bitmap_window_->SetBounds(root_window_->bounds()); 243 bitmap_window_->SetBounds(root_window_->bounds());
173 bitmap_window_->Show(); 244 bitmap_window_->Show();
174 bitmap_window_->SetName("Bitmap"); 245 bitmap_window_->SetName("Bitmap");
175 246
176 root_window_->AddChild(bitmap_window_.get()); 247 root_window_->AddChild(bitmap_window_.get());
177 248
178 // Draw initial frame and start the timer to regularly draw frames. 249 // Draw initial frame and start the timer to regularly draw frames.
179 DrawFrame(); 250 DrawFrame();
180 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), 251 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay),
181 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); 252 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this)));
182 } 253 }
183 254
184 void MusDemo::OnWmDisplayRemoved(aura::WindowTreeHostMus* window_tree_host) { 255 void MusDemo::OnWmDisplayRemoved(aura::WindowTreeHostMus* window_tree_host) {
185 timer_.Stop(); 256 window_tree_data_.reset();
186 root_window_->RemoveChild(bitmap_window_.get()); 257 }
187 bitmap_window_.reset(); 258
259 MusDemo::WindowTreeData::~WindowTreeData() {
260 if (IsInitialized()) {
261 timer_.Stop();
262 root_window_->RemoveChild(bitmap_window_.get());
263 bitmap_window_.reset();
264 }
188 } 265 }
189 266
190 void MusDemo::OnWmDisplayModified(const display::Display& display) {} 267 void MusDemo::OnWmDisplayModified(const display::Display& display) {}
191 268
192 mojom::EventResult MusDemo::OnAccelerator(uint32_t id, const Event& event) { 269 mojom::EventResult MusDemo::OnAccelerator(uint32_t id, const Event& event) {
193 return mojom::EventResult::UNHANDLED; 270 return mojom::EventResult::UNHANDLED;
194 } 271 }
195 272
196 void MusDemo::OnWmPerformMoveLoop(aura::Window* window, 273 void MusDemo::OnWmPerformMoveLoop(aura::Window* window,
197 mojom::MoveLoopSource source, 274 mojom::MoveLoopSource source,
198 const gfx::Point& cursor_location, 275 const gfx::Point& cursor_location,
199 const base::Callback<void(bool)>& on_done) { 276 const base::Callback<void(bool)>& on_done) {
200 // Don't care 277 // Don't care
201 } 278 }
202 279
203 void MusDemo::OnWmCancelMoveLoop(aura::Window* window) {} 280 void MusDemo::OnWmCancelMoveLoop(aura::Window* window) {}
204 281
205 void MusDemo::OnWmSetClientArea( 282 void MusDemo::OnWmSetClientArea(
206 aura::Window* window, 283 aura::Window* window,
207 const gfx::Insets& insets, 284 const gfx::Insets& insets,
208 const std::vector<gfx::Rect>& additional_client_areas) {} 285 const std::vector<gfx::Rect>& additional_client_areas) {}
209 286
210 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } 287 bool MusDemo::IsWindowActive(aura::Window* window) { return false; }
211 288
212 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} 289 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {}
213 290
214 void MusDemo::DrawFrame() { 291 void MusDemo::WindowTreeData::DrawFrame() {
215 base::TimeTicks now = base::TimeTicks::Now(); 292 base::TimeTicks now = base::TimeTicks::Now();
216 293
217 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() 294 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds()
218 << "ms since the last frame was drawn."; 295 << "ms since the last frame was drawn.";
219 last_draw_frame_time_ = now; 296 last_draw_frame_time_ = now;
220 297
221 angle_ += 2.0; 298 angle_ += 2.0;
222 if (angle_ >= 360.0) 299 if (angle_ >= 360.0)
223 angle_ = 0.0; 300 angle_ = 0.0;
224 301
(...skipping 15 matching lines...) Expand all
240 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); 317 gfx::ImageSkiaRep image_skia_rep(bitmap, 1);
241 gfx::ImageSkia image_skia(image_skia_rep); 318 gfx::ImageSkia image_skia(image_skia_rep);
242 gfx::Image image(image_skia); 319 gfx::Image image(image_skia);
243 320
244 window_delegate_->SetImage(image); 321 window_delegate_->SetImage(image);
245 bitmap_window_->SchedulePaintInRect(bitmap_window_->bounds()); 322 bitmap_window_->SchedulePaintInRect(bitmap_window_->bounds());
246 } 323 }
247 324
248 } // namespace demo 325 } // namespace demo
249 } // namespace aura 326 } // namespace aura
OLDNEW
« no previous file with comments | « services/ui/demo/mus_demo.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698