Chromium Code Reviews| Index: components/mus/demo/mus_demo.cc |
| diff --git a/components/mus/demo/mus_demo.cc b/components/mus/demo/mus_demo.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..544d49f6db29ee95871b3d59b603fbd911cb16a7 |
| --- /dev/null |
| +++ b/components/mus/demo/mus_demo.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/mus/demo/mus_demo.h" |
| + |
| +#include "base/time/time.h" |
| +#include "components/bitmap_uploader/bitmap_uploader.h" |
| +#include "components/mus/public/cpp/window_tree_host_factory.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/gfx/geometry/rect.h" |
| + |
| +namespace mus { |
| + |
| +namespace { |
| + |
| +// Milliseconds between frames. |
| +const int64_t kFrameDelay = 33; |
| + |
| +// Size of square in pixels to draw. |
| +const int kSquareSize = 300; |
| + |
| +const SkColor kBgColor = SK_ColorRED; |
| +const SkColor kFgColor = SK_ColorYELLOW; |
| + |
| +void DrawSquare(const gfx::Rect& bounds, double angle, SkCanvas* canvas) { |
|
rjkroege
2016/03/29 18:53:22
This is software rasterization -> MUS. A future it
kylechar
2016/03/29 20:43:39
Done.
|
| + // Create SkRect to draw centered inside the bounds. |
| + gfx::Point top_left = bounds.CenterPoint(); |
| + top_left.Offset(-kSquareSize / 2, -kSquareSize / 2); |
| + SkRect rect = |
| + SkRect::MakeXYWH(top_left.x(), top_left.y(), kSquareSize, kSquareSize); |
| + |
| + // Set SkPaint to fill solid color. |
| + SkPaint paint; |
| + paint.setStyle(SkPaint::kFill_Style); |
| + paint.setColor(kFgColor); |
| + |
| + // Rotate the canvas. |
| + const gfx::Size canvas_size = bounds.size(); |
| + if (angle != 0.0) { |
| + canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), |
| + SkFloatToScalar(canvas_size.height() * 0.5f)); |
| + canvas->rotate(angle); |
| + canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
| + -SkFloatToScalar(canvas_size.height() * 0.5f)); |
| + } |
| + |
| + canvas->drawRect(rect, paint); |
| +} |
| + |
| +} // namespace |
| + |
| +MusDemo::MusDemo() {} |
| + |
| +MusDemo::~MusDemo() {} |
| + |
| +void MusDemo::Initialize(mojo::Connector* connector, |
| + const mojo::Identity& identity, |
| + uint32_t id) { |
| + connector_ = connector; |
| + |
| + CreateWindowTreeHost(connector_, this, &host_, this); |
| + host_->SetTitle("MUS Demo"); |
| +} |
| + |
| +bool MusDemo::AcceptConnection(mojo::Connection* connection) { |
| + return true; |
| +} |
| + |
| +void MusDemo::OnEmbed(Window* window) { |
| + window_ = window; |
| + |
| + // Initialize bitmap uploader for sending frames to MUS. |
| + uploader_.reset(new bitmap_uploader::BitmapUploader(window_)); |
| + uploader_->Init(connector_); |
| + |
| + // Draw initial frame and start the timer to regularly draw frames. |
| + DrawFrame(); |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| + base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); |
| +} |
| + |
| +void MusDemo::OnUnembed(Window* root) {} |
| + |
| +void MusDemo::OnConnectionLost(WindowTreeConnection* connection) { |
| + timer_.Stop(); |
| +} |
| + |
| +void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} |
| + |
| +bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { |
| + return true; |
| +} |
| + |
| +bool MusDemo::OnWmSetProperty(Window* window, |
| + const std::string& name, |
| + scoped_ptr<std::vector<uint8_t>>* new_data) { |
| + return true; |
| +} |
| + |
| +Window* MusDemo::OnWmCreateTopLevelWindow( |
| + std::map<std::string, std::vector<uint8_t>>* properties) { |
| + // TODO(kylechar): Check if this is needed. |
| + return nullptr; |
| +} |
| + |
| +void MusDemo::OnAccelerator(uint32_t id, const ui::Event& event) {} |
| + |
| +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::DrawFrame() { |
| + angle_ += 2.0; |
| + 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(); |
| + } |
| + |
| + // Draw the rotated square on background in bitmap. |
| + SkCanvas canvas(bitmap_); |
| + canvas.clear(kBgColor); |
| + DrawSquare(bounds, angle_, &canvas); |
| + canvas.flush(); |
| + |
| + // Copy pixels data into vector that will be passed to BitmapUploader. |
|
rjkroege
2016/03/29 18:53:22
I know you mostly copied this code from what I wro
kylechar
2016/03/29 20:43:39
Done.
|
| + bitmap_.lockPixels(); |
| + const unsigned char* addr = |
| + static_cast<const unsigned char*>(bitmap_.getPixels()); |
| + const int bytes = bounds.width() * bounds.height() * 4; |
| + scoped_ptr<std::vector<unsigned char>> data( |
| + new std::vector<unsigned char>(addr, addr + bytes)); |
| + bitmap_.unlockPixels(); |
| + |
| + // Send frame to MUS via BitmapUploader. |
| + uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), |
| + bitmap_uploader::BitmapUploader::BGRA); |
| +} |
| + |
| +} // namespace mus |