| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/mus/demo/mus_demo.h" |
| 6 |
| 7 #include "base/time/time.h" |
| 8 #include "components/bitmap_uploader/bitmap_uploader.h" |
| 9 #include "components/mus/public/cpp/window_tree_host_factory.h" |
| 10 #include "third_party/skia/include/core/SkCanvas.h" |
| 11 #include "third_party/skia/include/core/SkColor.h" |
| 12 #include "third_party/skia/include/core/SkImageInfo.h" |
| 13 #include "third_party/skia/include/core/SkPaint.h" |
| 14 #include "third_party/skia/include/core/SkRect.h" |
| 15 #include "ui/gfx/geometry/rect.h" |
| 16 |
| 17 namespace mus_demo { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Milliseconds between frames. |
| 22 const int64_t kFrameDelay = 33; |
| 23 |
| 24 // Size of square in pixels to draw. |
| 25 const int kSquareSize = 300; |
| 26 |
| 27 const SkColor kBgColor = SK_ColorRED; |
| 28 const SkColor kFgColor = SK_ColorYELLOW; |
| 29 |
| 30 void DrawSquare(const gfx::Rect& bounds, double angle, SkCanvas* canvas) { |
| 31 // Create SkRect to draw centered inside the bounds. |
| 32 gfx::Point top_left = bounds.CenterPoint(); |
| 33 top_left.Offset(-kSquareSize / 2, -kSquareSize / 2); |
| 34 SkRect rect = |
| 35 SkRect::MakeXYWH(top_left.x(), top_left.y(), kSquareSize, kSquareSize); |
| 36 |
| 37 // Set SkPaint to fill solid color. |
| 38 SkPaint paint; |
| 39 paint.setStyle(SkPaint::kFill_Style); |
| 40 paint.setColor(kFgColor); |
| 41 |
| 42 // Rotate the canvas. |
| 43 const gfx::Size canvas_size = bounds.size(); |
| 44 if (angle != 0.0) { |
| 45 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), |
| 46 SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 47 canvas->rotate(angle); |
| 48 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
| 49 -SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 50 } |
| 51 |
| 52 canvas->drawRect(rect, paint); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 MusDemo::MusDemo() {} |
| 58 |
| 59 MusDemo::~MusDemo() {} |
| 60 |
| 61 void MusDemo::Initialize(mojo::Connector* connector, |
| 62 const mojo::Identity& identity, |
| 63 uint32_t id) { |
| 64 connector_ = connector; |
| 65 |
| 66 CreateWindowTreeHost(connector_, this, &window_tree_host_, this); |
| 67 window_tree_host_->SetTitle("MUS Demo"); |
| 68 } |
| 69 |
| 70 bool MusDemo::AcceptConnection(mojo::Connection* connection) { |
| 71 return true; |
| 72 } |
| 73 |
| 74 void MusDemo::OnEmbed(mus::Window* window) { |
| 75 window_ = window; |
| 76 |
| 77 // Initialize bitmap uploader for sending frames to MUS. |
| 78 uploader_.reset(new bitmap_uploader::BitmapUploader(window_)); |
| 79 uploader_->Init(connector_); |
| 80 |
| 81 // Draw initial frame and start the timer to regularly draw frames. |
| 82 DrawFrame(); |
| 83 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| 84 base::Bind(&MusDemo::DrawFrame, base::Unretained(this))); |
| 85 } |
| 86 |
| 87 void MusDemo::OnUnembed(mus::Window* root) {} |
| 88 |
| 89 void MusDemo::OnConnectionLost(mus::WindowTreeConnection* connection) { |
| 90 timer_.Stop(); |
| 91 } |
| 92 |
| 93 void MusDemo::SetWindowManagerClient(mus::WindowManagerClient* client) {} |
| 94 |
| 95 bool MusDemo::OnWmSetBounds(mus::Window* window, gfx::Rect* bounds) { |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool MusDemo::OnWmSetProperty(mus::Window* window, |
| 100 const std::string& name, |
| 101 scoped_ptr<std::vector<uint8_t>>* new_data) { |
| 102 return true; |
| 103 } |
| 104 |
| 105 mus::Window* MusDemo::OnWmCreateTopLevelWindow( |
| 106 std::map<std::string, std::vector<uint8_t>>* properties) { |
| 107 // TODO(kylechar): Check if this should return something useful. |
| 108 return nullptr; |
| 109 } |
| 110 |
| 111 void MusDemo::OnAccelerator(uint32_t id, const ui::Event& event) {} |
| 112 |
| 113 void MusDemo::AllocBitmap() { |
| 114 const gfx::Rect bounds = window_->GetBoundsInRoot(); |
| 115 |
| 116 // Allocate bitmap the same size as the window for drawing. |
| 117 bitmap_.reset(); |
| 118 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 119 kPremul_SkAlphaType); |
| 120 bitmap_.allocPixels(image_info); |
| 121 } |
| 122 |
| 123 void MusDemo::DrawFrame() { |
| 124 angle_ += 2.0; |
| 125 if (angle_ >= 360.0) |
| 126 angle_ = 0.0; |
| 127 |
| 128 const gfx::Rect bounds = window_->GetBoundsInRoot(); |
| 129 |
| 130 // Check that bitmap and window sizes match, otherwise reallocate bitmap. |
| 131 const SkImageInfo info = bitmap_.info(); |
| 132 if (info.width() != bounds.width() || info.height() != bounds.height()) { |
| 133 AllocBitmap(); |
| 134 } |
| 135 |
| 136 // Draw the rotated square on background in bitmap. |
| 137 SkCanvas canvas(bitmap_); |
| 138 canvas.clear(kBgColor); |
| 139 // TODO(kylechar): Add GL drawing instead of software rasterization in future. |
| 140 DrawSquare(bounds, angle_, &canvas); |
| 141 canvas.flush(); |
| 142 |
| 143 // Copy pixels data into vector that will be passed to BitmapUploader. |
| 144 // TODO(rjkroege): Make a 1/0-copy bitmap uploader for the contents of a |
| 145 // SkBitmap. |
| 146 bitmap_.lockPixels(); |
| 147 const unsigned char* addr = |
| 148 static_cast<const unsigned char*>(bitmap_.getPixels()); |
| 149 const int bytes = bounds.width() * bounds.height() * 4; |
| 150 scoped_ptr<std::vector<unsigned char>> data( |
| 151 new std::vector<unsigned char>(addr, addr + bytes)); |
| 152 bitmap_.unlockPixels(); |
| 153 |
| 154 // Send frame to MUS via BitmapUploader. |
| 155 uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), |
| 156 bitmap_uploader::BitmapUploader::BGRA); |
| 157 } |
| 158 |
| 159 } // namespace mus_demo |
| OLD | NEW |