rsdGL.cpp revision 2382aba4a55c6ae74789c478eead8fbd96593321
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ui/FramebufferNativeWindow.h>
18#include <ui/PixelFormat.h>
19#include <ui/EGLUtils.h>
20#include <ui/egl/android_natives.h>
21
22#include <sys/types.h>
23#include <sys/resource.h>
24#include <sched.h>
25
26#include <cutils/properties.h>
27
28#include <GLES/gl.h>
29#include <GLES/glext.h>
30#include <GLES2/gl2.h>
31#include <GLES2/gl2ext.h>
32
33#include <string.h>
34
35#include "rsdCore.h"
36#include "rsdGL.h"
37
38#include <malloc.h>
39#include "rsContext.h"
40#include "rsdShaderCache.h"
41#include "rsdVertexArray.h"
42#include "rsdFrameBufferObj.h"
43
44using namespace android;
45using namespace android::renderscript;
46
47static int32_t gGLContextCount = 0;
48
49static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
50    if (returnVal != EGL_TRUE) {
51        fprintf(stderr, "%s() returned %d\n", op, returnVal);
52    }
53
54    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
55            = eglGetError()) {
56        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
57                error);
58    }
59}
60
61static void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
62
63#define X(VAL) {VAL, #VAL}
64    struct {EGLint attribute; const char* name;} names[] = {
65    X(EGL_BUFFER_SIZE),
66    X(EGL_ALPHA_SIZE),
67    X(EGL_BLUE_SIZE),
68    X(EGL_GREEN_SIZE),
69    X(EGL_RED_SIZE),
70    X(EGL_DEPTH_SIZE),
71    X(EGL_STENCIL_SIZE),
72    X(EGL_CONFIG_CAVEAT),
73    X(EGL_CONFIG_ID),
74    X(EGL_LEVEL),
75    X(EGL_MAX_PBUFFER_HEIGHT),
76    X(EGL_MAX_PBUFFER_PIXELS),
77    X(EGL_MAX_PBUFFER_WIDTH),
78    X(EGL_NATIVE_RENDERABLE),
79    X(EGL_NATIVE_VISUAL_ID),
80    X(EGL_NATIVE_VISUAL_TYPE),
81    X(EGL_SAMPLES),
82    X(EGL_SAMPLE_BUFFERS),
83    X(EGL_SURFACE_TYPE),
84    X(EGL_TRANSPARENT_TYPE),
85    X(EGL_TRANSPARENT_RED_VALUE),
86    X(EGL_TRANSPARENT_GREEN_VALUE),
87    X(EGL_TRANSPARENT_BLUE_VALUE),
88    X(EGL_BIND_TO_TEXTURE_RGB),
89    X(EGL_BIND_TO_TEXTURE_RGBA),
90    X(EGL_MIN_SWAP_INTERVAL),
91    X(EGL_MAX_SWAP_INTERVAL),
92    X(EGL_LUMINANCE_SIZE),
93    X(EGL_ALPHA_MASK_SIZE),
94    X(EGL_COLOR_BUFFER_TYPE),
95    X(EGL_RENDERABLE_TYPE),
96    X(EGL_CONFORMANT),
97   };
98#undef X
99
100    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
101        EGLint value = -1;
102        EGLBoolean returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
103        if (returnVal) {
104            LOGV(" %s: %d (0x%x)", names[j].name, value, value);
105        }
106    }
107}
108
109static void DumpDebug(RsdHal *dc) {
110    LOGE(" EGL ver %i %i", dc->gl.egl.majorVersion, dc->gl.egl.minorVersion);
111    LOGE(" EGL context %p  surface %p,  Display=%p", dc->gl.egl.context, dc->gl.egl.surface,
112         dc->gl.egl.display);
113    LOGE(" GL vendor: %s", dc->gl.gl.vendor);
114    LOGE(" GL renderer: %s", dc->gl.gl.renderer);
115    LOGE(" GL Version: %s", dc->gl.gl.version);
116    LOGE(" GL Extensions: %s", dc->gl.gl.extensions);
117    LOGE(" GL int Versions %i %i", dc->gl.gl.majorVersion, dc->gl.gl.minorVersion);
118
119    LOGV("MAX Textures %i, %i  %i", dc->gl.gl.maxVertexTextureUnits,
120         dc->gl.gl.maxFragmentTextureImageUnits, dc->gl.gl.maxTextureImageUnits);
121    LOGV("MAX Attribs %i", dc->gl.gl.maxVertexAttribs);
122    LOGV("MAX Uniforms %i, %i", dc->gl.gl.maxVertexUniformVectors,
123         dc->gl.gl.maxFragmentUniformVectors);
124    LOGV("MAX Varyings %i", dc->gl.gl.maxVaryingVectors);
125}
126
127void rsdGLShutdown(const Context *rsc) {
128    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
129
130    dc->gl.shaderCache->cleanupAll();
131    delete dc->gl.shaderCache;
132
133    delete dc->gl.vertexArrayState;
134
135    LOGV("%p, deinitEGL", rsc);
136
137    if (dc->gl.egl.context != EGL_NO_CONTEXT) {
138        RSD_CALL_GL(eglMakeCurrent, dc->gl.egl.display,
139                    EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
140        RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surfaceDefault);
141        if (dc->gl.egl.surface != EGL_NO_SURFACE) {
142            RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surface);
143        }
144        RSD_CALL_GL(eglDestroyContext, dc->gl.egl.display, dc->gl.egl.context);
145        checkEglError("eglDestroyContext");
146    }
147
148    gGLContextCount--;
149    if (!gGLContextCount) {
150        RSD_CALL_GL(eglTerminate, dc->gl.egl.display);
151    }
152}
153
154bool rsdGLInit(const Context *rsc) {
155    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
156
157    dc->gl.egl.numConfigs = -1;
158    EGLint configAttribs[128];
159    EGLint *configAttribsPtr = configAttribs;
160    EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
161
162    memset(configAttribs, 0, sizeof(configAttribs));
163
164    configAttribsPtr[0] = EGL_SURFACE_TYPE;
165    configAttribsPtr[1] = EGL_WINDOW_BIT;
166    configAttribsPtr += 2;
167
168    configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
169    configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
170    configAttribsPtr += 2;
171
172    configAttribsPtr[0] = EGL_RED_SIZE;
173    configAttribsPtr[1] = 8;
174    configAttribsPtr += 2;
175
176    configAttribsPtr[0] = EGL_GREEN_SIZE;
177    configAttribsPtr[1] = 8;
178    configAttribsPtr += 2;
179
180    configAttribsPtr[0] = EGL_BLUE_SIZE;
181    configAttribsPtr[1] = 8;
182    configAttribsPtr += 2;
183
184    if (rsc->mUserSurfaceConfig.alphaMin > 0) {
185        configAttribsPtr[0] = EGL_ALPHA_SIZE;
186        configAttribsPtr[1] = rsc->mUserSurfaceConfig.alphaMin;
187        configAttribsPtr += 2;
188    }
189
190    if (rsc->mUserSurfaceConfig.depthMin > 0) {
191        configAttribsPtr[0] = EGL_DEPTH_SIZE;
192        configAttribsPtr[1] = rsc->mUserSurfaceConfig.depthMin;
193        configAttribsPtr += 2;
194    }
195
196    if (rsc->mDev->mForceSW) {
197        configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
198        configAttribsPtr[1] = EGL_SLOW_CONFIG;
199        configAttribsPtr += 2;
200    }
201
202    configAttribsPtr[0] = EGL_NONE;
203    rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
204
205    LOGV("%p initEGL start", rsc);
206    rsc->setWatchdogGL("eglGetDisplay", __LINE__, __FILE__);
207    dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
208    checkEglError("eglGetDisplay");
209
210    RSD_CALL_GL(eglInitialize, dc->gl.egl.display,
211                &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
212    checkEglError("eglInitialize");
213
214    EGLBoolean ret;
215
216    EGLint numConfigs = -1, n = 0;
217    rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
218    ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
219    checkEglError("eglGetConfigs", ret);
220
221    if (numConfigs) {
222        EGLConfig* const configs = new EGLConfig[numConfigs];
223
224        rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
225        ret = eglChooseConfig(dc->gl.egl.display,
226                configAttribs, configs, numConfigs, &n);
227        if (!ret || !n) {
228            checkEglError("eglChooseConfig", ret);
229            LOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
230        }
231
232        // The first config is guaranteed to over-satisfy the constraints
233        dc->gl.egl.config = configs[0];
234
235        // go through the list and skip configs that over-satisfy our needs
236        for (int i=0 ; i<n ; i++) {
237            if (rsc->mUserSurfaceConfig.alphaMin <= 0) {
238                EGLint alphaSize;
239                eglGetConfigAttrib(dc->gl.egl.display,
240                        configs[i], EGL_ALPHA_SIZE, &alphaSize);
241                if (alphaSize > 0) {
242                    continue;
243                }
244            }
245
246            if (rsc->mUserSurfaceConfig.depthMin <= 0) {
247                EGLint depthSize;
248                eglGetConfigAttrib(dc->gl.egl.display,
249                        configs[i], EGL_DEPTH_SIZE, &depthSize);
250                if (depthSize > 0) {
251                    continue;
252                }
253            }
254
255            // Found one!
256            dc->gl.egl.config = configs[i];
257            break;
258        }
259
260        delete [] configs;
261    }
262
263    //if (props.mLogVisual) {
264    if (0) {
265        printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
266    }
267    //}
268
269    rsc->setWatchdogGL("eglCreateContext", __LINE__, __FILE__);
270    dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
271                                          EGL_NO_CONTEXT, context_attribs2);
272    checkEglError("eglCreateContext");
273    if (dc->gl.egl.context == EGL_NO_CONTEXT) {
274        LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
275        rsc->setWatchdogGL(NULL, 0, NULL);
276        return false;
277    }
278    gGLContextCount++;
279
280
281    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
282    rsc->setWatchdogGL("eglCreatePbufferSurface", __LINE__, __FILE__);
283    dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
284                                                        pbuffer_attribs);
285    checkEglError("eglCreatePbufferSurface");
286    if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
287        LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
288        rsdGLShutdown(rsc);
289        rsc->setWatchdogGL(NULL, 0, NULL);
290        return false;
291    }
292
293    rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
294    ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
295                         dc->gl.egl.surfaceDefault, dc->gl.egl.context);
296    if (ret == EGL_FALSE) {
297        LOGE("eglMakeCurrent returned EGL_FALSE");
298        checkEglError("eglMakeCurrent", ret);
299        rsdGLShutdown(rsc);
300        rsc->setWatchdogGL(NULL, 0, NULL);
301        return false;
302    }
303
304    dc->gl.gl.version = glGetString(GL_VERSION);
305    dc->gl.gl.vendor = glGetString(GL_VENDOR);
306    dc->gl.gl.renderer = glGetString(GL_RENDERER);
307    dc->gl.gl.extensions = glGetString(GL_EXTENSIONS);
308
309    //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
310    //LOGV("GL Version %s", mGL.mVersion);
311    //LOGV("GL Vendor %s", mGL.mVendor);
312    //LOGV("GL Renderer %s", mGL.mRenderer);
313    //LOGV("GL Extensions %s", mGL.mExtensions);
314
315    const char *verptr = NULL;
316    if (strlen((const char *)dc->gl.gl.version) > 9) {
317        if (!memcmp(dc->gl.gl.version, "OpenGL ES-CM", 12)) {
318            verptr = (const char *)dc->gl.gl.version + 12;
319        }
320        if (!memcmp(dc->gl.gl.version, "OpenGL ES ", 10)) {
321            verptr = (const char *)dc->gl.gl.version + 9;
322        }
323    }
324
325    if (!verptr) {
326        LOGE("Error, OpenGL ES Lite not supported");
327        rsdGLShutdown(rsc);
328        rsc->setWatchdogGL(NULL, 0, NULL);
329        return false;
330    } else {
331        sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
332    }
333
334    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &dc->gl.gl.maxVertexAttribs);
335    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &dc->gl.gl.maxVertexUniformVectors);
336    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxVertexTextureUnits);
337
338    glGetIntegerv(GL_MAX_VARYING_VECTORS, &dc->gl.gl.maxVaryingVectors);
339    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxTextureImageUnits);
340
341    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxFragmentTextureImageUnits);
342    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &dc->gl.gl.maxFragmentUniformVectors);
343
344    dc->gl.gl.OES_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
345                                                "GL_OES_texture_npot");
346    dc->gl.gl.GL_IMG_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
347                                                   "GL_IMG_texture_npot");
348    dc->gl.gl.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)dc->gl.gl.extensions,
349                                                            "GL_NV_texture_npot_2D_mipmap");
350    dc->gl.gl.EXT_texture_max_aniso = 1.0f;
351    bool hasAniso = NULL != strstr((const char *)dc->gl.gl.extensions,
352                                   "GL_EXT_texture_filter_anisotropic");
353    if (hasAniso) {
354        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &dc->gl.gl.EXT_texture_max_aniso);
355    }
356
357    if (0) {
358        DumpDebug(dc);
359    }
360
361    dc->gl.shaderCache = new RsdShaderCache();
362    dc->gl.vertexArrayState = new RsdVertexArrayState();
363    dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs);
364    dc->gl.currentFrameBuffer = NULL;
365
366    LOGV("initGLThread end %p", rsc);
367    rsc->setWatchdogGL(NULL, 0, NULL);
368    return true;
369}
370
371
372bool rsdGLSetSurface(const Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
373    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
374
375    EGLBoolean ret;
376    // WAR: Some drivers fail to handle 0 size surfaces correcntly.
377    // Use the pbuffer to avoid this pitfall.
378    if ((dc->gl.egl.surface != NULL) || (w == 0) || (h == 0)) {
379        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
380        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
381                             dc->gl.egl.surfaceDefault, dc->gl.egl.context);
382        checkEglError("eglMakeCurrent", ret);
383
384        rsc->setWatchdogGL("eglDestroySurface", __LINE__, __FILE__);
385        ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
386        checkEglError("eglDestroySurface", ret);
387
388        dc->gl.egl.surface = NULL;
389        dc->gl.width = 1;
390        dc->gl.height = 1;
391    }
392
393    if (dc->gl.wndSurface != NULL) {
394        dc->gl.wndSurface->decStrong(NULL);
395    }
396
397    dc->gl.wndSurface = (ANativeWindow *)sur;
398    if (dc->gl.wndSurface != NULL) {
399        dc->gl.wndSurface->incStrong(NULL);
400        dc->gl.width = w;
401        dc->gl.height = h;
402
403        rsc->setWatchdogGL("eglCreateWindowSurface", __LINE__, __FILE__);
404        dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
405                                                    dc->gl.wndSurface, NULL);
406        checkEglError("eglCreateWindowSurface");
407        if (dc->gl.egl.surface == EGL_NO_SURFACE) {
408            LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
409        }
410
411        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
412        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
413                             dc->gl.egl.surface, dc->gl.egl.context);
414        checkEglError("eglMakeCurrent", ret);
415    }
416    rsc->setWatchdogGL(NULL, 0, NULL);
417    return true;
418}
419
420void rsdGLSwap(const android::renderscript::Context *rsc) {
421    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
422    RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
423}
424
425void rsdGLCheckError(const android::renderscript::Context *rsc,
426                     const char *msg, bool isFatal) {
427    GLenum err = glGetError();
428    if (err != GL_NO_ERROR) {
429        char buf[1024];
430        snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
431
432        if (isFatal) {
433            rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
434        } else {
435            switch (err) {
436            case GL_OUT_OF_MEMORY:
437                rsc->setError(RS_ERROR_OUT_OF_MEMORY, buf);
438                break;
439            default:
440                rsc->setError(RS_ERROR_DRIVER, buf);
441                break;
442            }
443        }
444
445        LOGE("%p, %s", rsc, buf);
446    }
447
448}
449