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() : content_view_(NULL), zoom_percentage_(kDefaultZoomPercentage) {} |
117 virtual ~PNGViewer() {} | 119 virtual ~PNGViewer() {} |
118 | 120 |
119 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { | 121 void UpdateView(view_manager::Id node_id, const SkBitmap& bitmap) { |
120 bitmap_ = bitmap; | 122 bitmap_ = bitmap; |
121 zoom_percentage_ = kDefaultZoomPercentage; | 123 zoom_percentage_ = kDefaultZoomPercentage; |
122 DrawBitmap(); | 124 DrawBitmap(); |
123 } | 125 } |
124 | 126 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); | 159 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); |
158 return true; | 160 return true; |
159 } | 161 } |
160 | 162 |
161 // Overridden from view_manager::ViewManagerDelegate: | 163 // Overridden from view_manager::ViewManagerDelegate: |
162 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 164 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
163 view_manager::Node* root) OVERRIDE { | 165 view_manager::Node* root) OVERRIDE { |
164 content_view_ = view_manager::View::Create(view_manager); | 166 content_view_ = view_manager::View::Create(view_manager); |
165 root->SetActiveView(content_view_); | 167 root->SetActiveView(content_view_); |
166 content_view_->SetColor(SK_ColorGRAY); | 168 content_view_->SetColor(SK_ColorGRAY); |
169 root->AddObserver(this); | |
sky
2014/07/14 19:15:28
Same thing about removing observer.
hansmuller
2014/07/14 22:16:42
Done.
| |
167 if (!bitmap_.isNull()) | 170 if (!bitmap_.isNull()) |
168 DrawBitmap(); | 171 DrawBitmap(); |
169 } | 172 } |
170 virtual void OnViewManagerDisconnected( | 173 virtual void OnViewManagerDisconnected( |
171 view_manager::ViewManager* view_manager) OVERRIDE { | 174 view_manager::ViewManager* view_manager) OVERRIDE { |
172 base::MessageLoop::current()->Quit(); | 175 base::MessageLoop::current()->Quit(); |
173 } | 176 } |
174 | 177 |
175 void DrawBitmap() { | 178 void DrawBitmap() { |
176 if (!content_view_) | 179 if (!content_view_) |
177 return; | 180 return; |
178 | 181 |
179 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( | 182 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::CreatePlatformCanvas( |
180 content_view_->node()->bounds().width(), | 183 content_view_->node()->bounds().width(), |
181 content_view_->node()->bounds().height(), | 184 content_view_->node()->bounds().height(), |
182 true))); | 185 true))); |
183 canvas->drawColor(SK_ColorGRAY); | 186 canvas->drawColor(SK_ColorGRAY); |
184 SkPaint paint; | 187 SkPaint paint; |
185 SkScalar scale = | 188 SkScalar scale = |
186 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); | 189 SkFloatToScalar(zoom_percentage_ * 1.0f / kDefaultZoomPercentage); |
187 canvas->scale(scale, scale); | 190 canvas->scale(scale, scale); |
188 canvas->drawBitmap(bitmap_, 0, 0, &paint); | 191 canvas->drawBitmap(bitmap_, 0, 0, &paint); |
189 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); | 192 content_view_->SetContents(skia::GetTopDevice(*canvas)->accessBitmap(true)); |
190 } | 193 } |
191 | 194 |
195 virtual void OnNodeBoundsChanged(view_manager::Node* node, | |
196 const gfx::Rect&, | |
197 const gfx::Rect&) OVERRIDE { | |
198 DrawBitmap(); | |
199 } | |
200 | |
192 view_manager::View* content_view_; | 201 view_manager::View* content_view_; |
193 SkBitmap bitmap_; | 202 SkBitmap bitmap_; |
194 uint16_t zoom_percentage_; | 203 uint16_t zoom_percentage_; |
195 | 204 |
196 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | 205 DISALLOW_COPY_AND_ASSIGN(PNGViewer); |
197 }; | 206 }; |
198 | 207 |
199 void ZoomableMediaImpl::ZoomIn() { | 208 void ZoomableMediaImpl::ZoomIn() { |
200 viewer_->ZoomIn(); | 209 viewer_->ZoomIn(); |
201 } | 210 } |
(...skipping 12 matching lines...) Expand all Loading... | |
214 } | 223 } |
215 | 224 |
216 } // namespace examples | 225 } // namespace examples |
217 | 226 |
218 // static | 227 // static |
219 ApplicationDelegate* ApplicationDelegate::Create() { | 228 ApplicationDelegate* ApplicationDelegate::Create() { |
220 return new examples::PNGViewer; | 229 return new examples::PNGViewer; |
221 } | 230 } |
222 | 231 |
223 } // namespace mojo | 232 } // namespace mojo |
OLD | NEW |