Chromium Code Reviews| Index: examples/ui/shapes/shapes_view.cc |
| diff --git a/examples/ui/shapes/shapes_view.cc b/examples/ui/shapes/shapes_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4d44710a5c3cc6d26d8e76e80d8d4c5c42152e47 |
| --- /dev/null |
| +++ b/examples/ui/shapes/shapes_view.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "examples/ui/shapes/shapes_view.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "third_party/skia/include/core/SkSurface.h" |
| + |
| +namespace examples { |
| + |
| +constexpr uint32_t kContentImageResourceId = 1; |
| +constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; |
| + |
| +ShapesView::ShapesView( |
| + mojo::ApplicationImpl* app_impl, |
| + const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback) |
| + : GaneshView(app_impl, "Shapes", create_view_callback) {} |
| + |
| +ShapesView::~ShapesView() {} |
| + |
| +void ShapesView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params, |
| + mojo::Array<uint32_t> children_needing_layout, |
| + const OnLayoutCallback& callback) { |
| + size_.width = layout_params->constraints->max_width; |
| + size_.height = layout_params->constraints->max_height; |
| + |
| + // Submit the new layout information. |
| + auto info = mojo::ui::ViewLayoutResult::New(); |
| + info->size = size_.Clone(); |
| + callback.Run(info.Pass()); |
| + |
| + // Draw! |
| + UpdateScene(); |
| +} |
| + |
| +void ShapesView::UpdateScene() { |
| + mojo::Rect bounds; |
| + bounds.width = size_.width; |
| + bounds.height = size_.height; |
| + |
| + 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.
|
| + mojo::gfx::composition::ResourcePtr content_resource = |
| + ganesh_renderer()->DrawCanvas( |
| + size_, base::Bind(&ShapesView::DrawContent, base::Unretained(this))); |
| + DCHECK(content_resource); |
| + update->resources.insert(kContentImageResourceId, content_resource.Pass()); |
| + |
| + auto root_node = mojo::gfx::composition::Node::New(); |
| + root_node->op = mojo::gfx::composition::NodeOp::New(); |
| + root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New()); |
| + root_node->op->get_image()->content_rect = bounds.Clone(); |
| + root_node->op->get_image()->image_resource_id = kContentImageResourceId; |
| + update->nodes.insert(kRootNodeId, root_node.Pass()); |
| + |
| + scene()->Update(update.Pass()); |
| + scene()->Publish(nullptr); |
| +} |
| + |
| +void ShapesView::DrawContent(SkCanvas* canvas) { |
| + canvas->clear(SK_ColorCYAN); |
| + |
| + SkPaint paint; |
| + paint.setColor(SK_ColorGREEN); |
| + SkRect rect = SkRect::MakeWH(size_.width, size_.height); |
| + rect.inset(10, 10); |
| + canvas->drawRect(rect, paint); |
| + |
| + paint.setColor(SK_ColorRED); |
| + paint.setFlags(SkPaint::kAntiAlias_Flag); |
| + canvas->drawCircle(50, 100, 100, paint); |
| + |
| + canvas->flush(); |
| +} |
| + |
| +} // namespace examples |