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

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

Issue 2688013003: Mus Demo: Code cleanup in MusDemo::WindowTreeData (Closed)
Patch Set: Final fix before landing. 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 | « no previous file | 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/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/time/time.h" 8 #include "base/time/time.h"
9 #include "services/service_manager/public/cpp/connector.h" 9 #include "services/service_manager/public/cpp/connector.h"
10 #include "services/service_manager/public/cpp/service_context.h" 10 #include "services/service_manager/public/cpp/service_context.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 } // namespace 68 } // namespace
69 69
70 class MusDemo::WindowTreeData { 70 class MusDemo::WindowTreeData {
71 public: 71 public:
72 explicit WindowTreeData( 72 explicit WindowTreeData(
73 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { 73 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
74 Init(std::move(window_tree_host)); 74 Init(std::move(window_tree_host));
75 } 75 }
76 ~WindowTreeData();
77 76
78 private: 77 private:
79 // Initializes the window tree host and start drawing frames. 78 // Initializes the window tree host and start drawing frames.
80 void Init(std::unique_ptr<aura::WindowTreeHostMus> window_tree_host); 79 void Init(std::unique_ptr<aura::WindowTreeHostMus> window_tree_host);
81 80
82 // Draws one frame, incrementing the rotation angle. 81 // Draws one frame, incrementing the rotation angle.
83 void DrawFrame(); 82 void DrawFrame();
84 83
84 // Helper function to retrieve the window to which we draw the bitmap.
85 aura::Window* bitmap_window() {
86 DCHECK(!window_tree_host_->window()->children().empty());
87 return window_tree_host_->window()->children()[0];
88 }
89
85 // The Window tree host corresponding to this data. 90 // The Window tree host corresponding to this data.
86 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_; 91 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_;
87 92
88 // Root window of the window tree host.
89 aura::Window* root_window_ = nullptr;
90
91 // Window to which we draw the bitmap.
92 std::unique_ptr<aura::Window> bitmap_window_;
93
94 // Destroys itself when the window gets destroyed. 93 // Destroys itself when the window gets destroyed.
95 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr; 94 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr;
96 95
97 // Timer for calling DrawFrame(). 96 // Timer for calling DrawFrame().
98 base::RepeatingTimer timer_; 97 base::RepeatingTimer timer_;
99 98
100 // Current rotation angle for drawing. 99 // Current rotation angle for drawing.
101 double angle_ = 0.0; 100 double angle_ = 0.0;
102 101
103 // Last time a frame was drawn. 102 DISALLOW_COPY_AND_ASSIGN(WindowTreeData);
104 base::TimeTicks last_draw_frame_time_;
105 }; 103 };
106 104
107 MusDemo::WindowTreeData::~WindowTreeData() {
108 timer_.Stop();
109 root_window_->RemoveChild(bitmap_window_.get());
110 bitmap_window_.reset();
111 }
112
113 void MusDemo::WindowTreeData::Init( 105 void MusDemo::WindowTreeData::Init(
114 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { 106 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
115 window_tree_host->InitHost(); 107 window_tree_host->InitHost();
116 window_tree_host->Show(); 108 window_tree_host->Show();
117 root_window_ = window_tree_host->window();
118 // Take ownership of the WTH. 109 // Take ownership of the WTH.
119 window_tree_host_ = std::move(window_tree_host); 110 window_tree_host_ = std::move(window_tree_host);
120 111
121 // Initialize the window for the bitmap. 112 // Initialize the window for the bitmap.
122 window_delegate_ = new aura_extra::ImageWindowDelegate(); 113 window_delegate_ = new aura_extra::ImageWindowDelegate();
123 bitmap_window_ = base::MakeUnique<aura::Window>(window_delegate_); 114 aura::Window* root_window = window_tree_host_->window();
124 bitmap_window_->Init(LAYER_TEXTURED); 115 aura::Window* bitmap_window = new aura::Window(window_delegate_);
125 bitmap_window_->SetBounds(root_window_->bounds()); 116 bitmap_window->Init(LAYER_TEXTURED);
126 bitmap_window_->Show(); 117 bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size()));
127 bitmap_window_->SetName("Bitmap"); 118 bitmap_window->Show();
128 119 bitmap_window->SetName("Bitmap");
129 root_window_->AddChild(bitmap_window_.get()); 120 root_window->AddChild(bitmap_window);
130 121
131 // Draw initial frame and start the timer to regularly draw frames. 122 // Draw initial frame and start the timer to regularly draw frames.
132 DrawFrame(); 123 DrawFrame();
133 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), 124 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay),
134 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); 125 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this)));
135 } 126 }
136 127
137 MusDemo::MusDemo() {} 128 MusDemo::MusDemo() {}
138 129
139 MusDemo::~MusDemo() { 130 MusDemo::~MusDemo() {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 void MusDemo::OnWmSetClientArea( 241 void MusDemo::OnWmSetClientArea(
251 aura::Window* window, 242 aura::Window* window,
252 const gfx::Insets& insets, 243 const gfx::Insets& insets,
253 const std::vector<gfx::Rect>& additional_client_areas) {} 244 const std::vector<gfx::Rect>& additional_client_areas) {}
254 245
255 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } 246 bool MusDemo::IsWindowActive(aura::Window* window) { return false; }
256 247
257 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} 248 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {}
258 249
259 void MusDemo::WindowTreeData::DrawFrame() { 250 void MusDemo::WindowTreeData::DrawFrame() {
260 base::TimeTicks now = base::TimeTicks::Now();
261
262 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds()
263 << "ms since the last frame was drawn.";
264 last_draw_frame_time_ = now;
265
266 angle_ += 2.0; 251 angle_ += 2.0;
267 if (angle_ >= 360.0) 252 if (angle_ >= 360.0)
268 angle_ = 0.0; 253 angle_ = 0.0;
269 254
270 const gfx::Rect& bounds = bitmap_window_->bounds(); 255 const gfx::Rect& bounds = bitmap_window()->bounds();
271 256
272 // Allocate a bitmap of correct size. 257 // Allocate a bitmap of correct size.
273 SkBitmap bitmap; 258 SkBitmap bitmap;
274 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), 259 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(),
275 kPremul_SkAlphaType); 260 kPremul_SkAlphaType);
276 bitmap.allocPixels(image_info); 261 bitmap.allocPixels(image_info);
277 262
278 // Draw the rotated square on background in bitmap. 263 // Draw the rotated square on background in bitmap.
279 SkCanvas canvas(bitmap); 264 SkCanvas canvas(bitmap);
280 canvas.clear(kBgColor); 265 canvas.clear(kBgColor);
281 // TODO(kylechar): Add GL drawing instead of software rasterization in future. 266 // TODO(kylechar): Add GL drawing instead of software rasterization in future.
282 DrawSquare(bounds, angle_, &canvas); 267 DrawSquare(bounds, angle_, &canvas);
283 canvas.flush(); 268 canvas.flush();
284 269
285 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); 270 gfx::ImageSkiaRep image_skia_rep(bitmap, 1);
286 gfx::ImageSkia image_skia(image_skia_rep); 271 gfx::ImageSkia image_skia(image_skia_rep);
287 gfx::Image image(image_skia); 272 gfx::Image image(image_skia);
288 273
289 window_delegate_->SetImage(image); 274 window_delegate_->SetImage(image);
290 bitmap_window_->SchedulePaintInRect(bitmap_window_->bounds()); 275 bitmap_window()->SchedulePaintInRect(gfx::Rect(bounds.size()));
291 } 276 }
292 277
293 } // namespace demo 278 } // namespace demo
294 } // namespace aura 279 } // namespace aura
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698