1 // Copyright 2012-2015 Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "wcore_error.h"
33 #include "nacl_container.h"
35 #include "nacl_platform.h"
39 nacl_dl_check_enum(int32_t waffle_dl
)
42 case WAFFLE_DL_OPENGL
:
43 wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM
,
44 "NACL does not support OpenGL");
46 case WAFFLE_DL_OPENGL_ES1
:
47 wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM
,
48 "NACL does not support OpenGL ES1");
50 case WAFFLE_DL_OPENGL_ES2
:
52 case WAFFLE_DL_OPENGL_ES3
:
53 wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM
,
54 "NACL does not support OpenGL ES3");
63 nacl_dl_open(struct nacl_platform
*plat
)
65 plat
->dl_gl
= dlopen(NACL_GLES2_LIBRARY
, RTLD_LAZY
);
68 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
69 "dlopen(\"%s\") failed: %s", NACL_GLES2_LIBRARY
, dlerror());
77 nacl_dl_can_open(struct wcore_platform
*wc_plat
,
80 struct nacl_platform
*plat
= nacl_platform(wc_plat
);
83 WCORE_ERROR_DISABLED({
84 ok
= nacl_dl_check_enum(waffle_dl
);
90 if (plat
->dl_gl
!= NULL
)
93 WCORE_ERROR_DISABLED({
97 return plat
->dl_gl
!= NULL
;
100 // Construct a string that maps GL function to NaCl function
101 // by concating given prefix and function name tail from 'src'.
103 nacl_dl_prefix(const char *src
, const char *prefix
)
105 if (strncmp(src
, "gl", 2) != 0) {
106 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
107 "NACL symbol name does not start with \"gl\"");
111 uint32_t len
= strlen(src
) + strlen(prefix
);
113 char *dst
= wcore_calloc(len
);
117 int n
= snprintf(dst
, len
, "%s%s", prefix
, src
+ 2);
118 if (n
< 0 || n
>= len
) {
119 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
120 "NACL cannot create symbol prefix");
129 nacl_dl_sym(struct wcore_platform
*wc_plat
,
133 struct nacl_platform
*plat
= nacl_platform(wc_plat
);
135 if (!nacl_dl_check_enum(waffle_dl
))
138 if (plat
->dl_gl
== NULL
)
141 if (plat
->dl_gl
== NULL
)
144 char *nacl_name
= nacl_dl_prefix(name
, "GLES2");
148 // Clear any previous error.
151 void *sym
= dlsym(plat
->dl_gl
, name
);
158 // dlsym returned NULL. Check if an error occured.
159 const char *error
= dlerror();
161 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
162 "dlsym(libname=\"%s\", symbol=\"%s\") failed: %s",
163 NACL_GLES2_LIBRARY
, nacl_name
, error
);
171 nacl_dl_close(struct wcore_platform
*wc_plat
)
173 struct nacl_platform
*plat
= nacl_platform(wc_plat
);
176 const char *error_msg
= NULL
;
181 error_code
= dlclose(plat
->dl_gl
);
186 error_msg
= dlerror();
189 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
190 "dlclose(libname=\"%s\") failed: %s",
191 NACL_GLES2_LIBRARY
, error_msg
);
194 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
195 "dlclose(libname=\"%s\") failed",