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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" | 9 #include "mojo/examples/media_viewer/media_viewer.mojom.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/services/public/cpp/view_manager/node.h" | 12 #include "mojo/services/public/cpp/view_manager/node.h" |
| 13 #include "mojo/services/public/cpp/view_manager/node_observer.h" |
13 #include "mojo/services/public/cpp/view_manager/types.h" | 14 #include "mojo/services/public/cpp/view_manager/types.h" |
14 #include "mojo/services/public/cpp/view_manager/view.h" | 15 #include "mojo/services/public/cpp/view_manager/view.h" |
15 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
16 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
17 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
18 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
19 #include "skia/ext/refptr.h" | 20 #include "skia/ext/refptr.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
21 #include "third_party/skia/include/core/SkCanvas.h" | 22 #include "third_party/skia/include/core/SkCanvas.h" |
22 #include "third_party/skia/include/core/SkPaint.h" | 23 #include "third_party/skia/include/core/SkPaint.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 105 } |
105 return 0; | 106 return 0; |
106 } | 107 } |
107 | 108 |
108 PNGViewer* viewer_; | 109 PNGViewer* viewer_; |
109 | 110 |
110 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 111 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
111 }; | 112 }; |
112 | 113 |
113 class PNGViewer : public ApplicationDelegate, | 114 class PNGViewer : public ApplicationDelegate, |
114 public view_manager::ViewManagerDelegate { | 115 public view_manager::ViewManagerDelegate, |
| 116 public view_manager::NodeObserver { |
115 public: | 117 public: |
116 PNGViewer() : content_view_(NULL), zoom_percentage_(kDefaultZoomPercentage) {} | 118 PNGViewer() |
117 virtual ~PNGViewer() {} | 119 : content_view_(NULL), |
| 120 root_(NULL), |
| 121 zoom_percentage_(kDefaultZoomPercentage) {} |
| 122 virtual ~PNGViewer() { |
| 123 if (root_) |
| 124 root_->RemoveObserver(this); |
| 125 } |
118 | 126 |
119 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { | 127 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
120 bitmap_ = bitmap; | 128 bitmap_ = bitmap; |
121 zoom_percentage_ = kDefaultZoomPercentage; | 129 zoom_percentage_ = kDefaultZoomPercentage; |
122 DrawBitmap(); | 130 DrawBitmap(); |
123 } | 131 } |
124 | 132 |
125 void ZoomIn() { | 133 void ZoomIn() { |
126 if (zoom_percentage_ >= kMaxZoomPercentage) | 134 if (zoom_percentage_ >= kMaxZoomPercentage) |
127 return; | 135 return; |
(...skipping 26 matching lines...) Expand all Loading... |
154 MOJO_OVERRIDE { | 162 MOJO_OVERRIDE { |
155 connection->AddService<NavigatorImpl>(this); | 163 connection->AddService<NavigatorImpl>(this); |
156 connection->AddService<ZoomableMediaImpl>(this); | 164 connection->AddService<ZoomableMediaImpl>(this); |
157 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); | 165 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); |
158 return true; | 166 return true; |
159 } | 167 } |
160 | 168 |
161 // Overridden from view_manager::ViewManagerDelegate: | 169 // Overridden from view_manager::ViewManagerDelegate: |
162 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 170 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
163 view_manager::Node* root) OVERRIDE { | 171 view_manager::Node* root) OVERRIDE { |
| 172 root_ = root; |
| 173 root_->AddObserver(this); |
164 content_view_ = view_manager::View::Create(view_manager); | 174 content_view_ = view_manager::View::Create(view_manager); |
165 root->SetActiveView(content_view_); | 175 root_->SetActiveView(content_view_); |
166 content_view_->SetColor(SK_ColorGRAY); | 176 content_view_->SetColor(SK_ColorGRAY); |
167 if (!bitmap_.isNull()) | 177 if (!bitmap_.isNull()) |
168 DrawBitmap(); | 178 DrawBitmap(); |
169 } | 179 } |
170 virtual void OnViewManagerDisconnected( | 180 virtual void OnViewManagerDisconnected( |
171 view_manager::ViewManager* view_manager) OVERRIDE { | 181 view_manager::ViewManager* view_manager) OVERRIDE { |
172 base::MessageLoop::current()->Quit(); | 182 base::MessageLoop::current()->Quit(); |
173 } | 183 } |
174 | 184 |
175 void DrawBitmap() { | 185 void DrawBitmap() { |
176 if (!content_view_) | 186 if (!content_view_) |
177 return; | 187 return; |
178 | 188 |
179 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( | 189 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
180 content_view_->node()->bounds().width(), | 190 content_view_->node()->bounds().width(), |
181 content_view_->node()->bounds().height(), | 191 content_view_->node()->bounds().height(), |
182 true))); | 192 true))); |
183 canvas->drawColor(SK_ColorGRAY); | 193 canvas->drawColor(SK_ColorGRAY); |
184 SkPaint paint; | 194 SkPaint paint; |
185 SkScalar scale = | 195 SkScalar scale = |
186 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | 196 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
187 canvas->scale(scale, scale); | 197 canvas->scale(scale, scale); |
188 canvas->drawBitmap(bitmap_, 0, 0, &paint); | 198 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
189 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | 199 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
190 } | 200 } |
191 | 201 |
| 202 // NodeObserver: |
| 203 virtual void OnNodeBoundsChanged(view_manager::Node* node, |
| 204 const gfx::Rect& old_bounds, |
| 205 const gfx::Rect& new_bounds) OVERRIDE { |
| 206 DCHECK_EQ(node, root_); |
| 207 DrawBitmap(); |
| 208 } |
| 209 virtual void OnNodeDestroyed(view_manager::Node* node) OVERRIDE { |
| 210 DCHECK_EQ(node, root_); |
| 211 node->RemoveObserver(this); |
| 212 root_ = NULL; |
| 213 } |
| 214 |
192 view_manager::View* content_view_; | 215 view_manager::View* content_view_; |
| 216 view_manager::Node* root_; |
193 SkBitmap bitmap_; | 217 SkBitmap bitmap_; |
194 uint16_t zoom_percentage_; | 218 uint16_t zoom_percentage_; |
195 | 219 |
196 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 220 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
197 }; | 221 }; |
198 | 222 |
199 void ZoomableMediaImpl::ZoomIn() { | 223 void ZoomableMediaImpl::ZoomIn() { |
200 viewer_->ZoomIn(); | 224 viewer_->ZoomIn(); |
201 } | 225 } |
202 | 226 |
(...skipping 11 matching lines...) Expand all Loading... |
214 } | 238 } |
215 | 239 |
216 } // namespace examples | 240 } // namespace examples |
217 | 241 |
218 // static | 242 // static |
219 ApplicationDelegate* ApplicationDelegate::Create() { | 243 ApplicationDelegate* ApplicationDelegate::Create() { |
220 return new examples::PNGViewer; | 244 return new examples::PNGViewer; |
221 } | 245 } |
222 | 246 |
223 } // namespace mojo | 247 } // namespace mojo |
OLD | NEW |