| 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                 unsigned 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(unsigned square_size) |  | 
|   61     : square_size_(square_size) {} |  | 
|   62  |  | 
|   63 WindowTreeData::~WindowTreeData() {} |  | 
|   64  |  | 
|   65 aura::Window* WindowTreeData::bitmap_window() { |  | 
|   66   DCHECK(!window_tree_host_->window()->children().empty()); |  | 
|   67   return window_tree_host_->window()->children()[0]; |  | 
|   68 } |  | 
|   69  |  | 
|   70 void WindowTreeData::Init( |  | 
|   71     std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) { |  | 
|   72   window_tree_host->InitHost(); |  | 
|   73   window_tree_host->Show(); |  | 
|   74   // Take ownership of the WTH. |  | 
|   75   window_tree_host_ = std::move(window_tree_host); |  | 
|   76  |  | 
|   77   // Initialize the window for the bitmap. |  | 
|   78   window_delegate_ = new aura_extra::ImageWindowDelegate(); |  | 
|   79   aura::Window* root_window = window_tree_host_->window(); |  | 
|   80   aura::Window* bitmap_window = new aura::Window(window_delegate_); |  | 
|   81   bitmap_window->Init(LAYER_TEXTURED); |  | 
|   82   bitmap_window->SetBounds(gfx::Rect(root_window->bounds().size())); |  | 
|   83   bitmap_window->Show(); |  | 
|   84   bitmap_window->SetName("Bitmap"); |  | 
|   85   root_window->AddChild(bitmap_window); |  | 
|   86  |  | 
|   87   // Draw initial frame and start the timer to regularly draw frames. |  | 
|   88   DrawFrame(); |  | 
|   89   timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kFrameDelay), |  | 
|   90                base::Bind(&WindowTreeData::DrawFrame, base::Unretained(this))); |  | 
|   91 } |  | 
|   92  |  | 
|   93 void WindowTreeData::DrawFrame() { |  | 
|   94   angle_ += 2.0; |  | 
|   95   if (angle_ >= 360.0) |  | 
|   96     angle_ = 0.0; |  | 
|   97  |  | 
|   98   const gfx::Rect& bounds = bitmap_window()->bounds(); |  | 
|   99  |  | 
|  100   // Allocate a bitmap of correct size. |  | 
|  101   SkBitmap bitmap; |  | 
|  102   SkImageInfo image_info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |  | 
|  103                                                 kPremul_SkAlphaType); |  | 
|  104   bitmap.allocPixels(image_info); |  | 
|  105  |  | 
|  106   // Draw the rotated square on background in bitmap. |  | 
|  107   SkCanvas canvas(bitmap); |  | 
|  108   canvas.clear(kBgColor); |  | 
|  109   // TODO(kylechar): Add GL drawing instead of software rasterization in future. |  | 
|  110   DrawSquare(bounds, angle_, &canvas, square_size_); |  | 
|  111   canvas.flush(); |  | 
|  112  |  | 
|  113   gfx::ImageSkiaRep image_skia_rep(bitmap, 1); |  | 
|  114   gfx::ImageSkia image_skia(image_skia_rep); |  | 
|  115   gfx::Image image(image_skia); |  | 
|  116  |  | 
|  117   window_delegate_->SetImage(image); |  | 
|  118   bitmap_window()->SchedulePaintInRect(gfx::Rect(bounds.size())); |  | 
|  119 } |  | 
|  120  |  | 
|  121 }  // namespace demo |  | 
|  122 }  // namespace ui |  | 
| OLD | NEW |