OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "services/ui/demo/window_tree_data.h" |
| 6 |
| 7 #include "base/time/time.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkColor.h" |
| 11 #include "third_party/skia/include/core/SkImageInfo.h" |
| 12 #include "third_party/skia/include/core/SkPaint.h" |
| 13 #include "third_party/skia/include/core/SkRect.h" |
| 14 #include "ui/aura/mus/window_tree_host_mus.h" |
| 15 #include "ui/aura/window.h" |
| 16 #include "ui/aura_extra/image_window_delegate.h" |
| 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 |
| 20 namespace ui { |
| 21 namespace demo { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Milliseconds between frames. |
| 26 const int64_t kFrameDelay = 33; |
| 27 |
| 28 const SkColor kBgColor = SK_ColorRED; |
| 29 const SkColor kFgColor = SK_ColorYELLOW; |
| 30 |
| 31 void DrawSquare(const gfx::Rect& bounds, |
| 32 double angle, |
| 33 SkCanvas* canvas, |
| 34 int size) { |
| 35 // Create SkRect to draw centered inside the bounds. |
| 36 gfx::Point top_left = bounds.CenterPoint(); |
| 37 top_left.Offset(-size / 2, -size / 2); |
| 38 SkRect rect = SkRect::MakeXYWH(top_left.x(), top_left.y(), size, size); |
| 39 |
| 40 // Set SkPaint to fill solid color. |
| 41 SkPaint paint; |
| 42 paint.setStyle(SkPaint::kFill_Style); |
| 43 paint.setColor(kFgColor); |
| 44 |
| 45 // Rotate the canvas. |
| 46 const gfx::Size canvas_size = bounds.size(); |
| 47 if (angle != 0.0) { |
| 48 canvas->translate(SkFloatToScalar(canvas_size.width() * 0.5f), |
| 49 SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 50 canvas->rotate(angle); |
| 51 canvas->translate(-SkFloatToScalar(canvas_size.width() * 0.5f), |
| 52 -SkFloatToScalar(canvas_size.height() * 0.5f)); |
| 53 } |
| 54 |
| 55 canvas->drawRect(rect, paint); |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
| 60 WindowTreeData::WindowTreeData(int square_size) : square_size_(square_size) {} |
| 61 |
| 62 WindowTreeData::~WindowTreeData() {} |
| 63 |
| 64 aura::Window* WindowTreeData::bitmap_window() { |
| 65 DCHECK(!window_tree_host_->window()->children().empty()); |
| 66 return window_tree_host_->window()->children()[0]; |
| 67 } |
| 68 |
| 69 void WindowTreeData::Init( |
| 70 std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { |
| 71 window_tree_host->InitHost(); |
| 72 window_tree_host->Show(); |
| 73 // Take ownership of the WTH. |
| 74 window_tree_host_ = std::move(window_tree_host); |
| 75 |
| 76 // Initialize the window for the bitmap. |
| 77 window_delegate_ = new aura_extra::ImageWindowDelegate(); |
| 78 aura::Window* root_window = window_tree_host_->window(); |
| 79 aura::Window* bitmap_window = new aura::Window(window_delegate_); |
| 80 bitmap_window->Init(LAYER_TEXTURED); |
| 81 bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size())); |
| 82 bitmap_window->Show(); |
| 83 bitmap_window->SetName("Bitmap"); |
| 84 root_window->AddChild(bitmap_window); |
| 85 |
| 86 // Draw initial frame and start the timer to regularly draw frames. |
| 87 DrawFrame(); |
| 88 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |
| 89 base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); |
| 90 } |
| 91 |
| 92 void WindowTreeData::DrawFrame() { |
| 93 angle_ += 2.0; |
| 94 if (angle_ >= 360.0) |
| 95 angle_ = 0.0; |
| 96 |
| 97 const gfx::Rect& bounds = bitmap_window()->bounds(); |
| 98 |
| 99 // Allocate a bitmap of correct size. |
| 100 SkBitmap bitmap; |
| 101 SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 102 kPremul_SkAlphaType); |
| 103 bitmap.allocPixels(image_info); |
| 104 |
| 105 // Draw the rotated square on background in bitmap. |
| 106 SkCanvas canvas(bitmap); |
| 107 canvas.clear(kBgColor); |
| 108 // TODO(kylechar): Add GL drawing instead of software rasterization in future. |
| 109 DrawSquare(bounds, angle_, &canvas, square_size_); |
| 110 canvas.flush(); |
| 111 |
| 112 gfx::ImageSkiaRep image_skia_rep(bitmap, 1); |
| 113 gfx::ImageSkia image_skia(image_skia_rep); |
| 114 gfx::Image image(image_skia); |
| 115 |
| 116 window_delegate_->SetImage(image); |
| 117 bitmap_window()->SchedulePaintInRect(gfx::Rect(bounds.size())); |
| 118 } |
| 119 |
| 120 } // namespace demo |
| 121 } // namespace ui |
OLD | NEW |