Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: examples/ui/png_viewer/png_viewer.cc

Issue 1559723002: Update the UI examples. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-14
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 <string>
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "mojo/application/application_runner_chromium.h"
11 #include "mojo/data_pipe_utils/data_pipe_utils.h"
12 #include "mojo/public/c/system/main.h"
13 #include "mojo/ui/content_viewer_app.h"
14 #include "mojo/ui/ganesh_view.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkImage.h"
18 #include "third_party/skia/include/core/SkSurface.h"
19 #include "ui/gfx/codec/png_codec.h"
20
21 namespace examples {
22
23 constexpr uint32_t kContentImageResourceId = 1;
24 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId;
25
26 class PNGView : public mojo::ui::GaneshView {
27 public:
28 PNGView(
29 mojo::ApplicationImpl* app_impl,
30 const skia::RefPtr<SkImage>& image,
31 const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback)
32 : GaneshView(app_impl, "PNGViewer", create_view_callback), image_(image) {
33 DCHECK(image_);
34 }
35
36 ~PNGView() override {}
37
38 private:
39 // |View|:
40 void OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params,
41 mojo::Array<uint32_t> children_needing_layout,
42 const OnLayoutCallback& callback) override {
43 size_.width = layout_params->constraints->max_width;
44 size_.height = layout_params->constraints->max_height;
45
46 auto info = mojo::ui::ViewLayoutResult::New();
47 info->size = size_.Clone();
48 callback.Run(info.Pass());
49
50 UpdateScene();
51 }
52
53 void UpdateScene() {
54 mojo::Rect bounds;
55 bounds.width = size_.width;
56 bounds.height = size_.height;
57
58 auto update = mojo::gfx::composition::SceneUpdate::New();
59 mojo::gfx::composition::ResourcePtr content_resource =
60 ganesh_renderer()->DrawCanvas(
61 size_, base::Bind(&PNGView::DrawContent, base::Unretained(this)));
62 DCHECK(content_resource);
63 update->resources.insert(kContentImageResourceId, content_resource.Pass());
64
65 auto root_node = mojo::gfx::composition::Node::New();
66 root_node->op = mojo::gfx::composition::NodeOp::New();
67 root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New());
68 root_node->op->get_image()->content_rect = bounds.Clone();
69 root_node->op->get_image()->image_resource_id = kContentImageResourceId;
70 update->nodes.insert(kRootNodeId, root_node.Pass());
71
72 scene()->Update(update.Pass());
73 scene()->Publish(nullptr);
74 }
75
76 void DrawContent(SkCanvas* canvas) {
77 canvas->clear(SK_ColorBLACK);
78
79 int32_t w, h;
80 if (size_.width * image_->height() < size_.height * image_->width()) {
81 w = size_.width;
82 h = image_->height() * size_.width / image_->width();
83 } else {
84 w = image_->width() * size_.height / image_->height();
85 h = size_.height;
86 }
87 canvas->drawImageRect(
88 image_.get(), SkRect::MakeWH(image_->width(), image_->height()),
89 SkRect::MakeXYWH((size_.width - w) / 2, (size_.height - h) / 2, w, h),
90 nullptr);
91
92 canvas->flush();
93 }
94
95 skia::RefPtr<SkImage> image_;
96 mojo::Size size_;
97
98 DISALLOW_COPY_AND_ASSIGN(PNGView);
99 };
100
101 class PNGContentViewProviderApp : public mojo::ui::ViewProviderApp {
102 public:
103 PNGContentViewProviderApp(skia::RefPtr<SkImage> image) : image_(image) {
104 DCHECK(image_);
105 }
106
107 ~PNGContentViewProviderApp() override {}
108
109 bool CreateView(
110 const std::string& connection_url,
111 mojo::InterfaceRequest<mojo::ServiceProvider> services,
112 mojo::ServiceProviderPtr exposed_services,
113 const mojo::ui::ViewProvider::CreateViewCallback& callback) override {
114 new PNGView(app_impl(), image_, callback);
115 return true;
116 }
117
118 private:
119 skia::RefPtr<SkImage> image_;
120
121 DISALLOW_COPY_AND_ASSIGN(PNGContentViewProviderApp);
122 };
123
124 class PNGContentViewerApp : public mojo::ui::ContentViewerApp {
125 public:
126 PNGContentViewerApp() {}
127
128 ~PNGContentViewerApp() override {}
129
130 mojo::ui::ViewProviderApp* LoadContent(
131 const std::string& content_handler_url,
132 mojo::URLResponsePtr response) override {
133 std::string data;
134 mojo::common::BlockingCopyToString(response->body.Pass(), &data);
135 SkBitmap bitmap;
136 if (!::gfx::PNGCodec::Decode(
137 reinterpret_cast<const unsigned char*>(data.data()), data.length(),
138 &bitmap) ||
139 bitmap.empty()) {
140 LOG(ERROR) << "Could not decode PNG.";
141 return nullptr;
142 }
143
144 return new PNGContentViewProviderApp(
145 skia::AdoptRef(SkImage::NewFromBitmap(bitmap)));
146 }
147
148 private:
149 DISALLOW_COPY_AND_ASSIGN(PNGContentViewerApp);
150 };
151
152 } // namespace examples
153
154 MojoResult MojoMain(MojoHandle application_request) {
155 mojo::ApplicationRunnerChromium runner(new examples::PNGContentViewerApp());
156 return runner.Run(application_request);
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698