Chromium Code Reviews| Index: services/ui/demo/mus_demo.cc |
| diff --git a/services/ui/demo/mus_demo.cc b/services/ui/demo/mus_demo.cc |
| index d0a90bb18c92594888f24daa5ac617983b20fe12..437521cdaba76cc828fb22866a95fda1808c6c61 100644 |
| --- a/services/ui/demo/mus_demo.cc |
| +++ b/services/ui/demo/mus_demo.cc |
| @@ -8,18 +8,25 @@ |
| #include "base/time/time.h" |
| #include "services/service_manager/public/cpp/connector.h" |
| #include "services/service_manager/public/cpp/service_context.h" |
| -#include "services/ui/demo/bitmap_uploader.h" |
| #include "services/ui/public/cpp/gpu/gpu_service.h" |
| -#include "services/ui/public/cpp/window.h" |
| -#include "services/ui/public/cpp/window_tree_client.h" |
| #include "third_party/skia/include/core/SkCanvas.h" |
| #include "third_party/skia/include/core/SkColor.h" |
| #include "third_party/skia/include/core/SkImageInfo.h" |
| #include "third_party/skia/include/core/SkPaint.h" |
| #include "third_party/skia/include/core/SkRect.h" |
| +#include "ui/aura/client/default_capture_client.h" |
| +#include "ui/aura/env.h" |
| +#include "ui/aura/mus/mus_context_factory.h" |
| +#include "ui/aura/mus/property_converter.h" |
| +#include "ui/aura/mus/window_tree_client.h" |
| +#include "ui/aura/mus/window_tree_host_mus.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/compositor/paint_recorder.h" |
| #include "ui/gfx/geometry/rect.h" |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/wm/core/wm_state.h" |
| -namespace ui { |
| +namespace aura { |
| namespace demo { |
| namespace { |
| @@ -60,6 +67,32 @@ void DrawSquare(const gfx::Rect& bounds, double angle, SkCanvas* canvas) { |
| } // namespace |
| +// Layer delegate, which paints the provided image in the layer. |
| +class ImageLayerDelegate : public ui::LayerDelegate { |
| + public: |
| + ImageLayerDelegate(ui::Layer* layer) : layer_(layer) {} |
| + ~ImageLayerDelegate() override {} |
| + |
| + 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).
|
| + |
| + private: |
| + // ui::LayerDelegate: |
| + void OnPaintLayer(const ui::PaintContext& context) override { |
| + ui::PaintRecorder recorder(context, layer_->size()); |
| + if (!image_.IsEmpty()) |
| + recorder.canvas()->DrawImageInt(image_.AsImageSkia(), 0, 0); |
| + } |
| + |
| + void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| + void OnDeviceScaleFactorChanged(float device_scale_factor) override {} |
| + |
| + // Not owned. |
| + ui::Layer* layer_; |
| + gfx::Image image_; |
| + |
| + DISALLOW_ASSIGN(ImageLayerDelegate); |
| +}; |
| + |
| MusDemo::MusDemo() {} |
| MusDemo::~MusDemo() { |
| @@ -69,9 +102,21 @@ MusDemo::~MusDemo() { |
| void MusDemo::OnStart() { |
| screen_ = base::MakeUnique<display::ScreenBase>(); |
| display::Screen::SetScreenInstance(screen_.get()); |
| - gpu_service_ = GpuService::Create(context()->connector()); |
| - window_tree_client_ = base::MakeUnique<WindowTreeClient>(this, this); |
| - window_tree_client_->ConnectAsWindowManager(context()->connector()); |
| + |
| + env_ = Env::CreateInstance(Env::Mode::MUS); |
| + capture_client_ = base::MakeUnique<client::DefaultCaptureClient>(); |
| + property_converter_ = base::MakeUnique<PropertyConverter>(); |
| + wm_state_ = base::MakeUnique<::wm::WMState>(); |
| + |
| + gpu_service_ = ui::GpuService::Create(context()->connector()); |
| + context_factory_ = base::MakeUnique<MusContextFactory>(gpu_service_.get()); |
| + env_->set_context_factory(context_factory_.get()); |
| + |
| + window_tree_client_ = |
| + base::MakeUnique<WindowTreeClient>(context()->connector(), this, this); |
| + window_tree_client_->ConnectAsWindowManager(); |
| + |
| + env_->SetWindowTreeClient(window_tree_client_.get()); |
| } |
| bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, |
| @@ -79,25 +124,37 @@ bool MusDemo::OnConnect(const service_manager::ServiceInfo& remote_info, |
| return true; |
| } |
| -void MusDemo::OnEmbed(Window* window) { |
| +void MusDemo::OnEmbed(std::unique_ptr<WindowTreeHostMus> window_tree_host) { |
| // Not called for the WindowManager. |
| NOTREACHED(); |
| } |
| +void MusDemo::OnUnembed(Window* root) { |
| + NOTREACHED(); |
| +} |
| + |
| void MusDemo::OnEmbedRootDestroyed(Window* root) { |
| // Not called for the WindowManager. |
| NOTREACHED(); |
| } |
| void MusDemo::OnLostConnection(WindowTreeClient* client) { |
| - window_ = nullptr; |
| + root_window_ = nullptr; |
| window_tree_client_.reset(); |
| timer_.Stop(); |
| } |
| -void MusDemo::OnPointerEventObserved(const PointerEvent& event, |
| +void MusDemo::OnPointerEventObserved(const ui::PointerEvent& event, |
| Window* target) {} |
| +client::CaptureClient* MusDemo::GetCaptureClient() { |
| + return capture_client_.get(); |
| +} |
| + |
| +PropertyConverter* MusDemo::GetPropertyConverter() { |
| + return property_converter_.get(); |
| +} |
| + |
| void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} |
| bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { |
| @@ -111,7 +168,9 @@ bool MusDemo::OnWmSetProperty(Window* window, |
| } |
| Window* MusDemo::OnWmCreateTopLevelWindow( |
| + ui::mojom::WindowType window_type, |
| std::map<std::string, std::vector<uint8_t>>* properties) { |
| + NOTREACHED(); |
| return nullptr; |
| } |
| @@ -121,13 +180,33 @@ void MusDemo::OnWmClientJankinessChanged( |
| // Don't care |
| } |
| -void MusDemo::OnWmNewDisplay(Window* window, const display::Display& display) { |
| - DCHECK(!window_); // Only support one display. |
| - window_ = window; |
| +void MusDemo::OnWmWillCreateDisplay(const display::Display& display) { |
| + screen_->display_list().AddDisplay(display, |
| + display::DisplayList::Type::PRIMARY); |
| +} |
| + |
| +void MusDemo::OnWmNewDisplay( |
| + std::unique_ptr<WindowTreeHostMus> window_tree_host, |
| + const display::Display& display) { |
| + DCHECK(!root_window_); // Only support one display. |
| + |
| + window_tree_host->InitHost(); |
| + window_tree_host->Show(); |
| + root_window_ = window_tree_host->window(); |
| + // Take ownership of the WTH. |
| + window_tree_host_ = std::move(window_tree_host); |
| + |
| + // Initialize the layer for the bitmap. |
| + bitmap_layer_ = base::MakeUnique<ui::Layer>(ui::LAYER_TEXTURED); |
| + bitmap_layer_->SetVisible(true); |
| + bitmap_layer_->SetBounds(root_window_->bounds()); |
| + bitmap_layer_->set_name("Bitmap"); |
| + bitmap_layer_->SetFillsBoundsOpaquely(true); |
| - // Initialize bitmap uploader for sending frames to MUS. |
| - uploader_.reset(new BitmapUploader(window_)); |
| - uploader_->Init(gpu_service_.get()); |
| + layer_delegate_ = base::MakeUnique<ImageLayerDelegate>(bitmap_layer_.get()); |
| + bitmap_layer_->set_delegate(layer_delegate_.get()); |
| + |
| + root_window_->layer()->Add(bitmap_layer_.get()); |
| // Draw initial frame and start the timer to regularly draw frames. |
| DrawFrame(); |
| @@ -135,14 +214,22 @@ void MusDemo::OnWmNewDisplay(Window* window, const display::Display& display) { |
| base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); |
| } |
| -void MusDemo::OnWmDisplayRemoved(ui::Window* window) { |
| - window->Destroy(); |
| +void MusDemo::OnWmDisplayRemoved(WindowTreeHostMus* window_tree_host) { |
| + timer_.Stop(); |
| + root_window_->layer()->Remove(bitmap_layer_.get()); |
| + bitmap_layer_.reset(); |
| + layer_delegate_.reset(); |
| } |
| void MusDemo::OnWmDisplayModified(const display::Display& display) {} |
| +ui::mojom::EventResult MusDemo::OnAccelerator(uint32_t id, |
| + const ui::Event& event) { |
| + return ui::mojom::EventResult::UNHANDLED; |
| +} |
| + |
| void MusDemo::OnWmPerformMoveLoop(Window* window, |
| - mojom::MoveLoopSource source, |
| + ui::mojom::MoveLoopSource source, |
| const gfx::Point& cursor_location, |
| const base::Callback<void(bool)>& on_done) { |
| // Don't care |
| @@ -150,15 +237,10 @@ void MusDemo::OnWmPerformMoveLoop(Window* window, |
| void MusDemo::OnWmCancelMoveLoop(Window* window) {} |
| -void MusDemo::AllocBitmap() { |
| - const gfx::Rect bounds = window_->GetBoundsInRoot(); |
| - |
| - // Allocate bitmap the same size as the window for drawing. |
| - bitmap_.reset(); |
| - SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| - kPremul_SkAlphaType); |
| - bitmap_.allocPixels(image_info); |
| -} |
| +void MusDemo::OnWmSetClientArea( |
| + Window* window, |
| + const gfx::Insets& insets, |
| + const std::vector<gfx::Rect>& additional_client_areas) {} |
| void MusDemo::DrawFrame() { |
| base::TimeTicks now = base::TimeTicks::Now(); |
| @@ -171,13 +253,12 @@ void MusDemo::DrawFrame() { |
| if (angle_ >= 360.0) |
| angle_ = 0.0; |
| - const gfx::Rect bounds = window_->GetBoundsInRoot(); |
| - |
| - // Check that bitmap and window sizes match, otherwise reallocate bitmap. |
| - const SkImageInfo info = bitmap_.info(); |
| - if (info.width() != bounds.width() || info.height() != bounds.height()) { |
| - AllocBitmap(); |
| - } |
| + // Re-initialize the bitmap |
| + bitmap_.reset(); |
| + const gfx::Rect bounds = bitmap_layer_->bounds(); |
| + SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| + kPremul_SkAlphaType); |
| + bitmap_.allocPixels(image_info); |
| // Draw the rotated square on background in bitmap. |
| SkCanvas canvas(bitmap_); |
| @@ -186,28 +267,13 @@ void MusDemo::DrawFrame() { |
| DrawSquare(bounds, angle_, &canvas); |
| canvas.flush(); |
| - // Copy pixels data into vector that will be passed to BitmapUploader. |
| - // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a |
| - // SkBitmap. |
| - bitmap_.lockPixels(); |
| - const unsigned char* addr = |
| - static_cast<const unsigned char*>(bitmap_.getPixels()); |
| - const int bytes = bounds.width() * bounds.height() * 4; |
| - std::unique_ptr<std::vector<unsigned char>> data( |
| - new std::vector<unsigned char>(addr, addr + bytes)); |
| - bitmap_.unlockPixels(); |
| - |
| -#if defined(OS_ANDROID) |
| - // TODO(jcivelli): find a way to not have an ifdef here. |
| - BitmapUploader::Format bitmap_format = BitmapUploader::RGBA; |
| -#else |
| - BitmapUploader::Format bitmap_format = BitmapUploader::BGRA; |
| -#endif |
| - |
| - // Send frame to MUS via BitmapUploader. |
| - uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), |
| - bitmap_format); |
| + gfx::ImageSkiaRep image_skia_rep(bitmap_, 1); |
| + gfx::ImageSkia image_skia(image_skia_rep); |
| + gfx::Image image(image_skia); |
| + |
| + layer_delegate_->SetImage(image); |
| + bitmap_layer_->SchedulePaint(bitmap_layer_->bounds()); |
| } |
| } // namespace demo |
| -} // namespace ui |
| +} // namespace aura |