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
3 // found in the LICENSE file.
6 #include "base/callback.h"
7 #include "base/containers/hash_tables.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "components/view_manager/public/cpp/lib/view_manager_client_impl.h"
10 #include "components/view_manager/public/cpp/types.h"
11 #include "components/view_manager/public/cpp/view.h"
12 #include "components/view_manager/public/cpp/view_manager.h"
13 #include "components/view_manager/public/cpp/view_manager_client_factory.h"
14 #include "components/view_manager/public/cpp/view_manager_delegate.h"
15 #include "components/view_manager/public/cpp/view_observer.h"
16 #include "components/view_manager/public/interfaces/gpu.mojom.h"
17 #include "components/view_manager/public/interfaces/surface_id.mojom.h"
18 #include "components/view_manager/public/interfaces/surfaces.mojom.h"
19 #include "gpu/GLES2/gl2chromium.h"
20 #include "gpu/GLES2/gl2extchromium.h"
21 #include "mojo/application/public/cpp/application_connection.h"
22 #include "mojo/application/public/cpp/application_delegate.h"
23 #include "mojo/application/public/cpp/application_impl.h"
24 #include "mojo/application/public/cpp/application_runner.h"
25 #include "mojo/application/public/cpp/connect.h"
26 #include "mojo/application/public/cpp/content_handler_factory.h"
27 #include "mojo/application/public/cpp/interface_factory_impl.h"
28 #include "mojo/application/public/cpp/service_provider_impl.h"
29 #include "mojo/application/public/interfaces/content_handler.mojom.h"
30 #include "mojo/application/public/interfaces/shell.mojom.h"
31 #include "mojo/common/data_pipe_utils.h"
32 #include "mojo/converters/geometry/geometry_type_converters.h"
33 #include "mojo/converters/surfaces/surfaces_utils.h"
34 #include "mojo/public/c/gles2/gles2.h"
35 #include "mojo/public/c/system/main.h"
36 #include "mojo/public/cpp/bindings/binding.h"
37 #include "third_party/pdfium/public/fpdf_ext.h"
38 #include "third_party/pdfium/public/fpdfview.h"
39 #include "ui/gfx/geometry/rect.h"
40 #include "ui/mojo/events/input_events.mojom.h"
41 #include "ui/mojo/events/input_key_codes.mojom.h"
42 #include "ui/mojo/geometry/geometry.mojom.h"
43 #include "ui/mojo/geometry/geometry_util.h"
44 #include "v8/include/v8.h"
46 const uint32_t g_background_color
= 0xFF888888;
47 const uint32_t g_transparent_color
= 0x00000000;
49 namespace pdf_viewer
{
52 void LostContext(void*) {
56 // BitmapUploader is useful if you want to draw a bitmap or color in a View.
57 class BitmapUploader
: public mojo::ResourceReturner
{
59 explicit BitmapUploader(mojo::View
* view
)
61 color_(g_transparent_color
),
65 next_resource_id_(1u),
68 returner_binding_(this) {
70 ~BitmapUploader() override
{
71 MojoGLES2DestroyContext(gles2_context_
);
74 void Init(mojo::Shell
* shell
) {
75 mojo::ServiceProviderPtr surfaces_service_provider
;
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
),
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());
88 mojo::ServiceProviderPtr gpu_service_provider
;
89 mojo::URLRequestPtr
request2(mojo::URLRequest::New());
90 request2
->url
= mojo::String::From("mojo:view_manager");
91 shell
->ConnectToApplication(request2
.Pass(),
92 mojo::GetProxy(&gpu_service_provider
), nullptr,
94 ConnectToService(gpu_service_provider
.get(), &gpu_service_
);
96 mojo::CommandBufferPtr gles2_client
;
97 gpu_service_
->CreateOffscreenGLES2Context(GetProxy(&gles2_client
));
98 gles2_context_
= MojoGLES2CreateContext(
99 gles2_client
.PassInterface().PassHandle().release().value(),
100 &LostContext
, NULL
, mojo::Environment::GetDefaultAsyncWaiter());
101 MojoGLES2MakeCurrent(gles2_context_
);
104 // Sets the color which is RGBA.
105 void SetColor(uint32_t color
) {
114 RGBA
, // Pixel layout on Android.
115 BGRA
, // Pixel layout everywhere else.
119 void SetBitmap(int width
,
121 scoped_ptr
<std::vector
<unsigned char>> data
,
125 bitmap_
= data
.Pass();
134 size
.width
= view_
->bounds().width
;
135 size
.height
= view_
->bounds().height
;
136 if (!size
.width
|| !size
.height
) {
137 view_
->SetSurfaceId(mojo::SurfaceId::New());
141 if (id_namespace_
== 0u) // Can't generate a qualified ID yet.
144 if (size
!= surface_size_
) {
145 if (local_id_
!= 0u) {
146 surface_
->DestroySurface(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());
157 gfx::Rect
bounds(size
.width
, size
.height
);
158 mojo::PassPtr pass
= mojo::CreateDefaultPass(1, bounds
);
159 mojo::FramePtr frame
= mojo::Frame::New();
160 frame
->resources
.resize(0u);
162 pass
->quads
.resize(0u);
163 pass
->shared_quad_states
.push_back(
164 mojo::CreateDefaultSQS(size
.To
<gfx::Size
>()));
166 MojoGLES2MakeCurrent(gles2_context_
);
168 mojo::Size bitmap_size
;
169 bitmap_size
.width
= width_
;
170 bitmap_size
.height
= height_
;
171 GLuint texture_id
= BindTextureForSize(bitmap_size
);
172 glTexSubImage2D(GL_TEXTURE_2D
,
182 GLbyte mailbox
[GL_MAILBOX_SIZE_CHROMIUM
];
183 glGenMailboxCHROMIUM(mailbox
);
184 glProduceTextureCHROMIUM(GL_TEXTURE_2D
, mailbox
);
185 GLuint sync_point
= glInsertSyncPointCHROMIUM();
187 mojo::TransferableResourcePtr resource
=
188 mojo::TransferableResource::New();
189 resource
->id
= next_resource_id_
++;
190 resource_to_texture_id_map_
[resource
->id
] = texture_id
;
191 resource
->format
= mojo::RESOURCE_FORMAT_RGBA_8888
;
192 resource
->filter
= GL_LINEAR
;
193 resource
->size
= bitmap_size
.Clone();
194 mojo::MailboxHolderPtr mailbox_holder
= mojo::MailboxHolder::New();
195 mailbox_holder
->mailbox
= mojo::Mailbox::New();
196 for (int i
= 0; i
< GL_MAILBOX_SIZE_CHROMIUM
; ++i
)
197 mailbox_holder
->mailbox
->name
.push_back(mailbox
[i
]);
198 mailbox_holder
->texture_target
= GL_TEXTURE_2D
;
199 mailbox_holder
->sync_point
= sync_point
;
200 resource
->mailbox_holder
= mailbox_holder
.Pass();
201 resource
->is_repeated
= false;
202 resource
->is_software
= false;
204 mojo::QuadPtr quad
= mojo::Quad::New();
205 quad
->material
= mojo::MATERIAL_TEXTURE_CONTENT
;
207 mojo::RectPtr rect
= mojo::Rect::New();
208 if (width_
<= size
.width
&& height_
<= size
.height
) {
209 rect
->width
= width_
;
210 rect
->height
= height_
;
212 // The source bitmap is larger than the viewport. Resize it while
213 // maintaining the aspect ratio.
214 float width_ratio
= static_cast<float>(width_
) / size
.width
;
215 float height_ratio
= static_cast<float>(height_
) / size
.height
;
216 if (width_ratio
> height_ratio
) {
217 rect
->width
= size
.width
;
218 rect
->height
= height_
/ width_ratio
;
220 rect
->height
= size
.height
;
221 rect
->width
= width_
/ height_ratio
;
224 quad
->rect
= rect
.Clone();
225 quad
->opaque_rect
= rect
.Clone();
226 quad
->visible_rect
= rect
.Clone();
227 quad
->needs_blending
= true;
228 quad
->shared_quad_state_index
= 0u;
230 mojo::TextureQuadStatePtr texture_state
= mojo::TextureQuadState::New();
231 texture_state
->resource_id
= resource
->id
;
232 texture_state
->premultiplied_alpha
= true;
233 texture_state
->uv_top_left
= mojo::PointF::New();
234 texture_state
->uv_bottom_right
= mojo::PointF::New();
235 texture_state
->uv_bottom_right
->x
= 1.f
;
236 texture_state
->uv_bottom_right
->y
= 1.f
;
237 texture_state
->background_color
= mojo::Color::New();
238 texture_state
->background_color
->rgba
= g_transparent_color
;
239 for (int i
= 0; i
< 4; ++i
)
240 texture_state
->vertex_opacity
.push_back(1.f
);
241 texture_state
->y_flipped
= false;
243 frame
->resources
.push_back(resource
.Pass());
244 quad
->texture_quad_state
= texture_state
.Pass();
245 pass
->quads
.push_back(quad
.Pass());
248 if (color_
!= g_transparent_color
) {
249 mojo::QuadPtr quad
= mojo::Quad::New();
250 quad
->material
= mojo::MATERIAL_SOLID_COLOR
;
251 quad
->rect
= mojo::Rect::From(bounds
);
252 quad
->opaque_rect
= mojo::Rect::New();
253 quad
->visible_rect
= mojo::Rect::From(bounds
);
254 quad
->needs_blending
= true;
255 quad
->shared_quad_state_index
= 0u;
257 mojo::SolidColorQuadStatePtr color_state
=
258 mojo::SolidColorQuadState::New();
259 color_state
->color
= mojo::Color::New();
260 color_state
->color
->rgba
= color_
;
261 color_state
->force_anti_aliasing_off
= false;
263 quad
->solid_color_quad_state
= color_state
.Pass();
264 pass
->quads
.push_back(quad
.Pass());
267 frame
->passes
.push_back(pass
.Pass());
269 surface_
->SubmitFrame(local_id_
, frame
.Pass(), mojo::Closure());
272 uint32_t BindTextureForSize(const mojo::Size size
) {
273 // TODO(jamesr): Recycle textures.
275 glGenTextures(1, &texture
);
276 glBindTexture(GL_TEXTURE_2D
, texture
);
277 glTexImage2D(GL_TEXTURE_2D
,
286 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
290 uint32_t TextureFormat() {
291 return format_
== BGRA
? GL_BGRA_EXT
: GL_RGBA
;
294 void SetIdNamespace(uint32_t id_namespace
) {
295 id_namespace_
= id_namespace
;
296 if (color_
!= g_transparent_color
|| bitmap_
.get())
300 // ResourceReturner implementation.
301 void ReturnResources(
302 mojo::Array
<mojo::ReturnedResourcePtr
> resources
) override
{
303 MojoGLES2MakeCurrent(gles2_context_
);
304 // TODO(jamesr): Recycle.
305 for (size_t i
= 0; i
< resources
.size(); ++i
) {
306 mojo::ReturnedResourcePtr resource
= resources
[i
].Pass();
307 DCHECK_EQ(1, resource
->count
);
308 glWaitSyncPointCHROMIUM(resource
->sync_point
);
309 uint32_t texture_id
= resource_to_texture_id_map_
[resource
->id
];
310 DCHECK_NE(0u, texture_id
);
311 resource_to_texture_id_map_
.erase(resource
->id
);
312 glDeleteTextures(1, &texture_id
);
317 mojo::GpuPtr gpu_service_
;
318 MojoGLES2Context gles2_context_
;
325 scoped_ptr
<std::vector
<unsigned char>> bitmap_
;
326 mojo::SurfacePtr surface_
;
327 mojo::Size surface_size_
;
328 uint32_t next_resource_id_
;
329 uint32_t id_namespace_
;
331 base::hash_map
<uint32_t, uint32_t> resource_to_texture_id_map_
;
332 mojo::Binding
<mojo::ResourceReturner
> returner_binding_
;
334 DISALLOW_COPY_AND_ASSIGN(BitmapUploader
);
339 EmbedderData(mojo::Shell
* shell
, mojo::View
* root
) : bitmap_uploader_(root
) {
340 bitmap_uploader_
.Init(shell
);
341 bitmap_uploader_
.SetColor(g_background_color
);
344 BitmapUploader
& bitmap_uploader() { return bitmap_uploader_
; }
347 BitmapUploader bitmap_uploader_
;
349 DISALLOW_COPY_AND_ASSIGN(EmbedderData
);
352 class PDFView
: public mojo::ApplicationDelegate
,
353 public mojo::ViewManagerDelegate
,
354 public mojo::ViewObserver
{
356 PDFView(mojo::InterfaceRequest
<mojo::Application
> request
,
357 mojo::URLResponsePtr response
)
358 : app_(this, request
.Pass(), base::Bind(&PDFView::OnTerminate
,
359 base::Unretained(this))),
360 current_page_(0), page_count_(0), doc_(nullptr),
361 view_manager_client_factory_(app_
.shell(), this) {
362 FetchPDF(response
.Pass());
365 ~PDFView() override
{
367 FPDF_CloseDocument(doc_
);
368 for (auto& roots
: embedder_for_roots_
) {
369 roots
.first
->RemoveObserver(this);
375 // Overridden from ApplicationDelegate:
376 bool ConfigureIncomingConnection(
377 mojo::ApplicationConnection
* connection
) override
{
378 connection
->AddService(&view_manager_client_factory_
);
382 // Overridden from ViewManagerDelegate:
383 void OnEmbed(mojo::View
* root
) override
{
384 DCHECK(embedder_for_roots_
.find(root
) == embedder_for_roots_
.end());
385 root
->AddObserver(this);
386 EmbedderData
* embedder_data
= new EmbedderData(app_
.shell(), root
);
387 embedder_for_roots_
[root
] = embedder_data
;
388 DrawBitmap(embedder_data
);
391 void OnViewManagerDestroyed(mojo::ViewManager
* view_manager
) override
{}
393 // Overridden from ViewObserver:
394 void OnViewBoundsChanged(mojo::View
* view
,
395 const mojo::Rect
& old_bounds
,
396 const mojo::Rect
& new_bounds
) override
{
397 DCHECK(embedder_for_roots_
.find(view
) != embedder_for_roots_
.end());
398 DrawBitmap(embedder_for_roots_
[view
]);
401 void OnViewInputEvent(mojo::View
* view
,
402 const mojo::EventPtr
& event
) override
{
403 DCHECK(embedder_for_roots_
.find(view
) != embedder_for_roots_
.end());
404 if (event
->key_data
&&
405 (event
->action
!= mojo::EVENT_TYPE_KEY_PRESSED
||
406 event
->key_data
->is_char
)) {
410 if ((event
->key_data
&&
411 event
->key_data
->windows_key_code
== mojo::KEYBOARD_CODE_DOWN
) ||
412 (event
->pointer_data
&& event
->pointer_data
->vertical_wheel
< 0)) {
413 if (current_page_
< (page_count_
- 1)) {
415 DrawBitmap(embedder_for_roots_
[view
]);
417 } else if ((event
->key_data
&&
418 event
->key_data
->windows_key_code
== mojo::KEYBOARD_CODE_UP
) ||
419 (event
->pointer_data
&&
420 event
->pointer_data
->vertical_wheel
> 0)) {
421 if (current_page_
> 0) {
423 DrawBitmap(embedder_for_roots_
[view
]);
428 void OnViewDestroyed(mojo::View
* view
) override
{
429 DCHECK(embedder_for_roots_
.find(view
) != embedder_for_roots_
.end());
430 const auto& it
= embedder_for_roots_
.find(view
);
431 DCHECK(it
!= embedder_for_roots_
.end());
433 embedder_for_roots_
.erase(it
);
434 if (embedder_for_roots_
.size() == 0)
438 void DrawBitmap(EmbedderData
* embedder_data
) {
442 FPDF_PAGE page
= FPDF_LoadPage(doc_
, current_page_
);
443 int width
= static_cast<int>(FPDF_GetPageWidth(page
));
444 int height
= static_cast<int>(FPDF_GetPageHeight(page
));
446 scoped_ptr
<std::vector
<unsigned char>> bitmap
;
447 bitmap
.reset(new std::vector
<unsigned char>);
448 bitmap
->resize(width
* height
* 4);
450 FPDF_BITMAP f_bitmap
= FPDFBitmap_CreateEx(width
, height
, FPDFBitmap_BGRA
,
451 &(*bitmap
)[0], width
* 4);
452 FPDFBitmap_FillRect(f_bitmap
, 0, 0, width
, height
, 0xFFFFFFFF);
453 FPDF_RenderPageBitmap(f_bitmap
, page
, 0, 0, width
, height
, 0, 0);
454 FPDFBitmap_Destroy(f_bitmap
);
456 FPDF_ClosePage(page
);
458 embedder_data
->bitmap_uploader().SetBitmap(width
, height
, bitmap
.Pass(),
459 BitmapUploader::BGRA
);
462 void FetchPDF(mojo::URLResponsePtr response
) {
464 mojo::common::BlockingCopyToString(response
->body
.Pass(), &data_
);
465 if (data_
.length() >= static_cast<size_t>(std::numeric_limits
<int>::max()))
467 doc_
= FPDF_LoadMemDocument(data_
.data(), static_cast<int>(data_
.length()),
469 page_count_
= FPDF_GetPageCount(doc_
);
472 // Callback from the quit closure. We key off this rather than
473 // ApplicationDelegate::Quit() as we don't want to shut down the messageloop
474 // when we quit (the messageloop is shared among multiple PDFViews).
479 mojo::ApplicationImpl app_
;
484 std::map
<mojo::View
*, EmbedderData
*> embedder_for_roots_
;
485 mojo::ViewManagerClientFactory view_manager_client_factory_
;
487 DISALLOW_COPY_AND_ASSIGN(PDFView
);
490 class ContentHandlerImpl
: public mojo::ContentHandler
{
492 ContentHandlerImpl(mojo::InterfaceRequest
<ContentHandler
> request
)
493 : binding_(this, request
.Pass()) {}
494 ~ContentHandlerImpl() override
{}
497 // Overridden from ContentHandler:
498 void StartApplication(mojo::InterfaceRequest
<mojo::Application
> request
,
499 mojo::URLResponsePtr response
) override
{
500 new PDFView(request
.Pass(), response
.Pass());
503 mojo::StrongBinding
<mojo::ContentHandler
> binding_
;
505 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl
);
508 class PDFViewer
: public mojo::ApplicationDelegate
,
509 public mojo::InterfaceFactory
<mojo::ContentHandler
> {
512 v8::V8::InitializeICU();
516 ~PDFViewer() override
{
517 FPDF_DestroyLibrary();
521 // Overridden from ApplicationDelegate:
522 bool ConfigureIncomingConnection(
523 mojo::ApplicationConnection
* connection
) override
{
524 connection
->AddService(this);
528 // Overridden from InterfaceFactory<ContentHandler>:
529 void Create(mojo::ApplicationConnection
* connection
,
530 mojo::InterfaceRequest
<mojo::ContentHandler
> request
) override
{
531 new ContentHandlerImpl(request
.Pass());
534 DISALLOW_COPY_AND_ASSIGN(PDFViewer
);
538 } // namespace pdf_viewer
540 MojoResult
MojoMain(MojoHandle application_request
) {
541 mojo::ApplicationRunner
runner(new pdf_viewer::PDFViewer());
542 return runner
.Run(application_request
);