OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "examples/ui/shapes/shapes_view.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.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/SkSurface.h" |
| 12 |
| 13 namespace examples { |
| 14 |
| 15 namespace { |
| 16 constexpr uint32_t kContentImageResourceId = 1; |
| 17 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; |
| 18 } // namespace |
| 19 |
| 20 ShapesView::ShapesView( |
| 21 mojo::ApplicationImpl* app_impl, |
| 22 const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback) |
| 23 : GaneshView(app_impl, "Shapes", create_view_callback) {} |
| 24 |
| 25 ShapesView::~ShapesView() {} |
| 26 |
| 27 void ShapesView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params, |
| 28 mojo::Array<uint32_t> children_needing_layout, |
| 29 const OnLayoutCallback& callback) { |
| 30 size_.width = layout_params->constraints->max_width; |
| 31 size_.height = layout_params->constraints->max_height; |
| 32 |
| 33 // Submit the new layout information. |
| 34 auto info = mojo::ui::ViewLayoutResult::New(); |
| 35 info->size = size_.Clone(); |
| 36 callback.Run(info.Pass()); |
| 37 |
| 38 // Draw! |
| 39 UpdateScene(); |
| 40 } |
| 41 |
| 42 void ShapesView::UpdateScene() { |
| 43 mojo::Rect bounds; |
| 44 bounds.width = size_.width; |
| 45 bounds.height = size_.height; |
| 46 |
| 47 auto update = mojo::gfx::composition::SceneUpdate::New(); |
| 48 |
| 49 // Draw the content of the view to a texture and include it as an |
| 50 // image resource in the scene. |
| 51 mojo::gfx::composition::ResourcePtr content_resource = |
| 52 ganesh_renderer()->DrawCanvas( |
| 53 size_, base::Bind(&ShapesView::DrawContent, base::Unretained(this))); |
| 54 DCHECK(content_resource); |
| 55 update->resources.insert(kContentImageResourceId, content_resource.Pass()); |
| 56 |
| 57 // Add a root node to the scene graph to draw the image resource to |
| 58 // the screen such that it fills the entire view. |
| 59 auto root_node = mojo::gfx::composition::Node::New(); |
| 60 root_node->op = mojo::gfx::composition::NodeOp::New(); |
| 61 root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New()); |
| 62 root_node->op->get_image()->content_rect = bounds.Clone(); |
| 63 root_node->op->get_image()->image_resource_id = kContentImageResourceId; |
| 64 update->nodes.insert(kRootNodeId, root_node.Pass()); |
| 65 |
| 66 // Submit the scene update then publish it to cause the changes to be |
| 67 // applied. |
| 68 scene()->Update(update.Pass()); |
| 69 scene()->Publish(nullptr); |
| 70 } |
| 71 |
| 72 void ShapesView::DrawContent(SkCanvas* canvas) { |
| 73 canvas->clear(SK_ColorCYAN); |
| 74 |
| 75 SkPaint paint; |
| 76 paint.setColor(SK_ColorGREEN); |
| 77 SkRect rect = SkRect::MakeWH(size_.width, size_.height); |
| 78 rect.inset(10, 10); |
| 79 canvas->drawRect(rect, paint); |
| 80 |
| 81 paint.setColor(SK_ColorRED); |
| 82 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 83 canvas->drawCircle(50, 100, 100, paint); |
| 84 } |
| 85 |
| 86 } // namespace examples |
OLD | NEW |