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

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

Issue 2697013004: Reland Mus Demo: Move definition of WindowTreeData into separate files (Closed)
Patch Set: 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') | services/ui/demo/window_tree_data.h » ('j') | 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"
9 #include "services/service_manager/public/cpp/connector.h" 8 #include "services/service_manager/public/cpp/connector.h"
10 #include "services/service_manager/public/cpp/service_context.h" 9 #include "services/service_manager/public/cpp/service_context.h"
10 #include "services/ui/demo/window_tree_data.h"
11 #include "services/ui/public/cpp/gpu/gpu.h" 11 #include "services/ui/public/cpp/gpu/gpu.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkImageInfo.h"
16 #include "third_party/skia/include/core/SkPaint.h"
17 #include "third_party/skia/include/core/SkRect.h"
18 #include "ui/aura/client/default_capture_client.h" 12 #include "ui/aura/client/default_capture_client.h"
19 #include "ui/aura/env.h" 13 #include "ui/aura/env.h"
20 #include "ui/aura/mus/property_converter.h" 14 #include "ui/aura/mus/property_converter.h"
21 #include "ui/aura/mus/window_tree_client.h" 15 #include "ui/aura/mus/window_tree_client.h"
22 #include "ui/aura/mus/window_tree_host_mus.h" 16 #include "ui/aura/mus/window_tree_host_mus.h"
23 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
24 #include "ui/aura_extra/image_window_delegate.h"
25 #include "ui/gfx/geometry/rect.h" 18 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/image/image.h"
27 #include "ui/wm/core/wm_state.h" 19 #include "ui/wm/core/wm_state.h"
28 20
29 namespace ui { 21 namespace ui {
30 namespace demo { 22 namespace demo {
31 23
32 namespace { 24 namespace {
33 25
34 // Milliseconds between frames.
35 const int64_t kFrameDelay = 33;
36
37 // Size of square in pixels to draw. 26 // Size of square in pixels to draw.
38 const int kSquareSize = 300; 27 const int kSquareSize = 300;
39 28
40 const SkColor kBgColor = SK_ColorRED;
41 const SkColor kFgColor = SK_ColorYELLOW;
42
43 void DrawSquare(const gfx::Rect& bounds,
44 double angle,
45 SkCanvas* canvas,
46 int size) {
47 // Create SkRect to draw centered inside the bounds.
48 gfx::Point top_left = bounds.CenterPoint();
49 top_left.Offset(-size / 2, -size / 2);
50 SkRect rect = SkRect::MakeXYWH(top_left.x(), top_left.y(), size, size);
51
52 // Set SkPaint to fill solid color.
53 SkPaint paint;
54 paint.setStyle(SkPaint::kFill_Style);
55 paint.setColor(kFgColor);
56
57 // Rotate the canvas.
58 const gfx::Size canvas_size = bounds.size();
59 if (angle != 0.0) {
60 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f),
61 SkFloatToScalar(canvas_size.height() * 0.5f));
62 canvas->rotate(angle);
63 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f),
64 -SkFloatToScalar(canvas_size.height() * 0.5f));
65 }
66
67 canvas->drawRect(rect, paint);
68 }
69
70 } // namespace
71
72 class MusDemo::WindowTreeData {
73 public:
74 explicit WindowTreeData(
75 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host,
76 int square_size)
77 : square_size_(square_size) {
78 Init(std::move(window_tree_host));
79 }
80
81 private:
82 // Initializes the window tree host and start drawing frames.
83 void Init(std::unique_ptr<aura::WindowTreeHostMus> window_tree_host);
84
85 // Draws one frame, incrementing the rotation angle.
86 void DrawFrame();
87
88 // Helper function to retrieve the window to which we draw the bitmap.
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
112 void MusDemo::WindowTreeData::Init(
113 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
114 window_tree_host->InitHost();
115 window_tree_host->Show();
116 // Take ownership of the WTH.
117 window_tree_host_ = std::move(window_tree_host);
118
119 // Initialize the window for the bitmap.
120 window_delegate_ = new aura_extra::ImageWindowDelegate();
121 aura::Window* root_window = window_tree_host_->window();
122 aura::Window* bitmap_window = new aura::Window(window_delegate_);
123 bitmap_window->Init(LAYER_TEXTURED);
124 bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size()));
125 bitmap_window->Show();
126 bitmap_window->SetName("Bitmap");
127 root_window->AddChild(bitmap_window);
128
129 // Draw initial frame and start the timer to regularly draw frames.
130 DrawFrame();
131 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay),
132 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this)));
133 } 29 }
134 30
135 MusDemo::MusDemo() {} 31 MusDemo::MusDemo() {}
136 32
137 MusDemo::~MusDemo() { 33 MusDemo::~MusDemo() {
138 display::Screen::SetScreenInstance(nullptr); 34 display::Screen::SetScreenInstance(nullptr);
139 } 35 }
140 36
141 void MusDemo::OnStart() { 37 void MusDemo::OnStart() {
142 screen_ = base::MakeUnique<display::ScreenBase>(); 38 screen_ = base::MakeUnique<display::ScreenBase>();
143 display::Screen::SetScreenInstance(screen_.get()); 39 display::Screen::SetScreenInstance(screen_.get());
144 40
145 env_ = aura::Env::CreateInstance(aura::Env::Mode::MUS); 41 env_ = aura::Env::CreateInstance(aura::Env::Mode::MUS);
146 capture_client_ = base::MakeUnique<aura::client::DefaultCaptureClient>(); 42 capture_client_ = base::MakeUnique<aura::client::DefaultCaptureClient>();
147 property_converter_ = base::MakeUnique<aura::PropertyConverter>(); 43 property_converter_ = base::MakeUnique<aura::PropertyConverter>();
148 wm_state_ = base::MakeUnique<::wm::WMState>(); 44 wm_state_ = base::MakeUnique<::wm::WMState>();
149 45
150 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>( 46 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>(
151 context()->connector(), this, this); 47 context()->connector(), this, this);
152 window_tree_client_->ConnectAsWindowManager(); 48 window_tree_client_->ConnectAsWindowManager();
153 49
50 window_tree_data_ = base::MakeUnique<WindowTreeData>(kSquareSize);
51
154 env_->SetWindowTreeClient(window_tree_client_.get()); 52 env_->SetWindowTreeClient(window_tree_client_.get());
155 } 53 }
156 54
157 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, 55 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info,
158 service_manager::InterfaceRegistry* registry) { 56 service_manager::InterfaceRegistry* registry) {
159 return true; 57 return true;
160 } 58 }
161 59
162 void MusDemo::OnEmbed( 60 void MusDemo::OnEmbed(
163 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { 61 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 112 }
215 113
216 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) { 114 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) {
217 screen_->display_list().AddDisplay(display, 115 screen_->display_list().AddDisplay(display,
218 display::DisplayList::Type::PRIMARY); 116 display::DisplayList::Type::PRIMARY);
219 } 117 }
220 118
221 void MusDemo::OnWmNewDisplay( 119 void MusDemo::OnWmNewDisplay(
222 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host, 120 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host,
223 const display::Display& display) { 121 const display::Display& display) {
224 DCHECK(!window_tree_data_); // Only support one display. 122 DCHECK(!window_tree_data_->IsInitialized()); // Only support one display.
225 window_tree_data_ = base::MakeUnique<WindowTreeData>( 123 window_tree_data_->Init(std::move(window_tree_host));
226 std::move(window_tree_host), kSquareSize);
227 } 124 }
228 125
229 void MusDemo::OnWmDisplayRemoved(aura::WindowTreeHostMus* window_tree_host) { 126 void MusDemo::OnWmDisplayRemoved(aura::WindowTreeHostMus* window_tree_host) {
230 window_tree_data_.reset(); 127 window_tree_data_.reset();
231 } 128 }
232 129
233 void MusDemo::OnWmDisplayModified(const display::Display& display) {} 130 void MusDemo::OnWmDisplayModified(const display::Display& display) {}
234 131
235 mojom::EventResult MusDemo::OnAccelerator(uint32_t id, const Event& event) { 132 mojom::EventResult MusDemo::OnAccelerator(uint32_t id, const Event& event) {
236 return mojom::EventResult::UNHANDLED; 133 return mojom::EventResult::UNHANDLED;
(...skipping 10 matching lines...) Expand all
247 144
248 void MusDemo::OnWmSetClientArea( 145 void MusDemo::OnWmSetClientArea(
249 aura::Window* window, 146 aura::Window* window,
250 const gfx::Insets& insets, 147 const gfx::Insets& insets,
251 const std::vector<gfx::Rect>& additional_client_areas) {} 148 const std::vector<gfx::Rect>& additional_client_areas) {}
252 149
253 bool MusDemo::IsWindowActive(aura::Window* window) { return false; } 150 bool MusDemo::IsWindowActive(aura::Window* window) { return false; }
254 151
255 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {} 152 void MusDemo::OnWmDeactivateWindow(aura::Window* window) {}
256 153
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 154 } // namespace demo
286 } // namespace aura 155 } // namespace aura
OLDNEW
« no previous file with comments | « services/ui/demo/mus_demo.h ('k') | services/ui/demo/window_tree_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698