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