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 constexpr uint32_t kContentImageResourceId = 1; | |
16 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; | |
17 | |
18 ShapesView::ShapesView( | |
19 mojo::ApplicationImpl* app_impl, | |
20 const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback) | |
21 : GaneshView(app_impl, "Shapes", create_view_callback) {} | |
22 | |
23 ShapesView::~ShapesView() {} | |
24 | |
25 void ShapesView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params, | |
26 mojo::Array<uint32_t> children_needing_layout, | |
27 const OnLayoutCallback& callback) { | |
28 size_.width = layout_params->constraints->max_width; | |
29 size_.height = layout_params->constraints->max_height; | |
30 | |
31 // Submit the new layout information. | |
32 auto info = mojo::ui::ViewLayoutResult::New(); | |
33 info->size = size_.Clone(); | |
34 callback.Run(info.Pass()); | |
35 | |
36 // Draw! | |
37 UpdateScene(); | |
38 } | |
39 | |
40 void ShapesView::UpdateScene() { | |
41 mojo::Rect bounds; | |
42 bounds.width = size_.width; | |
43 bounds.height = size_.height; | |
44 | |
45 auto update = mojo::gfx::composition::SceneUpdate::New(); | |
viettrungluu
2016/01/13 00:10:57
Since this is a basic example, a few more (brief)
jeffbrown
2016/01/26 08:14:17
Done.
| |
46 mojo::gfx::composition::ResourcePtr content_resource = | |
47 ganesh_renderer()->DrawCanvas( | |
48 size_, base::Bind(&ShapesView::DrawContent, base::Unretained(this))); | |
49 DCHECK(content_resource); | |
50 update->resources.insert(kContentImageResourceId, content_resource.Pass()); | |
51 | |
52 auto root_node = mojo::gfx::composition::Node::New(); | |
53 root_node->op = mojo::gfx::composition::NodeOp::New(); | |
54 root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New()); | |
55 root_node->op->get_image()->content_rect = bounds.Clone(); | |
56 root_node->op->get_image()->image_resource_id = kContentImageResourceId; | |
57 update->nodes.insert(kRootNodeId, root_node.Pass()); | |
58 | |
59 scene()->Update(update.Pass()); | |
60 scene()->Publish(nullptr); | |
61 } | |
62 | |
63 void ShapesView::DrawContent(SkCanvas* canvas) { | |
64 canvas->clear(SK_ColorCYAN); | |
65 | |
66 SkPaint paint; | |
67 paint.setColor(SK_ColorGREEN); | |
68 SkRect rect = SkRect::MakeWH(size_.width, size_.height); | |
69 rect.inset(10, 10); | |
70 canvas->drawRect(rect, paint); | |
71 | |
72 paint.setColor(SK_ColorRED); | |
73 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
74 canvas->drawCircle(50, 100, 100, paint); | |
75 | |
76 canvas->flush(); | |
77 } | |
78 | |
79 } // namespace examples | |
OLD | NEW |