OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/view_manager/public/cpp/surface.h" |
9 #include "components/view_manager/public/cpp/types.h" | 10 #include "components/view_manager/public/cpp/types.h" |
10 #include "components/view_manager/public/cpp/view.h" | 11 #include "components/view_manager/public/cpp/view.h" |
11 #include "components/view_manager/public/cpp/view_observer.h" | 12 #include "components/view_manager/public/cpp/view_observer.h" |
12 #include "components/view_manager/public/cpp/view_tree_connection.h" | 13 #include "components/view_manager/public/cpp/view_tree_connection.h" |
13 #include "components/view_manager/public/cpp/view_tree_delegate.h" | 14 #include "components/view_manager/public/cpp/view_tree_delegate.h" |
| 15 #include "components/view_manager/public/interfaces/compositor_frame.mojom.h" |
14 #include "components/view_manager/public/interfaces/gpu.mojom.h" | 16 #include "components/view_manager/public/interfaces/gpu.mojom.h" |
15 #include "components/view_manager/public/interfaces/surface_id.mojom.h" | 17 #include "components/view_manager/public/interfaces/surface_id.mojom.h" |
16 #include "components/view_manager/public/interfaces/surfaces.mojom.h" | |
17 #include "gpu/GLES2/gl2chromium.h" | 18 #include "gpu/GLES2/gl2chromium.h" |
18 #include "gpu/GLES2/gl2extchromium.h" | 19 #include "gpu/GLES2/gl2extchromium.h" |
19 #include "mojo/application/public/cpp/application_connection.h" | 20 #include "mojo/application/public/cpp/application_connection.h" |
20 #include "mojo/application/public/cpp/application_delegate.h" | 21 #include "mojo/application/public/cpp/application_delegate.h" |
21 #include "mojo/application/public/cpp/application_impl.h" | 22 #include "mojo/application/public/cpp/application_impl.h" |
22 #include "mojo/application/public/cpp/application_runner.h" | 23 #include "mojo/application/public/cpp/application_runner.h" |
23 #include "mojo/application/public/cpp/connect.h" | 24 #include "mojo/application/public/cpp/connect.h" |
24 #include "mojo/application/public/cpp/content_handler_factory.h" | 25 #include "mojo/application/public/cpp/content_handler_factory.h" |
25 #include "mojo/application/public/cpp/interface_factory_impl.h" | 26 #include "mojo/application/public/cpp/interface_factory_impl.h" |
26 #include "mojo/application/public/cpp/service_provider_impl.h" | 27 #include "mojo/application/public/cpp/service_provider_impl.h" |
27 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 28 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
28 #include "mojo/application/public/interfaces/shell.mojom.h" | 29 #include "mojo/application/public/interfaces/shell.mojom.h" |
29 #include "mojo/common/data_pipe_utils.h" | 30 #include "mojo/common/data_pipe_utils.h" |
30 #include "mojo/converters/geometry/geometry_type_converters.h" | 31 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 32 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
31 #include "mojo/converters/surfaces/surfaces_utils.h" | 33 #include "mojo/converters/surfaces/surfaces_utils.h" |
32 #include "mojo/public/c/gles2/gles2.h" | 34 #include "mojo/public/c/gles2/gles2.h" |
33 #include "mojo/public/c/system/main.h" | 35 #include "mojo/public/c/system/main.h" |
34 #include "mojo/public/cpp/bindings/binding.h" | 36 #include "mojo/public/cpp/bindings/binding.h" |
35 #include "third_party/pdfium/public/fpdf_ext.h" | 37 #include "third_party/pdfium/public/fpdf_ext.h" |
36 #include "third_party/pdfium/public/fpdfview.h" | 38 #include "third_party/pdfium/public/fpdfview.h" |
37 #include "ui/gfx/geometry/rect.h" | 39 #include "ui/gfx/geometry/rect.h" |
38 #include "ui/mojo/events/input_events.mojom.h" | 40 #include "ui/mojo/events/input_events.mojom.h" |
39 #include "ui/mojo/events/input_key_codes.mojom.h" | 41 #include "ui/mojo/events/input_key_codes.mojom.h" |
40 #include "ui/mojo/geometry/geometry.mojom.h" | 42 #include "ui/mojo/geometry/geometry.mojom.h" |
41 #include "ui/mojo/geometry/geometry_util.h" | 43 #include "ui/mojo/geometry/geometry_util.h" |
42 #include "v8/include/v8.h" | 44 #include "v8/include/v8.h" |
43 | 45 |
44 const uint32_t g_background_color = 0xFF888888; | 46 const uint32_t g_background_color = 0xFF888888; |
45 const uint32_t g_transparent_color = 0x00000000; | 47 const uint32_t g_transparent_color = 0x00000000; |
46 | 48 |
47 namespace pdf_viewer { | 49 namespace pdf_viewer { |
48 namespace { | 50 namespace { |
49 | 51 |
50 void LostContext(void*) { | 52 void LostContext(void*) { |
51 DCHECK(false); | 53 DCHECK(false); |
52 } | 54 } |
53 | 55 |
54 // BitmapUploader is useful if you want to draw a bitmap or color in a View. | 56 // BitmapUploader is useful if you want to draw a bitmap or color in a View. |
55 class BitmapUploader : public mojo::ResourceReturner { | 57 class BitmapUploader : public mojo::SurfaceClient { |
56 public: | 58 public: |
57 explicit BitmapUploader(mojo::View* view) | 59 explicit BitmapUploader(mojo::View* view) |
58 : view_(view), | 60 : view_(view), |
59 color_(g_transparent_color), | 61 color_(g_transparent_color), |
60 width_(0), | 62 width_(0), |
61 height_(0), | 63 height_(0), |
62 format_(BGRA), | 64 format_(BGRA), |
63 next_resource_id_(1u), | 65 next_resource_id_(1u), |
64 id_namespace_(0u), | 66 id_namespace_(0u), |
65 local_id_(0u), | 67 local_id_(0u), |
66 returner_binding_(this) { | 68 returner_binding_(this) { |
67 } | 69 } |
68 ~BitmapUploader() override { | 70 ~BitmapUploader() override { |
69 MojoGLES2DestroyContext(gles2_context_); | 71 MojoGLES2DestroyContext(gles2_context_); |
70 } | 72 } |
71 | 73 |
72 void Init(mojo::Shell* shell) { | 74 void Init(mojo::Shell* shell) { |
73 mojo::ServiceProviderPtr surfaces_service_provider; | 75 surface_ = view_->RequestSurface(); |
74 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
75 request->url = mojo::String::From("mojo:view_manager"); | |
76 shell->ConnectToApplication(request.Pass(), | |
77 mojo::GetProxy(&surfaces_service_provider), | |
78 nullptr, nullptr); | |
79 ConnectToService(surfaces_service_provider.get(), &surface_); | |
80 surface_->GetIdNamespace( | |
81 base::Bind(&BitmapUploader::SetIdNamespace, base::Unretained(this))); | |
82 mojo::ResourceReturnerPtr returner_ptr; | |
83 returner_binding_.Bind(GetProxy(&returner_ptr)); | |
84 surface_->SetResourceReturner(returner_ptr.Pass()); | |
85 | 76 |
86 mojo::ServiceProviderPtr gpu_service_provider; | 77 mojo::ServiceProviderPtr gpu_service_provider; |
87 mojo::URLRequestPtr request2(mojo::URLRequest::New()); | 78 mojo::URLRequestPtr request2(mojo::URLRequest::New()); |
88 request2->url = mojo::String::From("mojo:view_manager"); | 79 request2->url = mojo::String::From("mojo:view_manager"); |
89 shell->ConnectToApplication(request2.Pass(), | 80 shell->ConnectToApplication(request2.Pass(), |
90 mojo::GetProxy(&gpu_service_provider), nullptr, | 81 mojo::GetProxy(&gpu_service_provider), nullptr, |
91 nullptr); | 82 nullptr); |
92 ConnectToService(gpu_service_provider.get(), &gpu_service_); | 83 ConnectToService(gpu_service_provider.get(), &gpu_service_); |
93 | 84 |
94 mojo::CommandBufferPtr gles2_client; | 85 mojo::CommandBufferPtr gles2_client; |
(...skipping 26 matching lines...) Expand all Loading... |
121 width_ = width; | 112 width_ = width; |
122 height_ = height; | 113 height_ = height; |
123 bitmap_ = data.Pass(); | 114 bitmap_ = data.Pass(); |
124 format_ = format; | 115 format_ = format; |
125 if (surface_) | 116 if (surface_) |
126 Upload(); | 117 Upload(); |
127 } | 118 } |
128 | 119 |
129 private: | 120 private: |
130 void Upload() { | 121 void Upload() { |
131 mojo::Size size; | 122 gfx::Rect bounds(view_->bounds().To<gfx::Rect>()); |
132 size.width = view_->bounds().width; | |
133 size.height = view_->bounds().height; | |
134 if (!size.width || !size.height) { | |
135 view_->SetSurfaceId(mojo::SurfaceId::New()); | |
136 return; | |
137 } | |
138 | |
139 if (id_namespace_ == 0u) // Can't generate a qualified ID yet. | |
140 return; | |
141 | |
142 if (size != surface_size_) { | |
143 if (local_id_ != 0u) { | |
144 surface_->DestroySurface(local_id_); | |
145 } | |
146 local_id_++; | |
147 surface_->CreateSurface(local_id_); | |
148 surface_size_ = size; | |
149 auto qualified_id = mojo::SurfaceId::New(); | |
150 qualified_id->id_namespace = id_namespace_; | |
151 qualified_id->local = local_id_; | |
152 view_->SetSurfaceId(qualified_id.Pass()); | |
153 } | |
154 | |
155 gfx::Rect bounds(size.width, size.height); | |
156 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); | 123 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); |
157 mojo::CompositorFramePtr frame = mojo::CompositorFrame::New(); | 124 mojo::CompositorFramePtr frame = mojo::CompositorFrame::New(); |
158 frame->resources.resize(0u); | 125 frame->resources.resize(0u); |
159 | 126 |
160 pass->quads.resize(0u); | 127 pass->quads.resize(0u); |
161 pass->shared_quad_states.push_back( | 128 pass->shared_quad_states.push_back( |
162 mojo::CreateDefaultSQS(size.To<gfx::Size>())); | 129 mojo::CreateDefaultSQS(bounds.size())); |
163 | 130 |
164 MojoGLES2MakeCurrent(gles2_context_); | 131 MojoGLES2MakeCurrent(gles2_context_); |
165 if (bitmap_.get()) { | 132 if (bitmap_.get()) { |
166 mojo::Size bitmap_size; | 133 mojo::Size bitmap_size; |
167 bitmap_size.width = width_; | 134 bitmap_size.width = width_; |
168 bitmap_size.height = height_; | 135 bitmap_size.height = height_; |
169 GLuint texture_id = BindTextureForSize(bitmap_size); | 136 GLuint texture_id = BindTextureForSize(bitmap_size); |
170 glTexSubImage2D(GL_TEXTURE_2D, | 137 glTexSubImage2D(GL_TEXTURE_2D, |
171 0, | 138 0, |
172 0, | 139 0, |
(...skipping 23 matching lines...) Expand all Loading... |
196 mailbox_holder->texture_target = GL_TEXTURE_2D; | 163 mailbox_holder->texture_target = GL_TEXTURE_2D; |
197 mailbox_holder->sync_point = sync_point; | 164 mailbox_holder->sync_point = sync_point; |
198 resource->mailbox_holder = mailbox_holder.Pass(); | 165 resource->mailbox_holder = mailbox_holder.Pass(); |
199 resource->is_repeated = false; | 166 resource->is_repeated = false; |
200 resource->is_software = false; | 167 resource->is_software = false; |
201 | 168 |
202 mojo::QuadPtr quad = mojo::Quad::New(); | 169 mojo::QuadPtr quad = mojo::Quad::New(); |
203 quad->material = mojo::MATERIAL_TEXTURE_CONTENT; | 170 quad->material = mojo::MATERIAL_TEXTURE_CONTENT; |
204 | 171 |
205 mojo::RectPtr rect = mojo::Rect::New(); | 172 mojo::RectPtr rect = mojo::Rect::New(); |
206 if (width_ <= size.width && height_ <= size.height) { | 173 if (width_ <= bounds.width() && height_ <= bounds.height()) { |
207 rect->width = width_; | 174 rect->width = width_; |
208 rect->height = height_; | 175 rect->height = height_; |
209 } else { | 176 } else { |
210 // The source bitmap is larger than the viewport. Resize it while | 177 // The source bitmap is larger than the viewport. Resize it while |
211 // maintaining the aspect ratio. | 178 // maintaining the aspect ratio. |
212 float width_ratio = static_cast<float>(width_) / size.width; | 179 float width_ratio = static_cast<float>(width_) / bounds.width(); |
213 float height_ratio = static_cast<float>(height_) / size.height; | 180 float height_ratio = static_cast<float>(height_) / bounds.height(); |
214 if (width_ratio > height_ratio) { | 181 if (width_ratio > height_ratio) { |
215 rect->width = size.width; | 182 rect->width = bounds.width(); |
216 rect->height = height_ / width_ratio; | 183 rect->height = height_ / width_ratio; |
217 } else { | 184 } else { |
218 rect->height = size.height; | 185 rect->height = bounds.height(); |
219 rect->width = width_ / height_ratio; | 186 rect->width = width_ / height_ratio; |
220 } | 187 } |
221 } | 188 } |
222 quad->rect = rect.Clone(); | 189 quad->rect = rect.Clone(); |
223 quad->opaque_rect = rect.Clone(); | 190 quad->opaque_rect = rect.Clone(); |
224 quad->visible_rect = rect.Clone(); | 191 quad->visible_rect = rect.Clone(); |
225 quad->needs_blending = true; | 192 quad->needs_blending = true; |
226 quad->shared_quad_state_index = 0u; | 193 quad->shared_quad_state_index = 0u; |
227 | 194 |
228 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); | 195 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); |
(...skipping 28 matching lines...) Expand all Loading... |
257 color_state->color = mojo::Color::New(); | 224 color_state->color = mojo::Color::New(); |
258 color_state->color->rgba = color_; | 225 color_state->color->rgba = color_; |
259 color_state->force_anti_aliasing_off = false; | 226 color_state->force_anti_aliasing_off = false; |
260 | 227 |
261 quad->solid_color_quad_state = color_state.Pass(); | 228 quad->solid_color_quad_state = color_state.Pass(); |
262 pass->quads.push_back(quad.Pass()); | 229 pass->quads.push_back(quad.Pass()); |
263 } | 230 } |
264 | 231 |
265 frame->passes.push_back(pass.Pass()); | 232 frame->passes.push_back(pass.Pass()); |
266 | 233 |
267 surface_->SubmitCompositorFrame(local_id_, frame.Pass(), mojo::Closure()); | 234 // TODO(rjkroege, fsamuel): We should throttle frames. |
| 235 surface_->SubmitCompositorFrame(frame.Pass()); |
268 } | 236 } |
269 | 237 |
270 uint32_t BindTextureForSize(const mojo::Size size) { | 238 uint32_t BindTextureForSize(const mojo::Size size) { |
271 // TODO(jamesr): Recycle textures. | 239 // TODO(jamesr): Recycle textures. |
272 GLuint texture = 0u; | 240 GLuint texture = 0u; |
273 glGenTextures(1, &texture); | 241 glGenTextures(1, &texture); |
274 glBindTexture(GL_TEXTURE_2D, texture); | 242 glBindTexture(GL_TEXTURE_2D, texture); |
275 glTexImage2D(GL_TEXTURE_2D, | 243 glTexImage2D(GL_TEXTURE_2D, |
276 0, | 244 0, |
277 TextureFormat(), | 245 TextureFormat(), |
(...skipping 10 matching lines...) Expand all Loading... |
288 uint32_t TextureFormat() { | 256 uint32_t TextureFormat() { |
289 return format_ == BGRA ? GL_BGRA_EXT : GL_RGBA; | 257 return format_ == BGRA ? GL_BGRA_EXT : GL_RGBA; |
290 } | 258 } |
291 | 259 |
292 void SetIdNamespace(uint32_t id_namespace) { | 260 void SetIdNamespace(uint32_t id_namespace) { |
293 id_namespace_ = id_namespace; | 261 id_namespace_ = id_namespace; |
294 if (color_ != g_transparent_color || bitmap_.get()) | 262 if (color_ != g_transparent_color || bitmap_.get()) |
295 Upload(); | 263 Upload(); |
296 } | 264 } |
297 | 265 |
298 // ResourceReturner implementation. | 266 // SurfaceClient implementation. |
299 void ReturnResources( | 267 void ReturnResources( |
300 mojo::Array<mojo::ReturnedResourcePtr> resources) override { | 268 mojo::Array<mojo::ReturnedResourcePtr> resources) override { |
301 MojoGLES2MakeCurrent(gles2_context_); | 269 MojoGLES2MakeCurrent(gles2_context_); |
302 // TODO(jamesr): Recycle. | 270 // TODO(jamesr): Recycle. |
303 for (size_t i = 0; i < resources.size(); ++i) { | 271 for (size_t i = 0; i < resources.size(); ++i) { |
304 mojo::ReturnedResourcePtr resource = resources[i].Pass(); | 272 mojo::ReturnedResourcePtr resource = resources[i].Pass(); |
305 DCHECK_EQ(1, resource->count); | 273 DCHECK_EQ(1, resource->count); |
306 glWaitSyncPointCHROMIUM(resource->sync_point); | 274 glWaitSyncPointCHROMIUM(resource->sync_point); |
307 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 275 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
308 DCHECK_NE(0u, texture_id); | 276 DCHECK_NE(0u, texture_id); |
309 resource_to_texture_id_map_.erase(resource->id); | 277 resource_to_texture_id_map_.erase(resource->id); |
310 glDeleteTextures(1, &texture_id); | 278 glDeleteTextures(1, &texture_id); |
311 } | 279 } |
312 } | 280 } |
313 | 281 |
314 mojo::View* view_; | 282 mojo::View* view_; |
315 mojo::GpuPtr gpu_service_; | 283 mojo::GpuPtr gpu_service_; |
| 284 scoped_ptr<uip::Surface> surface_; |
316 MojoGLES2Context gles2_context_; | 285 MojoGLES2Context gles2_context_; |
317 | 286 |
318 mojo::Size size_; | 287 mojo::Size size_; |
319 uint32_t color_; | 288 uint32_t color_; |
320 int width_; | 289 int width_; |
321 int height_; | 290 int height_; |
322 Format format_; | 291 Format format_; |
323 scoped_ptr<std::vector<unsigned char>> bitmap_; | 292 scoped_ptr<std::vector<unsigned char>> bitmap_; |
324 mojo::SurfacePtr surface_; | |
325 mojo::Size surface_size_; | |
326 uint32_t next_resource_id_; | 293 uint32_t next_resource_id_; |
327 uint32_t id_namespace_; | 294 uint32_t id_namespace_; |
328 uint32_t local_id_; | 295 uint32_t local_id_; |
329 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; | 296 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; |
330 mojo::Binding<mojo::ResourceReturner> returner_binding_; | 297 mojo::Binding<mojo::SurfaceClient> returner_binding_; |
331 | 298 |
332 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); | 299 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); |
333 }; | 300 }; |
334 | 301 |
335 class EmbedderData { | 302 class EmbedderData { |
336 public: | 303 public: |
337 EmbedderData(mojo::Shell* shell, mojo::View* root) : bitmap_uploader_(root) { | 304 EmbedderData(mojo::Shell* shell, mojo::View* root) : bitmap_uploader_(root) { |
338 bitmap_uploader_.Init(shell); | 305 bitmap_uploader_.Init(shell); |
339 bitmap_uploader_.SetColor(g_background_color); | 306 bitmap_uploader_.SetColor(g_background_color); |
340 } | 307 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 505 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
539 }; | 506 }; |
540 | 507 |
541 } // namespace | 508 } // namespace |
542 } // namespace pdf_viewer | 509 } // namespace pdf_viewer |
543 | 510 |
544 MojoResult MojoMain(MojoHandle application_request) { | 511 MojoResult MojoMain(MojoHandle application_request) { |
545 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); | 512 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); |
546 return runner.Run(application_request); | 513 return runner.Run(application_request); |
547 } | 514 } |
OLD | NEW |