blob: 9ab8e073790413e4f163ff1b1117c423a01bcf21 [file] [log] [blame]
Viet-Trung Luue126c862015-03-30 19:20:45 -07001// Copyright 2014 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.
4
5#include "shell/native_application_support.h"
6
7#include "base/files/file_path.h"
8#include "base/files/file_util.h"
9#include "base/logging.h"
James Robinsoncbf8d2e2015-08-21 18:13:44 -070010#include "mojo/public/platform/native/gles2_impl_chromium_bind_uniform_location_thunks.h"
James Robinsonfe8fa532015-08-24 11:10:17 -070011#include "mojo/public/platform/native/gles2_impl_chromium_map_sub_thunks.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070012#include "mojo/public/platform/native/gles2_impl_chromium_miscellaneous_thunks.h"
Viet-Trung Luua65790a2015-07-28 09:49:50 -070013#include "mojo/public/platform/native/gles2_impl_chromium_resize_thunks.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070014#include "mojo/public/platform/native/gles2_impl_chromium_sync_point_thunks.h"
15#include "mojo/public/platform/native/gles2_impl_chromium_texture_mailbox_thunks.h"
Viet-Trung Luu9bddf932015-08-05 12:14:29 -070016#include "mojo/public/platform/native/gles2_impl_ext_debug_marker_thunks.h"
James Robinson45d66292015-08-27 13:56:17 -070017#include "mojo/public/platform/native/gles2_impl_ext_discard_framebuffer_thunks.h"
James Robinson92035fd2015-08-27 15:41:19 -070018#include "mojo/public/platform/native/gles2_impl_ext_multisampled_render_to_texture_thunks.h"
Viet-Trung Luu32649a82015-09-16 14:41:14 -070019#include "mojo/public/platform/native/gles2_impl_ext_occlusion_query_thunks.h"
James Robinsonb998d4d2015-08-27 09:44:29 -070020#include "mojo/public/platform/native/gles2_impl_ext_texture_storage_thunks.h"
James Robinson3f90f822015-08-27 12:29:40 -070021#include "mojo/public/platform/native/gles2_impl_khr_blend_equation_advanced_thunks.h"
Viet-Trung Luu79667972015-08-05 08:53:30 -070022#include "mojo/public/platform/native/gles2_impl_oes_vertex_array_object_thunks.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070023#include "mojo/public/platform/native/gles2_impl_thunks.h"
Adam Bartha37b8c22016-04-21 13:46:39 -070024#include "mojo/public/platform/native/mgl_echo_thunks.h"
James Robinson1e96a132015-08-13 13:08:17 -070025#include "mojo/public/platform/native/mgl_onscreen_thunks.h"
James Robinson0cc70ad2015-08-28 13:31:37 -070026#include "mojo/public/platform/native/mgl_signal_sync_point_thunks.h"
James Robinson1e96a132015-08-13 13:08:17 -070027#include "mojo/public/platform/native/mgl_thunks.h"
Forrest Reiling74b0cac2016-03-02 16:21:15 -080028#include "mojo/public/platform/native/platform_handle_private_thunks.h"
Nick Brayda897d62015-04-08 16:28:55 -070029#include "mojo/public/platform/native/system_impl_private_thunks.h"
Viet-Trung Luue126c862015-03-30 19:20:45 -070030#include "mojo/public/platform/native/system_thunks.h"
31
Viet-Trung Luue126c862015-03-30 19:20:45 -070032namespace shell {
33
34namespace {
35
36template <typename Thunks>
37bool SetThunks(Thunks (*make_thunks)(),
38 const char* function_name,
39 base::NativeLibrary library) {
40 typedef size_t (*SetThunksFn)(const Thunks* thunks);
41 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>(
42 base::GetFunctionPointerFromNativeLibrary(library, function_name));
43 if (!set_thunks)
44 return false;
45 Thunks thunks = make_thunks();
46 size_t expected_size = set_thunks(&thunks);
47 if (expected_size > sizeof(Thunks)) {
48 LOG(ERROR) << "Invalid app library: expected " << function_name
49 << " to return thunks of size: " << expected_size;
50 return false;
51 }
52 return true;
53}
54
55} // namespace
56
Benjamin Lerman5d429aa2015-05-07 16:21:00 +020057base::NativeLibrary LoadNativeApplication(const base::FilePath& app_path) {
Viet-Trung Luue126c862015-03-30 19:20:45 -070058 DVLOG(2) << "Loading Mojo app in process from library: " << app_path.value();
59
60 base::NativeLibraryLoadError error;
61 base::NativeLibrary app_library = base::LoadNativeLibrary(app_path, &error);
Viet-Trung Luue126c862015-03-30 19:20:45 -070062 LOG_IF(ERROR, !app_library)
63 << "Failed to load app library (error: " << error.ToString() << ")";
64 return app_library;
65}
66
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070067bool RunNativeApplication(
68 base::NativeLibrary app_library,
69 mojo::InterfaceRequest<mojo::Application> application_request) {
Viet-Trung Luue126c862015-03-30 19:20:45 -070070 // Tolerate |app_library| being null, to make life easier for callers.
71 if (!app_library)
72 return false;
73
74 if (!SetThunks(&MojoMakeSystemThunks, "MojoSetSystemThunks", app_library)) {
75 LOG(ERROR) << "MojoSetSystemThunks not found";
76 return false;
77 }
78
Forrest Reiling74b0cac2016-03-02 16:21:15 -080079 // TODO(freiling): enforce the private nature of this API, somehow?
80 SetThunks(&MojoMakePlatformHandlePrivateThunks,
81 "MojoSetPlatformHandlePrivateThunks", app_library);
82
Nick Brayda897d62015-04-08 16:28:55 -070083 // TODO(ncbray): enforce the private nature of this API, somehow?
84 SetThunks(&MojoMakeSystemImplControlThunksPrivate,
85 "MojoSetSystemImplControlThunksPrivate", app_library);
86 SetThunks(&MojoMakeSystemImplThunksPrivate, "MojoSetSystemImplThunksPrivate",
87 app_library);
88
James Robinson1e96a132015-08-13 13:08:17 -070089 SetThunks(&MojoMakeGLES2ImplThunks, "MojoSetGLES2ImplThunks",
90 app_library);
James Robinson3ad6f4f2015-08-26 12:15:19 -070091 SetThunks(MojoMakeGLES2ImplEXTDebugMarkerThunks,
92 "MojoSetGLES2ImplEXTDebugMarkerThunks", app_library);
James Robinson45d66292015-08-27 13:56:17 -070093 SetThunks(MojoMakeGLES2ImplEXTDiscardFramebufferThunks,
94 "MojoSetGLES2ImplEXTDiscardFramebufferThunks", app_library);
Viet-Trung Luu32649a82015-09-16 14:41:14 -070095 SetThunks(MojoMakeGLES2ImplEXTOcclusionQueryThunks,
96 "MojoSetGLES2ImplEXTOcclusionQueryThunks", app_library);
James Robinsonb998d4d2015-08-27 09:44:29 -070097 SetThunks(MojoMakeGLES2ImplEXTTextureStorageThunks,
98 "MojoSetGLES2ImplEXTTextureStorageThunks", app_library);
James Robinson92035fd2015-08-27 15:41:19 -070099 SetThunks(MojoMakeGLES2ImplEXTMultisampledRenderToTextureThunks,
100 "MojoSetGLES2ImplEXTMultisampledRenderToTextureThunks",
101 app_library);
James Robinson3f90f822015-08-27 12:29:40 -0700102 SetThunks(MojoMakeGLES2ImplKHRBlendEquationAdvancedThunks,
103 "MojoSetGLES2ImplKHRBlendEquationAdvancedThunks", app_library);
James Robinson3ad6f4f2015-08-26 12:15:19 -0700104 SetThunks(MojoMakeGLES2ImplOESVertexArrayObjectThunks,
105 "MojoSetGLES2ImplOESVertexArrayObjectThunks", app_library);
Viet-Trung Luu32649a82015-09-16 14:41:14 -0700106 // Deprecated name for "MojoSetGLES2ImplEXTOcclusionQueryThunks" (TODO(vtl):
107 // when no app is using this name any longer, delete it):
108 SetThunks(MojoMakeGLES2ImplEXTOcclusionQueryThunks,
109 "MojoSetGLES2ImplOcclusionQueryEXTThunks", app_library);
James Robinson1e96a132015-08-13 13:08:17 -0700110 // "Chromium" extensions:
James Robinson3ad6f4f2015-08-26 12:15:19 -0700111 SetThunks(MojoMakeGLES2ImplCHROMIUMBindUniformLocationThunks,
112 "MojoSetGLES2ImplCHROMIUMBindUniformLocationThunks", app_library);
113 SetThunks(MojoMakeGLES2ImplCHROMIUMMapSubThunks,
114 "MojoSetGLES2ImplCHROMIUMMapSubThunks", app_library);
115 SetThunks(MojoMakeGLES2ImplCHROMIUMMiscellaneousThunks,
116 "MojoSetGLES2ImplCHROMIUMMiscellaneousThunks", app_library);
117 SetThunks(MojoMakeGLES2ImplCHROMIUMResizeThunks,
118 "MojoSetGLES2ImplCHROMIUMResizeThunks", app_library);
119 SetThunks(MojoMakeGLES2ImplCHROMIUMSyncPointThunks,
120 "MojoSetGLES2ImplCHROMIUMSyncPointThunks", app_library);
121 SetThunks(MojoMakeGLES2ImplCHROMIUMTextureMailboxThunks,
122 "MojoSetGLES2ImplCHROMIUMTextureMailboxThunks", app_library);
Viet-Trung Luue126c862015-03-30 19:20:45 -0700123
James Robinson1e96a132015-08-13 13:08:17 -0700124 if (SetThunks(MojoMakeMGLThunks, "MojoSetMGLThunks", app_library)) {
Adam Bartha37b8c22016-04-21 13:46:39 -0700125 SetThunks(MojoMakeMGLEchoThunks, "MojoSetMGLEchoThunks", app_library);
126
James Robinson0cc70ad2015-08-28 13:31:37 -0700127 // TODO(jamesr): We should only need to expose the onscreen thunks to apps
128 // that need to draw to the screen like the system compositor.
James Robinson1e96a132015-08-13 13:08:17 -0700129 SetThunks(MojoMakeMGLOnscreenThunks, "MojoSetMGLOnscreenThunks",
130 app_library);
James Robinson1e96a132015-08-13 13:08:17 -0700131
James Robinson0cc70ad2015-08-28 13:31:37 -0700132 SetThunks(MojoMakeMGLSignalSyncPointThunks,
133 "MojoSetMGLSignalSyncPointThunks", app_library);
134 }
Viet-Trung Luue126c862015-03-30 19:20:45 -0700135
Viet-Trung Luue126c862015-03-30 19:20:45 -0700136 typedef MojoResult (*MojoMainFunction)(MojoHandle);
137 MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
138 base::GetFunctionPointerFromNativeLibrary(app_library, "MojoMain"));
139 if (!main_function) {
140 LOG(ERROR) << "MojoMain not found";
141 return false;
142 }
Mitch Rudominer15619102015-04-10 10:43:34 -0700143 // |MojoMain()| takes ownership of the Application request handle.
Viet-Trung Luue126c862015-03-30 19:20:45 -0700144 MojoHandle handle = application_request.PassMessagePipe().release().value();
145 MojoResult result = main_function(handle);
Viet-Trung Luuf2a8c262015-04-02 13:54:32 -0700146 LOG_IF(ERROR, result != MOJO_RESULT_OK)
147 << "MojoMain returned error (result: " << result << ")";
Viet-Trung Luue126c862015-03-30 19:20:45 -0700148 return true;
149}
150
151} // namespace shell