OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/mus/demo/mus_demo.h" | |
6 | |
7 #include "base/time/time.h" | |
8 #include "components/bitmap_uploader/bitmap_uploader.h" | |
9 #include "components/mus/common/gpu_service.h" | |
10 #include "components/mus/public/cpp/window.h" | |
11 #include "components/mus/public/cpp/window_tree_client.h" | |
12 #include "services/shell/public/cpp/connector.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/gfx/geometry/rect.h" | |
19 | |
20 namespace mus_demo { | |
21 | |
22 namespace { | |
23 | |
24 // Milliseconds between frames. | |
25 const int64_t kFrameDelay = 33; | |
26 | |
27 // Size of square in pixels to draw. | |
28 const int kSquareSize = 300; | |
29 | |
30 const SkColor kBgColor = SK_ColorRED; | |
31 const SkColor kFgColor = SK_ColorYELLOW; | |
32 | |
33 void DrawSquare(const gfx::Rect& bounds, double angle, SkCanvas* canvas) { | |
34 // Create SkRect to draw centered inside the bounds. | |
35 gfx::Point top_left = bounds.CenterPoint(); | |
36 top_left.Offset(-kSquareSize / 2, -kSquareSize / 2); | |
37 SkRect rect = | |
38 SkRect::MakeXYWH(top_left.x(), top_left.y(), kSquareSize, kSquareSize); | |
39 | |
40 // Set SkPaint to fill solid color. | |
41 SkPaint paint; | |
42 paint.setStyle(SkPaint::kFill_Style); | |
43 paint.setColor(kFgColor); | |
44 | |
45 // Rotate the canvas. | |
46 const gfx::Size canvas_size = bounds.size(); | |
47 if (angle != 0.0) { | |
48 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), | |
49 SkFloatToScalar(canvas_size.height() * 0.5f)); | |
50 canvas->rotate(angle); | |
51 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), | |
52 -SkFloatToScalar(canvas_size.height() * 0.5f)); | |
53 } | |
54 | |
55 canvas->drawRect(rect, paint); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 MusDemo::MusDemo() {} | |
61 | |
62 MusDemo::~MusDemo() { | |
63 delete window_tree_client_; | |
64 } | |
65 | |
66 void MusDemo::Initialize(shell::Connector* connector, | |
67 const shell::Identity& identity, | |
68 uint32_t id) { | |
69 connector_ = connector; | |
70 mus::GpuService::Initialize(connector_); | |
71 window_tree_client_ = new mus::WindowTreeClient(this, this, nullptr); | |
72 window_tree_client_->ConnectAsWindowManager(connector); | |
73 } | |
74 | |
75 bool MusDemo::AcceptConnection(shell::Connection* connection) { | |
76 return true; | |
77 } | |
78 | |
79 void MusDemo::OnEmbed(mus::Window* window) { | |
80 // Not called for the WindowManager. | |
81 NOTREACHED(); | |
82 } | |
83 | |
84 void MusDemo::OnDidDestroyClient(mus::WindowTreeClient* client) { | |
85 window_tree_client_ = nullptr; | |
86 timer_.Stop(); | |
87 } | |
88 | |
89 void MusDemo::OnEventObserved(const ui::Event& event, mus::Window* target) {} | |
90 | |
91 void MusDemo::SetWindowManagerClient(mus::WindowManagerClient* client) {} | |
92 | |
93 bool MusDemo::OnWmSetBounds(mus::Window* window, gfx::Rect* bounds) { | |
94 return true; | |
95 } | |
96 | |
97 bool MusDemo::OnWmSetProperty(mus::Window* window, | |
98 const std::string& name, | |
99 std::unique_ptr<std::vector<uint8_t>>* new_data) { | |
100 return true; | |
101 } | |
102 | |
103 mus::Window* MusDemo::OnWmCreateTopLevelWindow( | |
104 std::map<std::string, std::vector<uint8_t>>* properties) { | |
105 return nullptr; | |
106 } | |
107 | |
108 void MusDemo::OnWmClientJankinessChanged( | |
109 const std::set<mus::Window*>& client_windows, | |
110 bool janky) { | |
111 // Don't care | |
112 } | |
113 | |
114 void MusDemo::OnWmNewDisplay(mus::Window* window, | |
115 const display::Display& display) { | |
116 DCHECK(!window_); // Only support one display. | |
117 window_ = window; | |
118 | |
119 // Initialize bitmap uploader for sending frames to MUS. | |
120 uploader_.reset(new bitmap_uploader::BitmapUploader(window_)); | |
121 uploader_->Init(connector_); | |
122 | |
123 // Draw initial frame and start the timer to regularly draw frames. | |
124 DrawFrame(); | |
125 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | |
126 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); | |
127 } | |
128 | |
129 void MusDemo::OnAccelerator(uint32_t id, const ui::Event& event) { | |
130 // Don't care | |
131 } | |
132 | |
133 void MusDemo::AllocBitmap() { | |
134 const gfx::Rect bounds = window_->GetBoundsInRoot(); | |
135 | |
136 // Allocate bitmap the same size as the window for drawing. | |
137 bitmap_.reset(); | |
138 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | |
139 kPremul_SkAlphaType); | |
140 bitmap_.allocPixels(image_info); | |
141 } | |
142 | |
143 void MusDemo::DrawFrame() { | |
144 angle_ += 2.0; | |
145 if (angle_ >= 360.0) | |
146 angle_ = 0.0; | |
147 | |
148 const gfx::Rect bounds = window_->GetBoundsInRoot(); | |
149 | |
150 // Check that bitmap and window sizes match, otherwise reallocate bitmap. | |
151 const SkImageInfo info = bitmap_.info(); | |
152 if (info.width() != bounds.width() || info.height() != bounds.height()) { | |
153 AllocBitmap(); | |
154 } | |
155 | |
156 // Draw the rotated square on background in bitmap. | |
157 SkCanvas canvas(bitmap_); | |
158 canvas.clear(kBgColor); | |
159 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | |
160 DrawSquare(bounds, angle_, &canvas); | |
161 canvas.flush(); | |
162 | |
163 // Copy pixels data into vector that will be passed to BitmapUploader. | |
164 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a | |
165 // SkBitmap. | |
166 bitmap_.lockPixels(); | |
167 const unsigned char* addr = | |
168 static_cast<const unsigned char*>(bitmap_.getPixels()); | |
169 const int bytes = bounds.width() * bounds.height() * 4; | |
170 std::unique_ptr<std::vector<unsigned char>> data( | |
171 new std::vector<unsigned char>(addr, addr + bytes)); | |
172 bitmap_.unlockPixels(); | |
173 | |
174 // Send frame to MUS via BitmapUploader. | |
175 uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), | |
176 bitmap_uploader::BitmapUploader::BGRA); | |
177 } | |
178 | |
179 } // namespace mus_demo | |
OLD | NEW |