| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 canvas->rotate(angle); | 62 canvas->rotate(angle); |
| 63 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), | 63 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
| 64 -SkFloatToScalar(canvas_size.height() * 0.5f)); | 64 -SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 65 } | 65 } |
| 66 | 66 |
| 67 canvas->drawRect(rect, paint); | 67 canvas->drawRect(rect, paint); |
| 68 } | 68 } |
| 69 | 69 |
| 70 } // namespace | 70 } // namespace |
| 71 | 71 |
| 72 class MusDemo::WindowTreeData { | 72 MusDemo::WindowTreeData::WindowTreeData( |
| 73 public: | 73 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host, |
| 74 explicit WindowTreeData( | 74 int square_size) |
| 75 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host, | 75 : square_size_(square_size) { |
| 76 int square_size) | 76 Init(std::move(window_tree_host)); |
| 77 : square_size_(square_size) { | 77 } |
| 78 Init(std::move(window_tree_host)); | |
| 79 } | |
| 80 | 78 |
| 81 private: | 79 MusDemo::WindowTreeData::~WindowTreeData() {} |
| 82 // Initializes the window tree host and start drawing frames. | |
| 83 void Init(std::unique_ptr<aura::WindowTreeHostMus> window_tree_host); | |
| 84 | 80 |
| 85 // Draws one frame, incrementing the rotation angle. | 81 aura::Window* MusDemo::WindowTreeData::bitmap_window() { |
| 86 void DrawFrame(); | 82 DCHECK(!window_tree_host_->window()->children().empty()); |
| 87 | 83 return window_tree_host_->window()->children()[0]; |
| 88 // Helper function to retrieve the window to which we draw the bitmap. | 84 } |
| 89 aura::Window* bitmap_window() { | |
| 90 DCHECK(!window_tree_host_->window()->children().empty()); | |
| 91 return window_tree_host_->window()->children()[0]; | |
| 92 } | |
| 93 | |
| 94 // The Window tree host corresponding to this data. | |
| 95 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host_; | |
| 96 | |
| 97 // Destroys itself when the window gets destroyed. | |
| 98 aura_extra::ImageWindowDelegate* window_delegate_ = nullptr; | |
| 99 | |
| 100 // Timer for calling DrawFrame(). | |
| 101 base::RepeatingTimer timer_; | |
| 102 | |
| 103 // Current rotation angle for drawing. | |
| 104 double angle_ = 0.0; | |
| 105 | |
| 106 // Size in pixels of the square to draw. | |
| 107 const int square_size_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(WindowTreeData); | |
| 110 }; | |
| 111 | 85 |
| 112 void MusDemo::WindowTreeData::Init( | 86 void MusDemo::WindowTreeData::Init( |
| 113 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { | 87 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { |
| 114 window_tree_host->InitHost(); | 88 window_tree_host->InitHost(); |
| 115 window_tree_host->Show(); | 89 window_tree_host->Show(); |
| 116 // Take ownership of the WTH. | 90 // Take ownership of the WTH. |
| 117 window_tree_host_ = std::move(window_tree_host); | 91 window_tree_host_ = std::move(window_tree_host); |
| 118 | 92 |
| 119 // Initialize the window for the bitmap. | 93 // Initialize the window for the bitmap. |
| 120 window_delegate_ = new aura_extra::ImageWindowDelegate(); | 94 window_delegate_ = new aura_extra::ImageWindowDelegate(); |
| 121 aura::Window* root_window = window_tree_host_->window(); | 95 aura::Window* root_window = window_tree_host_->window(); |
| 122 aura::Window* bitmap_window = new aura::Window(window_delegate_); | 96 aura::Window* bitmap_window = new aura::Window(window_delegate_); |
| 123 bitmap_window->Init(LAYER_TEXTURED); | 97 bitmap_window->Init(LAYER_TEXTURED); |
| 124 bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size())); | 98 bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size())); |
| 125 bitmap_window->Show(); | 99 bitmap_window->Show(); |
| 126 bitmap_window->SetName("Bitmap"); | 100 bitmap_window->SetName("Bitmap"); |
| 127 root_window->AddChild(bitmap_window); | 101 root_window->AddChild(bitmap_window); |
| 128 | 102 |
| 129 // Draw initial frame and start the timer to regularly draw frames. | 103 // Draw initial frame and start the timer to regularly draw frames. |
| 130 DrawFrame(); | 104 DrawFrame(); |
| 131 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | 105 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| 132 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); | 106 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); |
| 133 } | 107 } |
| 134 | 108 |
| 109 void MusDemo::WindowTreeData::DrawFrame() { |
| 110 angle_ += 2.0; |
| 111 if (angle_ >= 360.0) |
| 112 angle_ = 0.0; |
| 113 |
| 114 const gfx::Rect& bounds = bitmap_window()->bounds(); |
| 115 |
| 116 // Allocate a bitmap of correct size. |
| 117 SkBitmap bitmap; |
| 118 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 119 kPremul_SkAlphaType); |
| 120 bitmap.allocPixels(image_info); |
| 121 |
| 122 // Draw the rotated square on background in bitmap. |
| 123 SkCanvas canvas(bitmap); |
| 124 canvas.clear(kBgColor); |
| 125 // TODO(kylechar): Add GL drawing instead of software rasterization in future. |
| 126 DrawSquare(bounds, angle_, &canvas, square_size_); |
| 127 canvas.flush(); |
| 128 |
| 129 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); |
| 130 gfx::ImageSkia image_skia(image_skia_rep); |
| 131 gfx::Image image(image_skia); |
| 132 |
| 133 window_delegate_->SetImage(image); |
| 134 bitmap_window()->SchedulePaintInRect(gfx::Rect(bounds.size())); |
| 135 } |
| 136 |
| 135 MusDemo::MusDemo() {} | 137 MusDemo::MusDemo() {} |
| 136 | 138 |
| 137 MusDemo::~MusDemo() { | 139 MusDemo::~MusDemo() { |
| 138 display::Screen::SetScreenInstance(nullptr); | 140 display::Screen::SetScreenInstance(nullptr); |
| 139 } | 141 } |
| 140 | 142 |
| 141 void MusDemo::OnStart() { | 143 void MusDemo::OnStart() { |
| 142 screen_ = base::MakeUnique<display::ScreenBase>(); | 144 screen_ = base::MakeUnique<display::ScreenBase>(); |
| 143 display::Screen::SetScreenInstance(screen_.get()); | 145 display::Screen::SetScreenInstance(screen_.get()); |
| 144 | 146 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 249 |
| 248 void MusDemo::OnWmSetClientArea( | 250 void MusDemo::OnWmSetClientArea( |
| 249 aura::Window* window, | 251 aura::Window* window, |
| 250 const gfx::Insets& insets, | 252 const gfx::Insets& insets, |
| 251 const std::vector<gfx::Rect>& additional_client_areas) {} | 253 const std::vector<gfx::Rect>& additional_client_areas) {} |
| 252 | 254 |
| 253 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } | 255 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } |
| 254 | 256 |
| 255 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} | 257 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} |
| 256 | 258 |
| 257 void MusDemo::WindowTreeData::DrawFrame() { | |
| 258 angle_ += 2.0; | |
| 259 if (angle_ >= 360.0) | |
| 260 angle_ = 0.0; | |
| 261 | |
| 262 const gfx::Rect& bounds = bitmap_window()->bounds(); | |
| 263 | |
| 264 // Allocate a bitmap of correct size. | |
| 265 SkBitmap bitmap; | |
| 266 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | |
| 267 kPremul_SkAlphaType); | |
| 268 bitmap.allocPixels(image_info); | |
| 269 | |
| 270 // Draw the rotated square on background in bitmap. | |
| 271 SkCanvas canvas(bitmap); | |
| 272 canvas.clear(kBgColor); | |
| 273 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | |
| 274 DrawSquare(bounds, angle_, &canvas, square_size_); | |
| 275 canvas.flush(); | |
| 276 | |
| 277 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); | |
| 278 gfx::ImageSkia image_skia(image_skia_rep); | |
| 279 gfx::Image image(image_skia); | |
| 280 | |
| 281 window_delegate_->SetImage(image); | |
| 282 bitmap_window()->SchedulePaintInRect(gfx::Rect(bounds.size())); | |
| 283 } | |
| 284 | |
| 285 } // namespace demo | 259 } // namespace demo |
| 286 } // namespace aura | 260 } // namespace aura |
| OLD | NEW |