blob: 5d0c152b101c980e20d2c141f0d94962ca66f272 [file] [log] [blame]
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02001/* 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"
15#include <android/utils/debug.h>
16#include <android/utils/path.h>
17#include <android/utils/bufprint.h>
18#include <android/utils/dll.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)
23#define DD(...) VERBOSE_PRINT(gles,__VA_ARGS__)
24
25/* Name of the GLES rendering library we're going to use */
26#define RENDERER_LIB_NAME "libOpenglRender"
27
28/* These definitions *must* match those under:
29 * development/tools/emulator/opengl/host/include/libOpenglRender/render_api.h
30 */
31#define DYNLINK_FUNCTIONS \
32 DYNLINK_FUNC(int,initLibrary,(void),(),return) \
33 DYNLINK_FUNC(int,initOpenGLRenderer,(int width, int height, int port),(width,height,port),return) \
34 DYNLINK_FUNC(int,createOpenGLSubwindow,(void* window, int x, int y, int width, int height, float zRot),(window,x,y,width,height,zRot),return)\
35 DYNLINK_FUNC(int,destroyOpenGLSubwindow,(void),(),return)\
36 DYNLINK_FUNC(void,repaintOpenGLDisplay,(void),(),)\
37 DYNLINK_FUNC(void,stopOpenGLRenderer,(void),(),)
38
39
40#ifndef CONFIG_STANDALONE_UI
41/* Defined in android/hw-pipe-net.c */
42extern int android_init_opengles_pipes(void);
43#endif
44
45static ADynamicLibrary* rendererLib;
46
47/* Define the pointers and the wrapper functions to call them */
48#define DYNLINK_FUNC(result,name,sig,params,ret) \
49 static result (*_ptr_##name) sig; \
50 static result name sig { \
51 ret (*_ptr_##name) params ; \
52 }
53
54DYNLINK_FUNCTIONS
55
56#undef DYNLINK_FUNC
57
58static int
59initOpenglesEmulationFuncs(ADynamicLibrary* rendererLib)
60{
61 void* symbol;
62 char* error;
63#define DYNLINK_FUNC(result,name,sig,params,ret) \
64 symbol = adynamicLibrary_findSymbol( rendererLib, #name, &error ); \
65 if (symbol != NULL) { \
66 _ptr_##name = symbol; \
67 } else { \
68 derror("GLES emulation: Could not find required symbol (%s): %s", #name, error); \
69 free(error); \
70 return -1; \
71 }
72DYNLINK_FUNCTIONS
73#undef DYNLINK_FUNC
74 return 0;
75}
76
77int
78android_initOpenglesEmulation(void)
79{
80 char* error = NULL;
81
82 if (rendererLib != NULL)
83 return 0;
84
85 D("Initializing hardware OpenGLES emulation support");
86
87 rendererLib = adynamicLibrary_open(RENDERER_LIB_NAME, &error);
88 if (rendererLib == NULL) {
89 derror("Could not load OpenGLES emulation library: %s", error);
90 return -1;
91 }
92
93#ifndef CONFIG_STANDALONE_UI
94 android_init_opengles_pipes();
95#endif
96
97
98 /* Resolve the functions */
99 if (initOpenglesEmulationFuncs(rendererLib) < 0) {
100 derror("OpenGLES emulation library mismatch. Be sure to use the correct version!");
101 goto BAD_EXIT;
102 }
103
104 if (!initLibrary()) {
105 derror("OpenGLES initialization failed!");
106 goto BAD_EXIT;
107 }
108
109 return 0;
110
111BAD_EXIT:
112 derror("OpenGLES emulation library could not be initialized!");
113 adynamicLibrary_close(rendererLib);
114 rendererLib = NULL;
115 return -1;
116}
117
118int
119android_startOpenglesRenderer(int width, int height)
120{
121 if (!rendererLib) {
122 D("Can't start OpenGLES renderer without support libraries");
123 return -1;
124 }
125
126 if (initOpenGLRenderer(width, height,ANDROID_OPENGLES_BASE_PORT) != 0) {
127 D("Can't start OpenGLES renderer?");
128 return -1;
129 }
130 return 0;
131}
132
133void
134android_stopOpenglesRenderer(void)
135{
136 if (rendererLib) {
137 stopOpenGLRenderer();
138 }
139}
140
141int
142android_showOpenglesWindow(void* window, int x, int y, int width, int height, float rotation)
143{
144 if (rendererLib) {
145 return createOpenGLSubwindow(window, x, y, width, height, rotation);
146 } else {
147 return -1;
148 }
149}
150
151int
152android_hideOpenglesWindow(void)
153{
154 if (rendererLib) {
155 return destroyOpenGLSubwindow();
156 } else {
157 return -1;
158 }
159}
160
161void
162android_redrawOpenglesWindow(void)
163{
164 if (rendererLib) {
165 repaintOpenGLDisplay();
166 }
167}