Chromium Code Reviews| 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" | |
| 12 #include "services/ui/public/cpp/gpu/gpu_service.h" | 11 #include "services/ui/public/cpp/gpu/gpu_service.h" |
| 13 #include "services/ui/public/cpp/window.h" | |
| 14 #include "services/ui/public/cpp/window_tree_client.h" | |
| 15 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
| 17 #include "third_party/skia/include/core/SkImageInfo.h" | 14 #include "third_party/skia/include/core/SkImageInfo.h" |
| 18 #include "third_party/skia/include/core/SkPaint.h" | 15 #include "third_party/skia/include/core/SkPaint.h" |
| 19 #include "third_party/skia/include/core/SkRect.h" | 16 #include "third_party/skia/include/core/SkRect.h" |
| 17 #include "ui/aura/client/default_capture_client.h" | |
| 18 #include "ui/aura/env.h" | |
| 19 #include "ui/aura/mus/mus_context_factory.h" | |
| 20 #include "ui/aura/mus/property_converter.h" | |
| 21 #include "ui/aura/mus/window_tree_client.h" | |
| 22 #include "ui/aura/mus/window_tree_host_mus.h" | |
| 23 #include "ui/aura/window.h" | |
| 24 #include "ui/aura_extra/image_window_delegate.h" | |
| 20 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
| 26 #include "ui/gfx/image/image.h" | |
| 27 #include "ui/wm/core/wm_state.h" | |
| 21 | 28 |
| 22 namespace ui { | 29 namespace ui { |
| 23 namespace demo { | 30 namespace demo { |
| 24 | 31 |
| 25 namespace { | 32 namespace { |
| 26 | 33 |
| 27 // Milliseconds between frames. | 34 // Milliseconds between frames. |
| 28 const int64_t kFrameDelay = 33; | 35 const int64_t kFrameDelay = 33; |
| 29 | 36 |
| 30 // Size of square in pixels to draw. | 37 // Size of square in pixels to draw. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 | 69 |
| 63 MusDemo::MusDemo() {} | 70 MusDemo::MusDemo() {} |
| 64 | 71 |
| 65 MusDemo::~MusDemo() { | 72 MusDemo::~MusDemo() { |
| 66 display::Screen::SetScreenInstance(nullptr); | 73 display::Screen::SetScreenInstance(nullptr); |
| 67 } | 74 } |
| 68 | 75 |
| 69 void MusDemo::OnStart() { | 76 void MusDemo::OnStart() { |
| 70 screen_ = base::MakeUnique<display::ScreenBase>(); | 77 screen_ = base::MakeUnique<display::ScreenBase>(); |
| 71 display::Screen::SetScreenInstance(screen_.get()); | 78 display::Screen::SetScreenInstance(screen_.get()); |
| 79 | |
| 80 env_ = aura::Env::CreateInstance(aura::Env::Mode::MUS); | |
| 81 capture_client_ = base::MakeUnique<aura::client::DefaultCaptureClient>(); | |
| 82 property_converter_ = base::MakeUnique<aura::PropertyConverter>(); | |
| 83 wm_state_ = base::MakeUnique<::wm::WMState>(); | |
| 84 | |
| 72 gpu_service_ = GpuService::Create(context()->connector()); | 85 gpu_service_ = GpuService::Create(context()->connector()); |
| 73 window_tree_client_ = base::MakeUnique<WindowTreeClient>(this, this); | 86 context_factory_ = |
| 74 window_tree_client_->ConnectAsWindowManager(context()->connector()); | 87 base::MakeUnique<aura::MusContextFactory>(gpu_service_.get()); |
| 88 env_->set_context_factory(context_factory_.get()); | |
| 89 | |
| 90 window_tree_client_ = base::MakeUnique<aura::WindowTreeClient>( | |
| 91 context()->connector(), this, this); | |
| 92 window_tree_client_->ConnectAsWindowManager(); | |
| 93 | |
| 94 env_->SetWindowTreeClient(window_tree_client_.get()); | |
| 75 } | 95 } |
| 76 | 96 |
| 77 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, | 97 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, |
| 78 service_manager::InterfaceRegistry* registry) { | 98 service_manager::InterfaceRegistry* registry) { |
| 79 return true; | 99 return true; |
| 80 } | 100 } |
| 81 | 101 |
| 82 void MusDemo::OnEmbed(Window* window) { | 102 void MusDemo::OnEmbed( |
| 103 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { | |
| 83 // Not called for the WindowManager. | 104 // Not called for the WindowManager. |
| 84 NOTREACHED(); | 105 NOTREACHED(); |
| 85 } | 106 } |
| 86 | 107 |
| 87 void MusDemo::OnEmbedRootDestroyed(Window* root) { | 108 void MusDemo::OnUnembed(aura::Window* root) { |
| 109 NOTREACHED(); | |
| 110 } | |
| 111 | |
| 112 void MusDemo::OnEmbedRootDestroyed(aura::Window* root) { | |
| 88 // Not called for the WindowManager. | 113 // Not called for the WindowManager. |
| 89 NOTREACHED(); | 114 NOTREACHED(); |
| 90 } | 115 } |
| 91 | 116 |
| 92 void MusDemo::OnLostConnection(WindowTreeClient* client) { | 117 void MusDemo::OnLostConnection(aura::WindowTreeClient* client) { |
| 93 window_ = nullptr; | 118 root_window_ = nullptr; |
| 94 window_tree_client_.reset(); | 119 window_tree_client_.reset(); |
| 95 timer_.Stop(); | 120 timer_.Stop(); |
| 96 } | 121 } |
| 97 | 122 |
| 98 void MusDemo::OnPointerEventObserved(const PointerEvent& event, | 123 void MusDemo::OnPointerEventObserved(const PointerEvent& event, |
| 99 Window* target) {} | 124 aura::Window* target) {} |
| 100 | 125 |
| 101 void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} | 126 aura::client::CaptureClient* MusDemo::GetCaptureClient() { |
| 127 return capture_client_.get(); | |
| 128 } | |
| 102 | 129 |
| 103 bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { | 130 aura::PropertyConverter* MusDemo::GetPropertyConverter() { |
| 131 return property_converter_.get(); | |
| 132 } | |
| 133 | |
| 134 void MusDemo::SetWindowManagerClient(aura::WindowManagerClient* client) {} | |
| 135 | |
| 136 bool MusDemo::OnWmSetBounds(aura::Window* window, gfx::Rect* bounds) { | |
| 104 return true; | 137 return true; |
| 105 } | 138 } |
| 106 | 139 |
| 107 bool MusDemo::OnWmSetProperty(Window* window, | 140 bool MusDemo::OnWmSetProperty(aura::Window* window, |
| 108 const std::string& name, | 141 const std::string& name, |
| 109 std::unique_ptr<std::vector<uint8_t>>* new_data) { | 142 std::unique_ptr<std::vector<uint8_t>>* new_data) { |
| 110 return true; | 143 return true; |
| 111 } | 144 } |
| 112 | 145 |
| 113 Window* MusDemo::OnWmCreateTopLevelWindow( | 146 aura::Window* MusDemo::OnWmCreateTopLevelWindow( |
| 147 mojom::WindowType window_type, | |
| 114 std::map<std::string, std::vector<uint8_t>>* properties) { | 148 std::map<std::string, std::vector<uint8_t>>* properties) { |
| 149 NOTREACHED(); | |
| 115 return nullptr; | 150 return nullptr; |
| 116 } | 151 } |
| 117 | 152 |
| 118 void MusDemo::OnWmClientJankinessChanged( | 153 void MusDemo::OnWmClientJankinessChanged( |
| 119 const std::set<Window*>& client_windows, | 154 const std::set<aura::Window*>& client_windows, |
| 120 bool janky) { | 155 bool janky) { |
| 121 // Don't care | 156 // Don't care |
| 122 } | 157 } |
| 123 | 158 |
| 124 void MusDemo::OnWmNewDisplay(Window* window, const display::Display& display) { | 159 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) { |
| 125 DCHECK(!window_); // Only support one display. | 160 screen_->display_list().AddDisplay(display, |
| 126 window_ = window; | 161 display::DisplayList::Type::PRIMARY); |
| 162 } | |
| 127 | 163 |
| 128 // Initialize bitmap uploader for sending frames to MUS. | 164 void MusDemo::OnWmNewDisplay( |
| 129 uploader_.reset(new BitmapUploader(window_)); | 165 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host, |
| 130 uploader_->Init(gpu_service_.get()); | 166 const display::Display& display) { |
| 167 DCHECK(!root_window_); // Only support one display. | |
| 168 | |
| 169 window_tree_host->InitHost(); | |
| 170 window_tree_host->Show(); | |
| 171 root_window_ = window_tree_host->window(); | |
| 172 // Take ownership of the WTH. | |
| 173 window_tree_host_ = std::move(window_tree_host); | |
| 174 | |
| 175 // Initialize the window for the bitmap. | |
| 176 window_delegate_ = new aura_extra::ImageWindowDelegate(); | |
| 177 bitmap_window_ = base::MakeUnique<aura::Window>(window_delegate_); | |
| 178 bitmap_window_->Init(LAYER_TEXTURED); | |
| 179 bitmap_window_->SetBounds(root_window_->bounds()); | |
| 180 bitmap_window_->Show(); | |
| 181 bitmap_window_->SetName("Bitmap"); | |
| 182 | |
| 183 root_window_->AddChild(bitmap_window_.get()); | |
| 131 | 184 |
| 132 // Draw initial frame and start the timer to regularly draw frames. | 185 // Draw initial frame and start the timer to regularly draw frames. |
| 133 DrawFrame(); | 186 DrawFrame(); |
| 134 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | 187 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| 135 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); | 188 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); |
| 136 } | 189 } |
| 137 | 190 |
| 138 void MusDemo::OnWmDisplayRemoved(ui::Window* window) { | 191 void MusDemo::OnWmDisplayRemoved(aura::WindowTreeHostMus* window_tree_host) { |
| 139 window->Destroy(); | 192 timer_.Stop(); |
| 193 root_window_->RemoveChild(bitmap_window_.get()); | |
| 194 bitmap_window_.reset(); | |
| 140 } | 195 } |
| 141 | 196 |
| 142 void MusDemo::OnWmDisplayModified(const display::Display& display) {} | 197 void MusDemo::OnWmDisplayModified(const display::Display& display) {} |
| 143 | 198 |
| 144 void MusDemo::OnWmPerformMoveLoop(Window* window, | 199 mojom::EventResult MusDemo::OnAccelerator(uint32_t id, const Event& event) { |
| 200 return mojom::EventResult::UNHANDLED; | |
| 201 } | |
| 202 | |
| 203 void MusDemo::OnWmPerformMoveLoop(aura::Window* window, | |
| 145 mojom::MoveLoopSource source, | 204 mojom::MoveLoopSource source, |
| 146 const gfx::Point& cursor_location, | 205 const gfx::Point& cursor_location, |
| 147 const base::Callback<void(bool)>& on_done) { | 206 const base::Callback<void(bool)>& on_done) { |
| 148 // Don't care | 207 // Don't care |
| 149 } | 208 } |
| 150 | 209 |
| 151 void MusDemo::OnWmCancelMoveLoop(Window* window) {} | 210 void MusDemo::OnWmCancelMoveLoop(aura::Window* window) {} |
| 152 | 211 |
| 153 void MusDemo::AllocBitmap() { | 212 void MusDemo::OnWmSetClientArea( |
| 154 const gfx::Rect bounds = window_->GetBoundsInRoot(); | 213 aura::Window* window, |
| 155 | 214 const gfx::Insets& insets, |
| 156 // Allocate bitmap the same size as the window for drawing. | 215 const std::vector<gfx::Rect>& additional_client_areas) {} |
| 157 bitmap_.reset(); | |
| 158 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), | |
| 159 kPremul_SkAlphaType); | |
| 160 bitmap_.allocPixels(image_info); | |
| 161 } | |
| 162 | 216 |
| 163 void MusDemo::DrawFrame() { | 217 void MusDemo::DrawFrame() { |
| 164 base::TimeTicks now = base::TimeTicks::Now(); | 218 base::TimeTicks now = base::TimeTicks::Now(); |
| 165 | 219 |
| 166 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() | 220 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() |
| 167 << "ms since the last frame was drawn."; | 221 << "ms since the last frame was drawn."; |
| 168 last_draw_frame_time_ = now; | 222 last_draw_frame_time_ = now; |
| 169 | 223 |
| 170 angle_ += 2.0; | 224 angle_ += 2.0; |
| 171 if (angle_ >= 360.0) | 225 if (angle_ >= 360.0) |
| 172 angle_ = 0.0; | 226 angle_ = 0.0; |
| 173 | 227 |
| 174 const gfx::Rect bounds = window_->GetBoundsInRoot(); | 228 // Re-initialize the bitmap |
|
kylechar
2016/12/05 22:02:30
Does this need to realloc the bitmap memory every
mfomitchev
2016/12/05 23:07:14
There's probably a better way to do this, but when
kylechar
2016/12/16 14:28:54
Just had a chance to look at this in a bit of deta
| |
| 175 | 229 bitmap_.reset(); |
| 176 // Check that bitmap and window sizes match, otherwise reallocate bitmap. | 230 const gfx::Rect bounds = bitmap_window_->bounds(); |
| 177 const SkImageInfo info = bitmap_.info(); | 231 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 178 if (info.width() != bounds.width() || info.height() != bounds.height()) { | 232 kPremul_SkAlphaType); |
| 179 AllocBitmap(); | 233 bitmap_.allocPixels(image_info); |
| 180 } | |
| 181 | 234 |
| 182 // Draw the rotated square on background in bitmap. | 235 // Draw the rotated square on background in bitmap. |
| 183 SkCanvas canvas(bitmap_); | 236 SkCanvas canvas(bitmap_); |
| 184 canvas.clear(kBgColor); | 237 canvas.clear(kBgColor); |
| 185 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | 238 // TODO(kylechar): Add GL drawing instead of software rasterization in future. |
| 186 DrawSquare(bounds, angle_, &canvas); | 239 DrawSquare(bounds, angle_, &canvas); |
| 187 canvas.flush(); | 240 canvas.flush(); |
| 188 | 241 |
| 189 // Copy pixels data into vector that will be passed to BitmapUploader. | 242 gfx::ImageSkiaRep image_skia_rep(bitmap_, 1); |
| 190 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a | 243 gfx::ImageSkia image_skia(image_skia_rep); |
| 191 // SkBitmap. | 244 gfx::Image image(image_skia); |
| 192 bitmap_.lockPixels(); | |
| 193 const unsigned char* addr = | |
| 194 static_cast<const unsigned char*>(bitmap_.getPixels()); | |
| 195 const int bytes = bounds.width() * bounds.height() * 4; | |
| 196 std::unique_ptr<std::vector<unsigned char>> data( | |
| 197 new std::vector<unsigned char>(addr, addr + bytes)); | |
| 198 bitmap_.unlockPixels(); | |
| 199 | 245 |
| 200 #if defined(OS_ANDROID) | 246 window_delegate_->SetImage(image); |
| 201 // TODO(jcivelli): find a way to not have an ifdef here. | 247 bitmap_window_->SchedulePaintInRect(bitmap_window_->bounds()); |
| 202 BitmapUploader::Format bitmap_format = BitmapUploader::RGBA; | |
| 203 #else | |
| 204 BitmapUploader::Format bitmap_format = BitmapUploader::BGRA; | |
| 205 #endif | |
| 206 | |
| 207 // Send frame to MUS via BitmapUploader. | |
| 208 uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), | |
| 209 bitmap_format); | |
| 210 } | 248 } |
| 211 | 249 |
| 212 } // namespace demo | 250 } // namespace demo |
| 213 } // namespace ui | 251 } // namespace aura |
| OLD | NEW |