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" |
11 #include "services/ui/demo/bitmap_uploader.h" | 11 #include "services/ui/demo/bitmap_uploader.h" |
12 #include "services/ui/public/cpp/gpu_service.h" | 12 #include "services/ui/public/cpp/gpu_service.h" |
13 #include "services/ui/public/cpp/window.h" | 13 #include "services/ui/public/cpp/window.h" |
14 #include "services/ui/public/cpp/window_tree_client.h" | 14 #include "services/ui/public/cpp/window_tree_client.h" |
15 #include "services/ui/public/cpp/window_tree_host_factory.h" | |
15 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
17 #include "third_party/skia/include/core/SkImageInfo.h" | 18 #include "third_party/skia/include/core/SkImageInfo.h" |
18 #include "third_party/skia/include/core/SkPaint.h" | 19 #include "third_party/skia/include/core/SkPaint.h" |
19 #include "third_party/skia/include/core/SkRect.h" | 20 #include "third_party/skia/include/core/SkRect.h" |
20 #include "ui/gfx/geometry/rect.h" | 21 #include "ui/gfx/geometry/rect.h" |
21 | 22 |
22 namespace ui { | 23 namespace ui { |
23 namespace demo { | 24 namespace demo { |
24 | 25 |
(...skipping 26 matching lines...) Expand all Loading... | |
51 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), | 52 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), |
52 SkFloatToScalar(canvas_size.height() * 0.5f)); | 53 SkFloatToScalar(canvas_size.height() * 0.5f)); |
53 canvas->rotate(angle); | 54 canvas->rotate(angle); |
54 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), | 55 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
55 -SkFloatToScalar(canvas_size.height() * 0.5f)); | 56 -SkFloatToScalar(canvas_size.height() * 0.5f)); |
56 } | 57 } |
57 | 58 |
58 canvas->drawRect(rect, paint); | 59 canvas->drawRect(rect, paint); |
59 } | 60 } |
60 | 61 |
62 SkBitmap AllocBitmap(Window* window) { | |
63 const gfx::Rect bounds = window->GetBoundsInRoot(); | |
64 | |
65 // Allocate bitmap the same size as the window for drawing. | |
66 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | |
67 kPremul_SkAlphaType); | |
68 SkBitmap bitmap; | |
69 bitmap.allocPixels(image_info); | |
70 return bitmap; | |
71 } | |
72 | |
61 } // namespace | 73 } // namespace |
62 | 74 |
75 struct MusDemo::WindowTreeData { | |
76 mojom::WindowTreeHostPtr host; | |
77 | |
78 std::unique_ptr<WindowTreeClient> window_tree; | |
79 | |
80 Window* window = nullptr; | |
81 | |
82 // Used to send frames to mus. | |
83 std::unique_ptr<BitmapUploader> uploader; | |
84 | |
85 // Bitmap that is the same size as our client window area. | |
86 SkBitmap bitmap; | |
87 | |
88 // Current rotation angle for drawing. | |
89 double angle = 0.0; | |
90 | |
91 // Timer for calling DrawFrame(). | |
92 base::RepeatingTimer timer; | |
93 }; | |
94 | |
63 MusDemo::MusDemo() {} | 95 MusDemo::MusDemo() {} |
64 | 96 |
65 MusDemo::~MusDemo() { | 97 MusDemo::~MusDemo() { |
66 display::Screen::SetScreenInstance(nullptr); | 98 display::Screen::SetScreenInstance(nullptr); |
67 } | 99 } |
68 | 100 |
69 void MusDemo::OnStart() { | 101 void MusDemo::OnStart() { |
70 screen_ = base::MakeUnique<display::ScreenBase>(); | 102 screen_ = base::MakeUnique<display::ScreenBase>(); |
71 display::Screen::SetScreenInstance(screen_.get()); | 103 display::Screen::SetScreenInstance(screen_.get()); |
72 gpu_service_ = GpuService::Create(context()->connector()); | 104 gpu_service_ = GpuService::Create(context()->connector()); |
73 window_tree_client_ = base::MakeUnique<WindowTreeClient>(this, this); | 105 AddWindowTree(); |
74 window_tree_client_->ConnectAsWindowManager(context()->connector()); | 106 AddWindowTree(); |
Tom (Use chromium acct)
2016/11/18 20:27:56
how do you propose we fix the chromebook case? Ju
rjkroege
2016/11/18 22:47:13
my thought was via command line flag.
| |
75 } | 107 } |
76 | 108 |
77 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, | 109 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, |
78 service_manager::InterfaceRegistry* registry) { | 110 service_manager::InterfaceRegistry* registry) { |
79 return true; | 111 return true; |
80 } | 112 } |
81 | 113 |
82 void MusDemo::OnEmbed(Window* window) { | 114 void MusDemo::OnEmbed(Window* window) { |
83 // Not called for the WindowManager. | 115 auto it = |
84 NOTREACHED(); | 116 std::find_if(window_tree_datas_.begin(), window_tree_datas_.end(), |
117 [window](std::unique_ptr<WindowTreeData>& data) { | |
118 return data->window_tree.get() == window->window_tree(); | |
119 }); | |
120 DCHECK(it != window_tree_datas_.end()); | |
121 auto& data = *it; | |
122 | |
123 data->window = window; | |
124 | |
125 // Initialize bitmap uploader for sending frames to MUS. | |
126 data->uploader.reset(new BitmapUploader(window)); | |
127 data->uploader->Init(gpu_service_.get()); | |
128 | |
129 // Draw initial frame and start the timer to regularly draw frames. | |
130 DrawFrame(data.get()); | |
131 data->timer.Start( | |
132 FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | |
133 base::Bind(&MusDemo::DrawFrame, base::Unretained(this), data.get())); | |
85 } | 134 } |
86 | 135 |
87 void MusDemo::OnEmbedRootDestroyed(Window* root) { | 136 void MusDemo::OnEmbedRootDestroyed(Window* root) {} |
88 // Not called for the WindowManager. | |
89 NOTREACHED(); | |
90 } | |
91 | 137 |
92 void MusDemo::OnLostConnection(WindowTreeClient* client) { | 138 void MusDemo::OnLostConnection(WindowTreeClient* client) { |
93 window_ = nullptr; | 139 window_tree_datas_.clear(); |
94 window_tree_client_.reset(); | |
95 timer_.Stop(); | |
96 } | 140 } |
97 | 141 |
98 void MusDemo::OnPointerEventObserved(const PointerEvent& event, | 142 void MusDemo::OnPointerEventObserved(const PointerEvent& event, |
99 Window* target) {} | 143 Window* target) {} |
100 | 144 |
101 void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} | 145 void MusDemo::AddWindowTree() { |
102 | 146 std::unique_ptr<WindowTreeData> data = base::MakeUnique<WindowTreeData>(); |
103 bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { | 147 data->window_tree = |
104 return true; | 148 CreateWindowTreeHost(context()->connector(), this, &data->host, nullptr); |
149 data->window = nullptr; | |
150 window_tree_datas_.push_back(std::move(data)); | |
105 } | 151 } |
106 | 152 |
107 bool MusDemo::OnWmSetProperty(Window* window, | 153 void MusDemo::DrawFrame(WindowTreeData* data) { |
108 const std::string& name, | 154 data->angle += 2.0; |
109 std::unique_ptr<std::vector<uint8_t>>* new_data) { | 155 if (data->angle >= 360.0) |
110 return true; | 156 data->angle = 0.0; |
111 } | |
112 | 157 |
113 Window* MusDemo::OnWmCreateTopLevelWindow( | 158 const gfx::Rect bounds = data->window->GetBoundsInRoot(); |
114 std::map<std::string, std::vector<uint8_t>>* properties) { | |
115 return nullptr; | |
116 } | |
117 | |
118 void MusDemo::OnWmClientJankinessChanged( | |
119 const std::set<Window*>& client_windows, | |
120 bool janky) { | |
121 // Don't care | |
122 } | |
123 | |
124 void MusDemo::OnWmNewDisplay(Window* window, const display::Display& display) { | |
rjkroege
2016/11/18 22:47:13
afaik, without an implementation of this method, t
| |
125 DCHECK(!window_); // Only support one display. | |
126 window_ = window; | |
127 | |
128 // Initialize bitmap uploader for sending frames to MUS. | |
129 uploader_.reset(new BitmapUploader(window_)); | |
130 uploader_->Init(gpu_service_.get()); | |
131 | |
132 // Draw initial frame and start the timer to regularly draw frames. | |
133 DrawFrame(); | |
134 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | |
135 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); | |
136 } | |
137 | |
138 void MusDemo::OnWmDisplayRemoved(ui::Window* window) { | |
139 window->Destroy(); | |
140 } | |
141 | |
142 void MusDemo::OnWmDisplayModified(const display::Display& display) {} | |
143 | |
144 void MusDemo::OnWmPerformMoveLoop(Window* window, | |
145 mojom::MoveLoopSource source, | |
146 const gfx::Point& cursor_location, | |
147 const base::Callback<void(bool)>& on_done) { | |
148 // Don't care | |
149 } | |
150 | |
151 void MusDemo::OnWmCancelMoveLoop(Window* window) {} | |
152 | |
153 void MusDemo::AllocBitmap() { | |
154 const gfx::Rect bounds = window_->GetBoundsInRoot(); | |
155 | |
156 // Allocate bitmap the same size as the window for drawing. | |
157 bitmap_.reset(); | |
158 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | |
159 kPremul_SkAlphaType); | |
160 bitmap_.allocPixels(image_info); | |
161 } | |
162 | |
163 void MusDemo::DrawFrame() { | |
164 base::TimeTicks now = base::TimeTicks::Now(); | |
165 | |
166 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() | |
167 << "ms since the last frame was drawn."; | |
168 last_draw_frame_time_ = now; | |
169 | |
170 angle_ += 2.0; | |
171 if (angle_ >= 360.0) | |
172 angle_ = 0.0; | |
173 | |
174 const gfx::Rect bounds = window_->GetBoundsInRoot(); | |
175 | 159 |
176 // Check that bitmap and window sizes match, otherwise reallocate bitmap. | 160 // Check that bitmap and window sizes match, otherwise reallocate bitmap. |
177 const SkImageInfo info = bitmap_.info(); | 161 const SkImageInfo info = data->bitmap.info(); |
178 if (info.width() != bounds.width() || info.height() != bounds.height()) { | 162 if (info.width() != bounds.width() || info.height() != bounds.height()) |
179 AllocBitmap(); | 163 data->bitmap = AllocBitmap(data->window); |
180 } | |
181 | 164 |
182 // Draw the rotated square on background in bitmap. | 165 // Draw the rotated square on background in bitmap. |
183 SkCanvas canvas(bitmap_); | 166 SkCanvas canvas(data->bitmap); |
184 canvas.clear(kBgColor); | 167 canvas.clear(kBgColor); |
185 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | 168 // TODO(kylechar): Add GL drawing instead of software rasterization in |
186 DrawSquare(bounds, angle_, &canvas); | 169 // future. |
170 DrawSquare(bounds, data->angle, &canvas); | |
187 canvas.flush(); | 171 canvas.flush(); |
188 | 172 |
189 // Copy pixels data into vector that will be passed to BitmapUploader. | 173 // Copy pixels data into vector that will be passed to BitmapUploader. |
190 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a | 174 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a |
191 // SkBitmap. | 175 // SkBitmap. |
192 bitmap_.lockPixels(); | 176 data->bitmap.lockPixels(); |
193 const unsigned char* addr = | 177 const unsigned char* addr = |
194 static_cast<const unsigned char*>(bitmap_.getPixels()); | 178 static_cast<const unsigned char*>(data->bitmap.getPixels()); |
195 const int bytes = bounds.width() * bounds.height() * 4; | 179 const int bytes = bounds.width() * bounds.height() * 4; |
196 std::unique_ptr<std::vector<unsigned char>> data( | 180 std::unique_ptr<std::vector<unsigned char>> pixels( |
197 new std::vector<unsigned char>(addr, addr + bytes)); | 181 new std::vector<unsigned char>(addr, addr + bytes)); |
198 bitmap_.unlockPixels(); | 182 data->bitmap.unlockPixels(); |
199 | 183 |
200 #if defined(OS_ANDROID) | 184 #if defined(OS_ANDROID) |
201 // TODO(jcivelli): find a way to not have an ifdef here. | 185 // TODO(jcivelli): find a way to not have an ifdef here. |
202 BitmapUploader::Format bitmap_format = BitmapUploader::RGBA; | 186 BitmapUploader::Format bitmap_format = BitmapUploader::RGBA; |
203 #else | 187 #else |
204 BitmapUploader::Format bitmap_format = BitmapUploader::BGRA; | 188 BitmapUploader::Format bitmap_format = BitmapUploader::BGRA; |
205 #endif | 189 #endif |
206 | 190 |
207 // Send frame to MUS via BitmapUploader. | 191 // Send frame to MUS via BitmapUploader. |
208 uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), | 192 data->uploader->SetBitmap(bounds.width(), bounds.height(), std::move(pixels), |
209 bitmap_format); | 193 bitmap_format); |
210 } | 194 } |
211 | 195 |
212 } // namespace demo | 196 } // namespace demo |
213 } // namespace ui | 197 } // namespace ui |
OLD | NEW |