 Chromium Code Reviews
 Chromium Code Reviews Issue 2688013003:
  Mus Demo: Code cleanup in MusDemo::WindowTreeData  (Closed)
    
  
    Issue 2688013003:
  Mus Demo: Code cleanup in MusDemo::WindowTreeData  (Closed) 
  | OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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* BitmapWindow() { | |
| 
sky
2017/02/10 18:48:37
GetBitmapWindow(), or as this is inlined and cheap
 
fwang
2017/02/11 09:58:48
Done.
 | |
| 86 return window_tree_host_->window()->children()[0]; | |
| 
sky
2017/02/10 18:48:38
DCHECK(!children.empty())
 
fwang
2017/02/11 09:58:48
Done.
 | |
| 87 } | |
| 88 | |
| 85 // The Window tree host corresponding to this data. | 89 // The Window tree host corresponding to this data. | 
| 86 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_; | 90 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_; | 
| 87 | 91 | 
| 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. | 92 // Destroys itself when the window gets destroyed. | 
| 95 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr; | 93 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr; | 
| 96 | 94 | 
| 97 // Timer for calling DrawFrame(). | 95 // Timer for calling DrawFrame(). | 
| 98 base::RepeatingTimer timer_; | 96 base::RepeatingTimer timer_; | 
| 99 | 97 | 
| 100 // Current rotation angle for drawing. | 98 // Current rotation angle for drawing. | 
| 101 double angle_ = 0.0; | 99 double angle_ = 0.0; | 
| 102 | |
| 103 // Last time a frame was drawn. | |
| 104 base::TimeTicks last_draw_frame_time_; | |
| 105 }; | 100 }; | 
| 
sky
2017/02/10 18:48:38
DISALLOW...
 
fwang
2017/02/11 09:58:48
Done.
 | |
| 106 | 101 | 
| 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( | 102 void MusDemo::WindowTreeData::Init( | 
| 114 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { | 103 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { | 
| 115 window_tree_host->InitHost(); | 104 window_tree_host->InitHost(); | 
| 116 window_tree_host->Show(); | 105 window_tree_host->Show(); | 
| 117 root_window_ = window_tree_host->window(); | |
| 118 // Take ownership of the WTH. | 106 // Take ownership of the WTH. | 
| 119 window_tree_host_ = std::move(window_tree_host); | 107 window_tree_host_ = std::move(window_tree_host); | 
| 120 | 108 | 
| 121 // Initialize the window for the bitmap. | 109 // Initialize the window for the bitmap. | 
| 122 window_delegate_ = new aura_extra::ImageWindowDelegate(); | 110 window_delegate_ = new aura_extra::ImageWindowDelegate(); | 
| 123 bitmap_window_ = base::MakeUnique<aura::Window>(window_delegate_); | 111 aura::Window* root_window = window_tree_host_->window(); | 
| 124 bitmap_window_->Init(LAYER_TEXTURED); | 112 aura::Window* bitmap_window = new aura::Window(window_delegate_); | 
| 125 bitmap_window_->SetBounds(root_window_->bounds()); | 113 bitmap_window->Init(LAYER_TEXTURED); | 
| 126 bitmap_window_->Show(); | 114 bitmap_window->SetBounds(root_window->bounds()); | 
| 
sky
2017/02/10 18:48:38
bitmap_window should have the same size, but an or
 
fwang
2017/02/11 09:58:48
Done.
 | |
| 127 bitmap_window_->SetName("Bitmap"); | 115 bitmap_window->Show(); | 
| 128 | 116 bitmap_window->SetName("Bitmap"); | 
| 129 root_window_->AddChild(bitmap_window_.get()); | 117 root_window->AddChild(bitmap_window); | 
| 130 | 118 | 
| 131 // Draw initial frame and start the timer to regularly draw frames. | 119 // Draw initial frame and start the timer to regularly draw frames. | 
| 132 DrawFrame(); | 120 DrawFrame(); | 
| 133 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | 121 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | 
| 134 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); | 122 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); | 
| 135 } | 123 } | 
| 136 | 124 | 
| 137 MusDemo::MusDemo() {} | 125 MusDemo::MusDemo() {} | 
| 138 | 126 | 
| 139 MusDemo::~MusDemo() { | 127 MusDemo::~MusDemo() { | 
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 void MusDemo::OnWmSetClientArea( | 238 void MusDemo::OnWmSetClientArea( | 
| 251 aura::Window* window, | 239 aura::Window* window, | 
| 252 const gfx::Insets& insets, | 240 const gfx::Insets& insets, | 
| 253 const std::vector<gfx::Rect>& additional_client_areas) {} | 241 const std::vector<gfx::Rect>& additional_client_areas) {} | 
| 254 | 242 | 
| 255 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } | 243 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } | 
| 256 | 244 | 
| 257 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} | 245 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} | 
| 258 | 246 | 
| 259 void MusDemo::WindowTreeData::DrawFrame() { | 247 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; | 248 angle_ += 2.0; | 
| 267 if (angle_ >= 360.0) | 249 if (angle_ >= 360.0) | 
| 268 angle_ = 0.0; | 250 angle_ = 0.0; | 
| 269 | 251 | 
| 270 const gfx::Rect& bounds = bitmap_window_->bounds(); | 252 const gfx::Rect& bounds = BitmapWindow()->bounds(); | 
| 271 | 253 | 
| 272 // Allocate a bitmap of correct size. | 254 // Allocate a bitmap of correct size. | 
| 273 SkBitmap bitmap; | 255 SkBitmap bitmap; | 
| 274 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | 256 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | 
| 275 kPremul_SkAlphaType); | 257 kPremul_SkAlphaType); | 
| 276 bitmap.allocPixels(image_info); | 258 bitmap.allocPixels(image_info); | 
| 277 | 259 | 
| 278 // Draw the rotated square on background in bitmap. | 260 // Draw the rotated square on background in bitmap. | 
| 279 SkCanvas canvas(bitmap); | 261 SkCanvas canvas(bitmap); | 
| 280 canvas.clear(kBgColor); | 262 canvas.clear(kBgColor); | 
| 281 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | 263 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | 
| 282 DrawSquare(bounds, angle_, &canvas); | 264 DrawSquare(bounds, angle_, &canvas); | 
| 283 canvas.flush(); | 265 canvas.flush(); | 
| 284 | 266 | 
| 285 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); | 267 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); | 
| 286 gfx::ImageSkia image_skia(image_skia_rep); | 268 gfx::ImageSkia image_skia(image_skia_rep); | 
| 287 gfx::Image image(image_skia); | 269 gfx::Image image(image_skia); | 
| 288 | 270 | 
| 289 window_delegate_->SetImage(image); | 271 window_delegate_->SetImage(image); | 
| 290 bitmap_window_->SchedulePaintInRect(bitmap_window_->bounds()); | 272 BitmapWindow()->SchedulePaintInRect(BitmapWindow()->bounds()); | 
| 
sky
2017/02/10 18:48:37
SchedulePaintInRect() takes a rectangle in the coo
 
fwang
2017/02/11 09:58:48
Done.
 | |
| 291 } | 273 } | 
| 292 | 274 | 
| 293 } // namespace demo | 275 } // namespace demo | 
| 294 } // namespace aura | 276 } // namespace aura | 
| OLD | NEW |