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/compositor/paint_recorder.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 aura { |
| 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. |
| 31 const int kSquareSize = 300; | 38 const int kSquareSize = 300; |
| 32 | 39 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 53 canvas->rotate(angle); | 60 canvas->rotate(angle); |
| 54 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), | 61 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
| 55 -SkFloatToScalar(canvas_size.height() * 0.5f)); | 62 -SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 56 } | 63 } |
| 57 | 64 |
| 58 canvas->drawRect(rect, paint); | 65 canvas->drawRect(rect, paint); |
| 59 } | 66 } |
| 60 | 67 |
| 61 } // namespace | 68 } // namespace |
| 62 | 69 |
| 70 // Layer delegate, which paints the provided image in the layer. | |
| 71 class ImageLayerDelegate : public ui::LayerDelegate { | |
| 72 public: | |
| 73 ImageLayerDelegate(ui::Layer* layer) : layer_(layer) {} | |
| 74 ~ImageLayerDelegate() override {} | |
| 75 | |
| 76 void SetImage(gfx::Image image) { image_ = image; } | |
|
sky
2016/12/05 16:06:41
const gfx::Image&
mfomitchev
2016/12/05 21:11:21
Done (removed).
| |
| 77 | |
| 78 private: | |
| 79 // ui::LayerDelegate: | |
| 80 void OnPaintLayer(const ui::PaintContext& context) override { | |
| 81 ui::PaintRecorder recorder(context, layer_->size()); | |
| 82 if (!image_.IsEmpty()) | |
| 83 recorder.canvas()->DrawImageInt(image_.AsImageSkia(), 0, 0); | |
| 84 } | |
| 85 | |
| 86 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} | |
| 87 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} | |
| 88 | |
| 89 // Not owned. | |
| 90 ui::Layer* layer_; | |
| 91 gfx::Image image_; | |
| 92 | |
| 93 DISALLOW_ASSIGN(ImageLayerDelegate); | |
| 94 }; | |
| 95 | |
| 63 MusDemo::MusDemo() {} | 96 MusDemo::MusDemo() {} |
| 64 | 97 |
| 65 MusDemo::~MusDemo() { | 98 MusDemo::~MusDemo() { |
| 66 display::Screen::SetScreenInstance(nullptr); | 99 display::Screen::SetScreenInstance(nullptr); |
| 67 } | 100 } |
| 68 | 101 |
| 69 void MusDemo::OnStart() { | 102 void MusDemo::OnStart() { |
| 70 screen_ = base::MakeUnique<display::ScreenBase>(); | 103 screen_ = base::MakeUnique<display::ScreenBase>(); |
| 71 display::Screen::SetScreenInstance(screen_.get()); | 104 display::Screen::SetScreenInstance(screen_.get()); |
| 72 gpu_service_ = GpuService::Create(context()->connector()); | 105 |
| 73 window_tree_client_ = base::MakeUnique<WindowTreeClient>(this, this); | 106 env_ = Env::CreateInstance(Env::Mode::MUS); |
| 74 window_tree_client_->ConnectAsWindowManager(context()->connector()); | 107 capture_client_ = base::MakeUnique<client::DefaultCaptureClient>(); |
| 108 property_converter_ = base::MakeUnique<PropertyConverter>(); | |
| 109 wm_state_ = base::MakeUnique<::wm::WMState>(); | |
| 110 | |
| 111 gpu_service_ = ui::GpuService::Create(context()->connector()); | |
| 112 context_factory_ = base::MakeUnique<MusContextFactory>(gpu_service_.get()); | |
| 113 env_->set_context_factory(context_factory_.get()); | |
| 114 | |
| 115 window_tree_client_ = | |
| 116 base::MakeUnique<WindowTreeClient>(context()->connector(), this, this); | |
| 117 window_tree_client_->ConnectAsWindowManager(); | |
| 118 | |
| 119 env_->SetWindowTreeClient(window_tree_client_.get()); | |
| 75 } | 120 } |
| 76 | 121 |
| 77 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, | 122 bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, |
| 78 service_manager::InterfaceRegistry* registry) { | 123 service_manager::InterfaceRegistry* registry) { |
| 79 return true; | 124 return true; |
| 80 } | 125 } |
| 81 | 126 |
| 82 void MusDemo::OnEmbed(Window* window) { | 127 void MusDemo::OnEmbed(std::unique_ptr<WindowTreeHostMus> window_tree_host) { |
| 83 // Not called for the WindowManager. | 128 // Not called for the WindowManager. |
| 84 NOTREACHED(); | 129 NOTREACHED(); |
| 85 } | 130 } |
| 86 | 131 |
| 132 void MusDemo::OnUnembed(Window* root) { | |
| 133 NOTREACHED(); | |
| 134 } | |
| 135 | |
| 87 void MusDemo::OnEmbedRootDestroyed(Window* root) { | 136 void MusDemo::OnEmbedRootDestroyed(Window* root) { |
| 88 // Not called for the WindowManager. | 137 // Not called for the WindowManager. |
| 89 NOTREACHED(); | 138 NOTREACHED(); |
| 90 } | 139 } |
| 91 | 140 |
| 92 void MusDemo::OnLostConnection(WindowTreeClient* client) { | 141 void MusDemo::OnLostConnection(WindowTreeClient* client) { |
| 93 window_ = nullptr; | 142 root_window_ = nullptr; |
| 94 window_tree_client_.reset(); | 143 window_tree_client_.reset(); |
| 95 timer_.Stop(); | 144 timer_.Stop(); |
| 96 } | 145 } |
| 97 | 146 |
| 98 void MusDemo::OnPointerEventObserved(const PointerEvent& event, | 147 void MusDemo::OnPointerEventObserved(const ui::PointerEvent& event, |
| 99 Window* target) {} | 148 Window* target) {} |
| 100 | 149 |
| 150 client::CaptureClient* MusDemo::GetCaptureClient() { | |
| 151 return capture_client_.get(); | |
| 152 } | |
| 153 | |
| 154 PropertyConverter* MusDemo::GetPropertyConverter() { | |
| 155 return property_converter_.get(); | |
| 156 } | |
| 157 | |
| 101 void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} | 158 void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} |
| 102 | 159 |
| 103 bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { | 160 bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { |
| 104 return true; | 161 return true; |
| 105 } | 162 } |
| 106 | 163 |
| 107 bool MusDemo::OnWmSetProperty(Window* window, | 164 bool MusDemo::OnWmSetProperty(Window* window, |
| 108 const std::string& name, | 165 const std::string& name, |
| 109 std::unique_ptr<std::vector<uint8_t>>* new_data) { | 166 std::unique_ptr<std::vector<uint8_t>>* new_data) { |
| 110 return true; | 167 return true; |
| 111 } | 168 } |
| 112 | 169 |
| 113 Window* MusDemo::OnWmCreateTopLevelWindow( | 170 Window* MusDemo::OnWmCreateTopLevelWindow( |
| 171 ui::mojom::WindowType window_type, | |
| 114 std::map<std::string, std::vector<uint8_t>>* properties) { | 172 std::map<std::string, std::vector<uint8_t>>* properties) { |
| 173 NOTREACHED(); | |
| 115 return nullptr; | 174 return nullptr; |
| 116 } | 175 } |
| 117 | 176 |
| 118 void MusDemo::OnWmClientJankinessChanged( | 177 void MusDemo::OnWmClientJankinessChanged( |
| 119 const std::set<Window*>& client_windows, | 178 const std::set<Window*>& client_windows, |
| 120 bool janky) { | 179 bool janky) { |
| 121 // Don't care | 180 // Don't care |
| 122 } | 181 } |
| 123 | 182 |
| 124 void MusDemo::OnWmNewDisplay(Window* window, const display::Display& display) { | 183 void MusDemo::OnWmWillCreateDisplay(const display::Display& display) { |
| 125 DCHECK(!window_); // Only support one display. | 184 screen_->display_list().AddDisplay(display, |
| 126 window_ = window; | 185 display::DisplayList::Type::PRIMARY); |
| 186 } | |
| 127 | 187 |
| 128 // Initialize bitmap uploader for sending frames to MUS. | 188 void MusDemo::OnWmNewDisplay( |
| 129 uploader_.reset(new BitmapUploader(window_)); | 189 std::unique_ptr<WindowTreeHostMus> window_tree_host, |
| 130 uploader_->Init(gpu_service_.get()); | 190 const display::Display& display) { |
| 191 DCHECK(!root_window_); // Only support one display. | |
| 192 | |
| 193 window_tree_host->InitHost(); | |
| 194 window_tree_host->Show(); | |
| 195 root_window_ = window_tree_host->window(); | |
| 196 // Take ownership of the WTH. | |
| 197 window_tree_host_ = std::move(window_tree_host); | |
| 198 | |
| 199 // Initialize the layer for the bitmap. | |
| 200 bitmap_layer_ = base::MakeUnique<ui::Layer>(ui::LAYER_TEXTURED); | |
| 201 bitmap_layer_->SetVisible(true); | |
| 202 bitmap_layer_->SetBounds(root_window_->bounds()); | |
| 203 bitmap_layer_->set_name("Bitmap"); | |
| 204 bitmap_layer_->SetFillsBoundsOpaquely(true); | |
| 205 | |
| 206 layer_delegate_ = base::MakeUnique<ImageLayerDelegate>(bitmap_layer_.get()); | |
| 207 bitmap_layer_->set_delegate(layer_delegate_.get()); | |
| 208 | |
| 209 root_window_->layer()->Add(bitmap_layer_.get()); | |
| 131 | 210 |
| 132 // Draw initial frame and start the timer to regularly draw frames. | 211 // Draw initial frame and start the timer to regularly draw frames. |
| 133 DrawFrame(); | 212 DrawFrame(); |
| 134 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), | 213 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| 135 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); | 214 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); |
| 136 } | 215 } |
| 137 | 216 |
| 138 void MusDemo::OnWmDisplayRemoved(ui::Window* window) { | 217 void MusDemo::OnWmDisplayRemoved(WindowTreeHostMus* window_tree_host) { |
| 139 window->Destroy(); | 218 timer_.Stop(); |
| 219 root_window_->layer()->Remove(bitmap_layer_.get()); | |
| 220 bitmap_layer_.reset(); | |
| 221 layer_delegate_.reset(); | |
| 140 } | 222 } |
| 141 | 223 |
| 142 void MusDemo::OnWmDisplayModified(const display::Display& display) {} | 224 void MusDemo::OnWmDisplayModified(const display::Display& display) {} |
| 143 | 225 |
| 226 ui::mojom::EventResult MusDemo::OnAccelerator(uint32_t id, | |
| 227 const ui::Event& event) { | |
| 228 return ui::mojom::EventResult::UNHANDLED; | |
| 229 } | |
| 230 | |
| 144 void MusDemo::OnWmPerformMoveLoop(Window* window, | 231 void MusDemo::OnWmPerformMoveLoop(Window* window, |
| 145 mojom::MoveLoopSource source, | 232 ui::mojom::MoveLoopSource source, |
| 146 const gfx::Point& cursor_location, | 233 const gfx::Point& cursor_location, |
| 147 const base::Callback<void(bool)>& on_done) { | 234 const base::Callback<void(bool)>& on_done) { |
| 148 // Don't care | 235 // Don't care |
| 149 } | 236 } |
| 150 | 237 |
| 151 void MusDemo::OnWmCancelMoveLoop(Window* window) {} | 238 void MusDemo::OnWmCancelMoveLoop(Window* window) {} |
| 152 | 239 |
| 153 void MusDemo::AllocBitmap() { | 240 void MusDemo::OnWmSetClientArea( |
| 154 const gfx::Rect bounds = window_->GetBoundsInRoot(); | 241 Window* window, |
| 155 | 242 const gfx::Insets& insets, |
| 156 // Allocate bitmap the same size as the window for drawing. | 243 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 | 244 |
| 163 void MusDemo::DrawFrame() { | 245 void MusDemo::DrawFrame() { |
| 164 base::TimeTicks now = base::TimeTicks::Now(); | 246 base::TimeTicks now = base::TimeTicks::Now(); |
| 165 | 247 |
| 166 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() | 248 VLOG(1) << (now - last_draw_frame_time_).InMilliseconds() |
| 167 << "ms since the last frame was drawn."; | 249 << "ms since the last frame was drawn."; |
| 168 last_draw_frame_time_ = now; | 250 last_draw_frame_time_ = now; |
| 169 | 251 |
| 170 angle_ += 2.0; | 252 angle_ += 2.0; |
| 171 if (angle_ >= 360.0) | 253 if (angle_ >= 360.0) |
| 172 angle_ = 0.0; | 254 angle_ = 0.0; |
| 173 | 255 |
| 174 const gfx::Rect bounds = window_->GetBoundsInRoot(); | 256 // Re-initialize the bitmap |
| 175 | 257 bitmap_.reset(); |
| 176 // Check that bitmap and window sizes match, otherwise reallocate bitmap. | 258 const gfx::Rect bounds = bitmap_layer_->bounds(); |
| 177 const SkImageInfo info = bitmap_.info(); | 259 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 178 if (info.width() != bounds.width() || info.height() != bounds.height()) { | 260 kPremul_SkAlphaType); |
| 179 AllocBitmap(); | 261 bitmap_.allocPixels(image_info); |
| 180 } | |
| 181 | 262 |
| 182 // Draw the rotated square on background in bitmap. | 263 // Draw the rotated square on background in bitmap. |
| 183 SkCanvas canvas(bitmap_); | 264 SkCanvas canvas(bitmap_); |
| 184 canvas.clear(kBgColor); | 265 canvas.clear(kBgColor); |
| 185 // TODO(kylechar): Add GL drawing instead of software rasterization in future. | 266 // TODO(kylechar): Add GL drawing instead of software rasterization in future. |
| 186 DrawSquare(bounds, angle_, &canvas); | 267 DrawSquare(bounds, angle_, &canvas); |
| 187 canvas.flush(); | 268 canvas.flush(); |
| 188 | 269 |
| 189 // Copy pixels data into vector that will be passed to BitmapUploader. | 270 gfx::ImageSkiaRep image_skia_rep(bitmap_, 1); |
| 190 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a | 271 gfx::ImageSkia image_skia(image_skia_rep); |
| 191 // SkBitmap. | 272 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 | 273 |
| 200 #if defined(OS_ANDROID) | 274 layer_delegate_->SetImage(image); |
| 201 // TODO(jcivelli): find a way to not have an ifdef here. | 275 bitmap_layer_->SchedulePaint(bitmap_layer_->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 } | 276 } |
| 211 | 277 |
| 212 } // namespace demo | 278 } // namespace demo |
| 213 } // namespace ui | 279 } // namespace aura |
| OLD | NEW |