David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2011 The Android Open Source Project |
| 2 | ** |
| 3 | ** This software is licensed under the terms of the GNU General Public |
| 4 | ** License version 2, as published by the Free Software Foundation, and |
| 5 | ** may be copied, distributed, and modified under those terms. |
| 6 | ** |
| 7 | ** This program is distributed in the hope that it will be useful, |
| 8 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | ** GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include "config-host.h" |
| 14 | #include "android/opengles.h" |
Jesse Hall | 183e927 | 2012-04-26 15:13:27 -0700 | [diff] [blame^] | 15 | |
| 16 | /* Declared in "android/globals.h" */ |
| 17 | int android_gles_fast_pipes = 1; |
| 18 | |
| 19 | #if CONFIG_ANDROID_OPENGLES |
| 20 | |
David Turner | 7b56a4a | 2011-09-12 18:21:58 +0200 | [diff] [blame] | 21 | #include "android/globals.h" |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 22 | #include <android/utils/debug.h> |
| 23 | #include <android/utils/path.h> |
| 24 | #include <android/utils/bufprint.h> |
| 25 | #include <android/utils/dll.h> |
Jesse Hall | 183e927 | 2012-04-26 15:13:27 -0700 | [diff] [blame^] | 26 | |
| 27 | #define RENDER_API_NO_PROTOTYPES 1 |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 28 | #include <libOpenglRender/render_api.h> |
Jesse Hall | 183e927 | 2012-04-26 15:13:27 -0700 | [diff] [blame^] | 29 | |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #define D(...) VERBOSE_PRINT(init,__VA_ARGS__) |
| 34 | #define DD(...) VERBOSE_PRINT(gles,__VA_ARGS__) |
| 35 | |
| 36 | /* Name of the GLES rendering library we're going to use */ |
Andrew Hsieh | c7389bd | 2012-03-13 02:13:40 -0700 | [diff] [blame] | 37 | #if HOST_LONG_BITS == 32 |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 38 | #define RENDERER_LIB_NAME "libOpenglRender" |
Andrew Hsieh | c7389bd | 2012-03-13 02:13:40 -0700 | [diff] [blame] | 39 | #elif HOST_LONG_BITS == 64 |
| 40 | #define RENDERER_LIB_NAME "lib64OpenglRender" |
| 41 | #else |
| 42 | #error Unknown HOST_LONG_BITS |
| 43 | #endif |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 44 | |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 45 | #define DYNLINK_FUNCTIONS \ |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 46 | DYNLINK_FUNC(initLibrary) \ |
| 47 | DYNLINK_FUNC(setStreamMode) \ |
| 48 | DYNLINK_FUNC(initOpenGLRenderer) \ |
| 49 | DYNLINK_FUNC(createOpenGLSubwindow) \ |
| 50 | DYNLINK_FUNC(destroyOpenGLSubwindow) \ |
| 51 | DYNLINK_FUNC(repaintOpenGLDisplay) \ |
| 52 | DYNLINK_FUNC(stopOpenGLRenderer) |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 53 | |
| 54 | #ifndef CONFIG_STANDALONE_UI |
| 55 | /* Defined in android/hw-pipe-net.c */ |
| 56 | extern int android_init_opengles_pipes(void); |
| 57 | #endif |
| 58 | |
| 59 | static ADynamicLibrary* rendererLib; |
| 60 | |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 61 | /* Define the function pointers */ |
| 62 | #define DYNLINK_FUNC(name) \ |
| 63 | static name##Fn name = NULL; |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 64 | DYNLINK_FUNCTIONS |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 65 | #undef DYNLINK_FUNC |
| 66 | |
| 67 | static int |
| 68 | initOpenglesEmulationFuncs(ADynamicLibrary* rendererLib) |
| 69 | { |
| 70 | void* symbol; |
| 71 | char* error; |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 72 | |
| 73 | #define DYNLINK_FUNC(name) \ |
| 74 | symbol = adynamicLibrary_findSymbol(rendererLib, #name, &error); \ |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 75 | if (symbol != NULL) { \ |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 76 | name = symbol; \ |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 77 | } else { \ |
| 78 | derror("GLES emulation: Could not find required symbol (%s): %s", #name, error); \ |
| 79 | free(error); \ |
| 80 | return -1; \ |
| 81 | } |
| 82 | DYNLINK_FUNCTIONS |
| 83 | #undef DYNLINK_FUNC |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 84 | |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | android_initOpenglesEmulation(void) |
| 90 | { |
| 91 | char* error = NULL; |
| 92 | |
| 93 | if (rendererLib != NULL) |
| 94 | return 0; |
| 95 | |
| 96 | D("Initializing hardware OpenGLES emulation support"); |
| 97 | |
| 98 | rendererLib = adynamicLibrary_open(RENDERER_LIB_NAME, &error); |
| 99 | if (rendererLib == NULL) { |
| 100 | derror("Could not load OpenGLES emulation library: %s", error); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | #ifndef CONFIG_STANDALONE_UI |
| 105 | android_init_opengles_pipes(); |
| 106 | #endif |
| 107 | |
| 108 | |
| 109 | /* Resolve the functions */ |
| 110 | if (initOpenglesEmulationFuncs(rendererLib) < 0) { |
| 111 | derror("OpenGLES emulation library mismatch. Be sure to use the correct version!"); |
| 112 | goto BAD_EXIT; |
| 113 | } |
| 114 | |
| 115 | if (!initLibrary()) { |
| 116 | derror("OpenGLES initialization failed!"); |
| 117 | goto BAD_EXIT; |
| 118 | } |
| 119 | |
David Turner | 7b56a4a | 2011-09-12 18:21:58 +0200 | [diff] [blame] | 120 | if (android_gles_fast_pipes) { |
| 121 | #ifdef _WIN32 |
| 122 | /* XXX: NEED Win32 pipe implementation */ |
| 123 | setStreamMode(STREAM_MODE_TCP); |
| 124 | #else |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 125 | setStreamMode(STREAM_MODE_UNIX); |
David Turner | 7b56a4a | 2011-09-12 18:21:58 +0200 | [diff] [blame] | 126 | #endif |
| 127 | } else { |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 128 | setStreamMode(STREAM_MODE_TCP); |
David Turner | 7b56a4a | 2011-09-12 18:21:58 +0200 | [diff] [blame] | 129 | } |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 130 | return 0; |
| 131 | |
| 132 | BAD_EXIT: |
| 133 | derror("OpenGLES emulation library could not be initialized!"); |
| 134 | adynamicLibrary_close(rendererLib); |
| 135 | rendererLib = NULL; |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | int |
Jesse Hall | 07ca7c2 | 2012-04-25 22:05:07 -0700 | [diff] [blame] | 140 | android_startOpenglesRenderer(int width, int height, OnPostFunc onPost, void* onPostContext) |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 141 | { |
| 142 | if (!rendererLib) { |
| 143 | D("Can't start OpenGLES renderer without support libraries"); |
| 144 | return -1; |
| 145 | } |
| 146 | |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 147 | if (!initOpenGLRenderer(width, height, ANDROID_OPENGLES_BASE_PORT, onPost, onPostContext)) { |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 148 | D("Can't start OpenGLES renderer?"); |
| 149 | return -1; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | android_stopOpenglesRenderer(void) |
| 156 | { |
| 157 | if (rendererLib) { |
| 158 | stopOpenGLRenderer(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | android_showOpenglesWindow(void* window, int x, int y, int width, int height, float rotation) |
| 164 | { |
| 165 | if (rendererLib) { |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 166 | int success = createOpenGLSubwindow((FBNativeWindowType)window, x, y, width, height, rotation); |
| 167 | return success ? 0 : -1; |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 168 | } else { |
| 169 | return -1; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | int |
| 174 | android_hideOpenglesWindow(void) |
| 175 | { |
| 176 | if (rendererLib) { |
Jesse Hall | 6699b68 | 2012-04-18 10:28:46 -0700 | [diff] [blame] | 177 | int success = destroyOpenGLSubwindow(); |
| 178 | return success ? 0 : -1; |
David 'Digit' Turner | cb88e79 | 2011-08-26 01:35:14 +0200 | [diff] [blame] | 179 | } else { |
| 180 | return -1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | android_redrawOpenglesWindow(void) |
| 186 | { |
| 187 | if (rendererLib) { |
| 188 | repaintOpenGLDisplay(); |
| 189 | } |
| 190 | } |
David Turner | 7b56a4a | 2011-09-12 18:21:58 +0200 | [diff] [blame] | 191 | |
| 192 | void |
| 193 | android_gles_unix_path(char* buff, size_t buffsize, int port) |
| 194 | { |
| 195 | const char* user = getenv("USER"); |
| 196 | char *p = buff, *end = buff + buffsize; |
| 197 | |
| 198 | /* The logic here must correspond to the one inside |
| 199 | * development/tools/emulator/opengl/shared/libOpenglCodecCommon/UnixStream.cpp */ |
| 200 | p = bufprint(p, end, "/tmp/"); |
| 201 | if (user && user[0]) { |
| 202 | p = bufprint(p, end, "android-%s/", user); |
| 203 | } |
| 204 | p = bufprint(p, end, "qemu-gles-%d", port); |
| 205 | } |
Jesse Hall | 183e927 | 2012-04-26 15:13:27 -0700 | [diff] [blame^] | 206 | |
| 207 | #else // CONFIG_ANDROID_OPENGLES |
| 208 | |
| 209 | int android_initOpenglesEmulation(void) |
| 210 | { |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | int android_startOpenglesRenderer(int width, int height, OnPostFunc onPost, void* onPostContext) |
| 215 | { |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | void android_stopOpenglesRenderer(void) |
| 220 | {} |
| 221 | |
| 222 | int android_showOpenglesWindow(void* window, int x, int y, int width, int height, float rotation) |
| 223 | { |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | int android_hideOpenglesWindow(void) |
| 228 | { |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | void android_redrawOpenglesWindow(void) |
| 233 | {} |
| 234 | |
| 235 | void android_gles_unix_path(char* buff, size_t buffsize, int port) |
| 236 | { |
| 237 | buff[0] = '\0'; |
| 238 | } |
| 239 | |
| 240 | #endif // !CONFIG_ANDROID_OPENGLES |