| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "mojo/application/application_runner_chromium.h" | 10 #include "mojo/application/application_runner_chromium.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 constexpr uint32_t kContentImageResourceId = 1; | 25 constexpr uint32_t kContentImageResourceId = 1; |
| 26 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; | 26 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId; |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 class PNGView : public mojo::ui::GaneshView { | 29 class PNGView : public mojo::ui::GaneshView { |
| 30 public: | 30 public: |
| 31 PNGView(mojo::InterfaceHandle<mojo::ApplicationConnector> app_connector, | 31 PNGView(mojo::InterfaceHandle<mojo::ApplicationConnector> app_connector, |
| 32 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request, | 32 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request, |
| 33 const skia::RefPtr<SkImage>& image) | 33 const sk_sp<SkImage>& image) |
| 34 : GaneshView(app_connector.Pass(), | 34 : GaneshView(app_connector.Pass(), |
| 35 view_owner_request.Pass(), | 35 view_owner_request.Pass(), |
| 36 "PNGViewer"), | 36 "PNGViewer"), |
| 37 image_(image) { | 37 image_(image) { |
| 38 DCHECK(image_); | 38 DCHECK(image_); |
| 39 } | 39 } |
| 40 | 40 |
| 41 ~PNGView() override {} | 41 ~PNGView() override {} |
| 42 | 42 |
| 43 private: | 43 private: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } else { | 97 } else { |
| 98 w = image_->width() * size.height / image_->height(); | 98 w = image_->width() * size.height / image_->height(); |
| 99 h = size.height; | 99 h = size.height; |
| 100 } | 100 } |
| 101 canvas->drawImageRect( | 101 canvas->drawImageRect( |
| 102 image_.get(), SkRect::MakeWH(image_->width(), image_->height()), | 102 image_.get(), SkRect::MakeWH(image_->width(), image_->height()), |
| 103 SkRect::MakeXYWH((size.width - w) / 2, (size.height - h) / 2, w, h), | 103 SkRect::MakeXYWH((size.width - w) / 2, (size.height - h) / 2, w, h), |
| 104 nullptr); | 104 nullptr); |
| 105 } | 105 } |
| 106 | 106 |
| 107 skia::RefPtr<SkImage> image_; | 107 sk_sp<SkImage> image_; |
| 108 | 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(PNGView); | 109 DISALLOW_COPY_AND_ASSIGN(PNGView); |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 class PNGContentViewProviderApp : public mojo::ui::ViewProviderApp { | 112 class PNGContentViewProviderApp : public mojo::ui::ViewProviderApp { |
| 113 public: | 113 public: |
| 114 PNGContentViewProviderApp(skia::RefPtr<SkImage> image) : image_(image) { | 114 PNGContentViewProviderApp(sk_sp<SkImage> image) : image_(image) { |
| 115 DCHECK(image_); | 115 DCHECK(image_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 ~PNGContentViewProviderApp() override {} | 118 ~PNGContentViewProviderApp() override {} |
| 119 | 119 |
| 120 void CreateView( | 120 void CreateView( |
| 121 const std::string& connection_url, | 121 const std::string& connection_url, |
| 122 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request, | 122 mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request, |
| 123 mojo::InterfaceRequest<mojo::ServiceProvider> services) override { | 123 mojo::InterfaceRequest<mojo::ServiceProvider> services) override { |
| 124 new PNGView(mojo::CreateApplicationConnector(app_impl()->shell()), | 124 new PNGView(mojo::CreateApplicationConnector(app_impl()->shell()), |
| 125 view_owner_request.Pass(), image_); | 125 view_owner_request.Pass(), image_); |
| 126 } | 126 } |
| 127 | 127 |
| 128 private: | 128 private: |
| 129 skia::RefPtr<SkImage> image_; | 129 sk_sp<SkImage> image_; |
| 130 | 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(PNGContentViewProviderApp); | 131 DISALLOW_COPY_AND_ASSIGN(PNGContentViewProviderApp); |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 class PNGContentViewerApp : public mojo::ui::ContentViewerApp { | 134 class PNGContentViewerApp : public mojo::ui::ContentViewerApp { |
| 135 public: | 135 public: |
| 136 PNGContentViewerApp() {} | 136 PNGContentViewerApp() {} |
| 137 | 137 |
| 138 ~PNGContentViewerApp() override {} | 138 ~PNGContentViewerApp() override {} |
| 139 | 139 |
| 140 mojo::ui::ViewProviderApp* LoadContent( | 140 mojo::ui::ViewProviderApp* LoadContent( |
| 141 const std::string& content_handler_url, | 141 const std::string& content_handler_url, |
| 142 mojo::URLResponsePtr response) override { | 142 mojo::URLResponsePtr response) override { |
| 143 std::string data; | 143 std::string data; |
| 144 mojo::common::BlockingCopyToString(response->body.Pass(), &data); | 144 mojo::common::BlockingCopyToString(response->body.Pass(), &data); |
| 145 SkBitmap bitmap; | 145 SkBitmap bitmap; |
| 146 if (!::gfx::PNGCodec::Decode( | 146 if (!::gfx::PNGCodec::Decode( |
| 147 reinterpret_cast<const unsigned char*>(data.data()), data.length(), | 147 reinterpret_cast<const unsigned char*>(data.data()), data.length(), |
| 148 &bitmap) || | 148 &bitmap) || |
| 149 bitmap.empty()) { | 149 bitmap.empty()) { |
| 150 LOG(ERROR) << "Could not decode PNG."; | 150 LOG(ERROR) << "Could not decode PNG."; |
| 151 return nullptr; | 151 return nullptr; |
| 152 } | 152 } |
| 153 | 153 |
| 154 return new PNGContentViewProviderApp( | 154 return new PNGContentViewProviderApp(SkImage::MakeFromBitmap(bitmap)); |
| 155 skia::AdoptRef(SkImage::NewFromBitmap(bitmap))); | |
| 156 } | 155 } |
| 157 | 156 |
| 158 private: | 157 private: |
| 159 DISALLOW_COPY_AND_ASSIGN(PNGContentViewerApp); | 158 DISALLOW_COPY_AND_ASSIGN(PNGContentViewerApp); |
| 160 }; | 159 }; |
| 161 | 160 |
| 162 } // namespace examples | 161 } // namespace examples |
| 163 | 162 |
| 164 MojoResult MojoMain(MojoHandle application_request) { | 163 MojoResult MojoMain(MojoHandle application_request) { |
| 165 mojo::ApplicationRunnerChromium runner(new examples::PNGContentViewerApp()); | 164 mojo::ApplicationRunnerChromium runner(new examples::PNGContentViewerApp()); |
| 166 return runner.Run(application_request); | 165 return runner.Run(application_request); |
| 167 } | 166 } |
| OLD | NEW |