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/lib/view_manager_client_impl.h" | 9 #include "components/view_manager/public/cpp/lib/view_manager_client_impl.h" |
10 #include "components/view_manager/public/cpp/types.h" | 10 #include "components/view_manager/public/cpp/types.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 next_resource_id_(1u), | 65 next_resource_id_(1u), |
66 id_namespace_(0u), | 66 id_namespace_(0u), |
67 local_id_(0u), | 67 local_id_(0u), |
68 returner_binding_(this) { | 68 returner_binding_(this) { |
69 } | 69 } |
70 ~BitmapUploader() override { | 70 ~BitmapUploader() override { |
71 MojoGLES2DestroyContext(gles2_context_); | 71 MojoGLES2DestroyContext(gles2_context_); |
72 } | 72 } |
73 | 73 |
74 void Init(mojo::Shell* shell) { | 74 void Init(mojo::Shell* shell) { |
75 mojo::ServiceProviderPtr surfaces_service_provider; | 75 view_->RequestCompositorFrameReceiver(GetProxy(&cc_frame_receiver_)); |
76 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
77 request->url = mojo::String::From("mojo:view_manager"); | |
78 shell->ConnectToApplication(request.Pass(), | |
79 mojo::GetProxy(&surfaces_service_provider), | |
80 nullptr, nullptr); | |
81 ConnectToService(surfaces_service_provider.get(), &surface_); | |
82 surface_->GetIdNamespace( | |
83 base::Bind(&BitmapUploader::SetIdNamespace, base::Unretained(this))); | |
84 mojo::ResourceReturnerPtr returner_ptr; | |
85 returner_binding_.Bind(GetProxy(&returner_ptr)); | |
86 surface_->SetResourceReturner(returner_ptr.Pass()); | |
87 | 76 |
88 mojo::ServiceProviderPtr gpu_service_provider; | 77 mojo::ServiceProviderPtr gpu_service_provider; |
89 mojo::URLRequestPtr request2(mojo::URLRequest::New()); | 78 mojo::URLRequestPtr request2(mojo::URLRequest::New()); |
90 request2->url = mojo::String::From("mojo:view_manager"); | 79 request2->url = mojo::String::From("mojo:view_manager"); |
91 shell->ConnectToApplication(request2.Pass(), | 80 shell->ConnectToApplication(request2.Pass(), |
92 mojo::GetProxy(&gpu_service_provider), nullptr, | 81 mojo::GetProxy(&gpu_service_provider), nullptr, |
93 nullptr); | 82 nullptr); |
94 ConnectToService(gpu_service_provider.get(), &gpu_service_); | 83 ConnectToService(gpu_service_provider.get(), &gpu_service_); |
95 | 84 |
96 mojo::CommandBufferPtr gles2_client; | 85 mojo::CommandBufferPtr gles2_client; |
97 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); | 86 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); |
98 gles2_context_ = MojoGLES2CreateContext( | 87 gles2_context_ = MojoGLES2CreateContext( |
99 gles2_client.PassInterface().PassHandle().release().value(), | 88 gles2_client.PassInterface().PassHandle().release().value(), |
100 &LostContext, NULL, mojo::Environment::GetDefaultAsyncWaiter()); | 89 &LostContext, NULL, mojo::Environment::GetDefaultAsyncWaiter()); |
101 MojoGLES2MakeCurrent(gles2_context_); | 90 MojoGLES2MakeCurrent(gles2_context_); |
102 } | 91 } |
103 | 92 |
104 // Sets the color which is RGBA. | 93 // Sets the color which is RGBA. |
105 void SetColor(uint32_t color) { | 94 void SetColor(uint32_t color) { |
106 if (color_ == color) | 95 if (color_ == color) |
107 return; | 96 return; |
108 color_ = color; | 97 color_ = color; |
109 if (surface_) | 98 if (cc_frame_receiver_) |
110 Upload(); | 99 Upload(); |
111 } | 100 } |
112 | 101 |
113 enum Format { | 102 enum Format { |
114 RGBA, // Pixel layout on Android. | 103 RGBA, // Pixel layout on Android. |
115 BGRA, // Pixel layout everywhere else. | 104 BGRA, // Pixel layout everywhere else. |
116 }; | 105 }; |
117 | 106 |
118 // Sets a bitmap. | 107 // Sets a bitmap. |
119 void SetBitmap(int width, | 108 void SetBitmap(int width, |
120 int height, | 109 int height, |
121 scoped_ptr<std::vector<unsigned char>> data, | 110 scoped_ptr<std::vector<unsigned char>> data, |
122 Format format) { | 111 Format format) { |
123 width_ = width; | 112 width_ = width; |
124 height_ = height; | 113 height_ = height; |
125 bitmap_ = data.Pass(); | 114 bitmap_ = data.Pass(); |
126 format_ = format; | 115 format_ = format; |
127 if (surface_) | 116 if (cc_frame_receiver_) |
128 Upload(); | 117 Upload(); |
129 } | 118 } |
130 | 119 |
131 private: | 120 private: |
132 void Upload() { | 121 void Upload() { |
133 mojo::Size size; | 122 gfx::Rect bounds(view_->bounds().To<gfx::Rect>()); |
134 size.width = view_->bounds().width; | |
135 size.height = view_->bounds().height; | |
136 if (!size.width || !size.height) { | |
137 view_->SetSurfaceId(mojo::SurfaceId::New()); | |
138 return; | |
139 } | |
140 | |
141 if (id_namespace_ == 0u) // Can't generate a qualified ID yet. | |
142 return; | |
143 | |
144 if (size != surface_size_) { | |
145 if (local_id_ != 0u) { | |
146 surface_->DestroySurface(local_id_); | |
147 } | |
148 local_id_++; | |
149 surface_->CreateSurface(local_id_); | |
150 surface_size_ = size; | |
151 auto qualified_id = mojo::SurfaceId::New(); | |
152 qualified_id->id_namespace = id_namespace_; | |
153 qualified_id->local = local_id_; | |
154 view_->SetSurfaceId(qualified_id.Pass()); | |
155 } | |
156 | |
157 gfx::Rect bounds(size.width, size.height); | |
158 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); | 123 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); |
159 mojo::CompositorFramePtr frame = mojo::CompositorFrame::New(); | 124 mojo::CompositorFramePtr frame = mojo::CompositorFrame::New(); |
160 frame->resources.resize(0u); | 125 frame->resources.resize(0u); |
161 | 126 |
162 pass->quads.resize(0u); | 127 pass->quads.resize(0u); |
163 pass->shared_quad_states.push_back( | 128 pass->shared_quad_states.push_back( |
164 mojo::CreateDefaultSQS(size.To<gfx::Size>())); | 129 mojo::CreateDefaultSQS(bounds.size())); |
165 | 130 |
166 MojoGLES2MakeCurrent(gles2_context_); | 131 MojoGLES2MakeCurrent(gles2_context_); |
167 if (bitmap_.get()) { | 132 if (bitmap_.get()) { |
168 mojo::Size bitmap_size; | 133 mojo::Size bitmap_size; |
169 bitmap_size.width = width_; | 134 bitmap_size.width = width_; |
170 bitmap_size.height = height_; | 135 bitmap_size.height = height_; |
171 GLuint texture_id = BindTextureForSize(bitmap_size); | 136 GLuint texture_id = BindTextureForSize(bitmap_size); |
172 glTexSubImage2D(GL_TEXTURE_2D, | 137 glTexSubImage2D(GL_TEXTURE_2D, |
173 0, | 138 0, |
174 0, | 139 0, |
(...skipping 23 matching lines...) Expand all Loading... | |
198 mailbox_holder->texture_target = GL_TEXTURE_2D; | 163 mailbox_holder->texture_target = GL_TEXTURE_2D; |
199 mailbox_holder->sync_point = sync_point; | 164 mailbox_holder->sync_point = sync_point; |
200 resource->mailbox_holder = mailbox_holder.Pass(); | 165 resource->mailbox_holder = mailbox_holder.Pass(); |
201 resource->is_repeated = false; | 166 resource->is_repeated = false; |
202 resource->is_software = false; | 167 resource->is_software = false; |
203 | 168 |
204 mojo::QuadPtr quad = mojo::Quad::New(); | 169 mojo::QuadPtr quad = mojo::Quad::New(); |
205 quad->material = mojo::MATERIAL_TEXTURE_CONTENT; | 170 quad->material = mojo::MATERIAL_TEXTURE_CONTENT; |
206 | 171 |
207 mojo::RectPtr rect = mojo::Rect::New(); | 172 mojo::RectPtr rect = mojo::Rect::New(); |
208 if (width_ <= size.width && height_ <= size.height) { | 173 if (width_ <= bounds.width() && height_ <= bounds.height()) { |
209 rect->width = width_; | 174 rect->width = width_; |
210 rect->height = height_; | 175 rect->height = height_; |
211 } else { | 176 } else { |
212 // The source bitmap is larger than the viewport. Resize it while | 177 // The source bitmap is larger than the viewport. Resize it while |
213 // maintaining the aspect ratio. | 178 // maintaining the aspect ratio. |
214 float width_ratio = static_cast<float>(width_) / size.width; | 179 float width_ratio = static_cast<float>(width_) / bounds.width(); |
rjkroege
2015/08/21 18:54:45
you have tested this in high DIP mode?
Fady Samuel
2015/08/21 21:31:10
This code worked in the past on android, and this
rjkroege
2015/08/21 22:58:00
you are making a presumption that bounds and size
Fady Samuel
2015/08/21 23:15:26
There is no unit conversion above.
| |
215 float height_ratio = static_cast<float>(height_) / size.height; | 180 float height_ratio = static_cast<float>(height_) / bounds.height(); |
216 if (width_ratio > height_ratio) { | 181 if (width_ratio > height_ratio) { |
217 rect->width = size.width; | 182 rect->width = bounds.width(); |
218 rect->height = height_ / width_ratio; | 183 rect->height = height_ / width_ratio; |
219 } else { | 184 } else { |
220 rect->height = size.height; | 185 rect->height = bounds.height(); |
221 rect->width = width_ / height_ratio; | 186 rect->width = width_ / height_ratio; |
222 } | 187 } |
223 } | 188 } |
224 quad->rect = rect.Clone(); | 189 quad->rect = rect.Clone(); |
225 quad->opaque_rect = rect.Clone(); | 190 quad->opaque_rect = rect.Clone(); |
226 quad->visible_rect = rect.Clone(); | 191 quad->visible_rect = rect.Clone(); |
227 quad->needs_blending = true; | 192 quad->needs_blending = true; |
228 quad->shared_quad_state_index = 0u; | 193 quad->shared_quad_state_index = 0u; |
229 | 194 |
230 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); | 195 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); |
(...skipping 28 matching lines...) Expand all Loading... | |
259 color_state->color = mojo::Color::New(); | 224 color_state->color = mojo::Color::New(); |
260 color_state->color->rgba = color_; | 225 color_state->color->rgba = color_; |
261 color_state->force_anti_aliasing_off = false; | 226 color_state->force_anti_aliasing_off = false; |
262 | 227 |
263 quad->solid_color_quad_state = color_state.Pass(); | 228 quad->solid_color_quad_state = color_state.Pass(); |
264 pass->quads.push_back(quad.Pass()); | 229 pass->quads.push_back(quad.Pass()); |
265 } | 230 } |
266 | 231 |
267 frame->passes.push_back(pass.Pass()); | 232 frame->passes.push_back(pass.Pass()); |
268 | 233 |
269 surface_->SubmitFrame(local_id_, frame.Pass(), mojo::Closure()); | 234 cc_frame_receiver_->SubmitCompositorFrame(frame.Pass(), mojo::Closure()); |
rjkroege
2015/08/19 23:45:44
replacing this mojo::Closure() here with a real ca
Fady Samuel
2015/08/20 16:22:44
Added TODO.
| |
270 } | 235 } |
271 | 236 |
272 uint32_t BindTextureForSize(const mojo::Size size) { | 237 uint32_t BindTextureForSize(const mojo::Size size) { |
273 // TODO(jamesr): Recycle textures. | 238 // TODO(jamesr): Recycle textures. |
274 GLuint texture = 0u; | 239 GLuint texture = 0u; |
275 glGenTextures(1, &texture); | 240 glGenTextures(1, &texture); |
276 glBindTexture(GL_TEXTURE_2D, texture); | 241 glBindTexture(GL_TEXTURE_2D, texture); |
277 glTexImage2D(GL_TEXTURE_2D, | 242 glTexImage2D(GL_TEXTURE_2D, |
278 0, | 243 0, |
279 TextureFormat(), | 244 TextureFormat(), |
(...skipping 28 matching lines...) Expand all Loading... | |
308 glWaitSyncPointCHROMIUM(resource->sync_point); | 273 glWaitSyncPointCHROMIUM(resource->sync_point); |
309 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 274 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
310 DCHECK_NE(0u, texture_id); | 275 DCHECK_NE(0u, texture_id); |
311 resource_to_texture_id_map_.erase(resource->id); | 276 resource_to_texture_id_map_.erase(resource->id); |
312 glDeleteTextures(1, &texture_id); | 277 glDeleteTextures(1, &texture_id); |
313 } | 278 } |
314 } | 279 } |
315 | 280 |
316 mojo::View* view_; | 281 mojo::View* view_; |
317 mojo::GpuPtr gpu_service_; | 282 mojo::GpuPtr gpu_service_; |
283 mojo::CompositorFrameReceiverPtr cc_frame_receiver_; | |
318 MojoGLES2Context gles2_context_; | 284 MojoGLES2Context gles2_context_; |
319 | 285 |
320 mojo::Size size_; | 286 mojo::Size size_; |
321 uint32_t color_; | 287 uint32_t color_; |
322 int width_; | 288 int width_; |
323 int height_; | 289 int height_; |
324 Format format_; | 290 Format format_; |
325 scoped_ptr<std::vector<unsigned char>> bitmap_; | 291 scoped_ptr<std::vector<unsigned char>> bitmap_; |
326 mojo::SurfacePtr surface_; | |
327 mojo::Size surface_size_; | |
328 uint32_t next_resource_id_; | 292 uint32_t next_resource_id_; |
329 uint32_t id_namespace_; | 293 uint32_t id_namespace_; |
330 uint32_t local_id_; | 294 uint32_t local_id_; |
331 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; | 295 base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_; |
332 mojo::Binding<mojo::ResourceReturner> returner_binding_; | 296 mojo::Binding<mojo::ResourceReturner> returner_binding_; |
333 | 297 |
334 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); | 298 DISALLOW_COPY_AND_ASSIGN(BitmapUploader); |
335 }; | 299 }; |
336 | 300 |
337 class EmbedderData { | 301 class EmbedderData { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 DISALLOW_COPY_AND_ASSIGN(PDFViewer); | 498 DISALLOW_COPY_AND_ASSIGN(PDFViewer); |
535 }; | 499 }; |
536 | 500 |
537 } // namespace | 501 } // namespace |
538 } // namespace pdf_viewer | 502 } // namespace pdf_viewer |
539 | 503 |
540 MojoResult MojoMain(MojoHandle application_request) { | 504 MojoResult MojoMain(MojoHandle application_request) { |
541 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); | 505 mojo::ApplicationRunner runner(new pdf_viewer::PDFViewer()); |
542 return runner.Run(application_request); | 506 return runner.Run(application_request); |
543 } | 507 } |
OLD | NEW |