Chromium Code Reviews| 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 { | |
| 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) { | |
|
rjkroege
2016/03/29 18:53:22
This is software rasterization -> MUS. A future it
kylechar
2016/03/29 20:43:39
Done.
| |
| 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, &host_, this); | |
| 67 host_->SetTitle("MUS Demo"); | |
| 68 } | |
| 69 | |
| 70 bool MusDemo::AcceptConnection(mojo::Connection* connection) { | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void MusDemo::OnEmbed(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(Window* root) {} | |
| 88 | |
| 89 void MusDemo::OnConnectionLost(WindowTreeConnection* connection) { | |
| 90 timer_.Stop(); | |
| 91 } | |
| 92 | |
| 93 void MusDemo::SetWindowManagerClient(WindowManagerClient* client) {} | |
| 94 | |
| 95 bool MusDemo::OnWmSetBounds(Window* window, gfx::Rect* bounds) { | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool MusDemo::OnWmSetProperty(Window* window, | |
| 100 const std::string& name, | |
| 101 scoped_ptr<std::vector<uint8_t>>* new_data) { | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 Window* MusDemo::OnWmCreateTopLevelWindow( | |
| 106 std::map<std::string, std::vector<uint8_t>>* properties) { | |
| 107 // TODO(kylechar): Check if this is needed. | |
| 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 DrawSquare(bounds, angle_, &canvas); | |
| 140 canvas.flush(); | |
| 141 | |
| 142 // 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.
| |
| 143 bitmap_.lockPixels(); | |
| 144 const unsigned char* addr = | |
| 145 static_cast<const unsigned char*>(bitmap_.getPixels()); | |
| 146 const int bytes = bounds.width() * bounds.height() * 4; | |
| 147 scoped_ptr<std::vector<unsigned char>> data( | |
| 148 new std::vector<unsigned char>(addr, addr + bytes)); | |
| 149 bitmap_.unlockPixels(); | |
| 150 | |
| 151 // Send frame to MUS via BitmapUploader. | |
| 152 uploader_->SetBitmap(bounds.width(), bounds.height(), std::move(data), | |
| 153 bitmap_uploader::BitmapUploader::BGRA); | |
| 154 } | |
| 155 | |
| 156 } // namespace mus | |
| OLD | NEW |