rsdGL.cpp revision a04e30dbb5ab11592b03666bb3d102070759c58e
14b3de47071d875faaa7d419d050a464b09538797Jason Sams/*
24b3de47071d875faaa7d419d050a464b09538797Jason Sams * Copyright (C) 2011 The Android Open Source Project
34b3de47071d875faaa7d419d050a464b09538797Jason Sams *
44b3de47071d875faaa7d419d050a464b09538797Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
54b3de47071d875faaa7d419d050a464b09538797Jason Sams * you may not use this file except in compliance with the License.
64b3de47071d875faaa7d419d050a464b09538797Jason Sams * You may obtain a copy of the License at
74b3de47071d875faaa7d419d050a464b09538797Jason Sams *
84b3de47071d875faaa7d419d050a464b09538797Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
94b3de47071d875faaa7d419d050a464b09538797Jason Sams *
104b3de47071d875faaa7d419d050a464b09538797Jason Sams * Unless required by applicable law or agreed to in writing, software
114b3de47071d875faaa7d419d050a464b09538797Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
124b3de47071d875faaa7d419d050a464b09538797Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b3de47071d875faaa7d419d050a464b09538797Jason Sams * See the License for the specific language governing permissions and
144b3de47071d875faaa7d419d050a464b09538797Jason Sams * limitations under the License.
154b3de47071d875faaa7d419d050a464b09538797Jason Sams */
164b3de47071d875faaa7d419d050a464b09538797Jason Sams
174b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <ui/FramebufferNativeWindow.h>
184b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <ui/PixelFormat.h>
194b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <ui/EGLUtils.h>
204b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <ui/egl/android_natives.h>
214b3de47071d875faaa7d419d050a464b09538797Jason Sams
224b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sys/types.h>
234b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sys/resource.h>
244b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sched.h>
254b3de47071d875faaa7d419d050a464b09538797Jason Sams
264b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <cutils/properties.h>
274b3de47071d875faaa7d419d050a464b09538797Jason Sams
284b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES/gl.h>
294b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES/glext.h>
304b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES2/gl2.h>
314b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES2/gl2ext.h>
324b3de47071d875faaa7d419d050a464b09538797Jason Sams
334b3de47071d875faaa7d419d050a464b09538797Jason Sams//#include <cutils/sched_policy.h>
344b3de47071d875faaa7d419d050a464b09538797Jason Sams//#include <sys/syscall.h>
354b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <string.h>
364b3de47071d875faaa7d419d050a464b09538797Jason Sams
374b3de47071d875faaa7d419d050a464b09538797Jason Sams
384b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdCore.h"
394b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdGL.h"
404b3de47071d875faaa7d419d050a464b09538797Jason Sams
414b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <malloc.h>
424b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsContext.h"
43a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdShaderCache.h"
44a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdVertexArray.h"
454b3de47071d875faaa7d419d050a464b09538797Jason Sams
464b3de47071d875faaa7d419d050a464b09538797Jason Samsusing namespace android;
474b3de47071d875faaa7d419d050a464b09538797Jason Samsusing namespace android::renderscript;
484b3de47071d875faaa7d419d050a464b09538797Jason Sams
494b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic int32_t gGLContextCount = 0;
504b3de47071d875faaa7d419d050a464b09538797Jason Sams
514b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
524b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (returnVal != EGL_TRUE) {
534b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "%s() returned %d\n", op, returnVal);
544b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
554b3de47071d875faaa7d419d050a464b09538797Jason Sams
564b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
574b3de47071d875faaa7d419d050a464b09538797Jason Sams            = eglGetError()) {
584b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
594b3de47071d875faaa7d419d050a464b09538797Jason Sams                error);
604b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
614b3de47071d875faaa7d419d050a464b09538797Jason Sams}
624b3de47071d875faaa7d419d050a464b09538797Jason Sams
634b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
644b3de47071d875faaa7d419d050a464b09538797Jason Sams
654b3de47071d875faaa7d419d050a464b09538797Jason Sams#define X(VAL) {VAL, #VAL}
664b3de47071d875faaa7d419d050a464b09538797Jason Sams    struct {EGLint attribute; const char* name;} names[] = {
674b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BUFFER_SIZE),
684b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_SIZE),
694b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BLUE_SIZE),
704b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_GREEN_SIZE),
714b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RED_SIZE),
724b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_DEPTH_SIZE),
734b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_STENCIL_SIZE),
744b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_CAVEAT),
754b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_ID),
764b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LEVEL),
774b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_HEIGHT),
784b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_PIXELS),
794b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_WIDTH),
804b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_RENDERABLE),
814b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_ID),
824b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_TYPE),
834b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLES),
844b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLE_BUFFERS),
854b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SURFACE_TYPE),
864b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_TYPE),
874b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_RED_VALUE),
884b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_GREEN_VALUE),
894b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_BLUE_VALUE),
904b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGB),
914b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGBA),
924b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MIN_SWAP_INTERVAL),
934b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_SWAP_INTERVAL),
944b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LUMINANCE_SIZE),
954b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_MASK_SIZE),
964b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_COLOR_BUFFER_TYPE),
974b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RENDERABLE_TYPE),
984b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFORMANT),
994b3de47071d875faaa7d419d050a464b09538797Jason Sams   };
1004b3de47071d875faaa7d419d050a464b09538797Jason Sams#undef X
1014b3de47071d875faaa7d419d050a464b09538797Jason Sams
1024b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
1034b3de47071d875faaa7d419d050a464b09538797Jason Sams        EGLint value = -1;
1044b3de47071d875faaa7d419d050a464b09538797Jason Sams        EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
1054b3de47071d875faaa7d419d050a464b09538797Jason Sams        EGLint error = eglGetError();
1064b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (returnVal && error == EGL_SUCCESS) {
1074b3de47071d875faaa7d419d050a464b09538797Jason Sams            LOGV(" %s: %d (0x%x)", names[j].name, value, value);
1084b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1094b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1104b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1114b3de47071d875faaa7d419d050a464b09538797Jason Sams
11287fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Samsstatic void DumpDebug(RsdHal *dc) {
1134b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" EGL ver %i %i", dc->gl.egl.majorVersion, dc->gl.egl.minorVersion);
1144b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" EGL context %p  surface %p,  Display=%p", dc->gl.egl.context, dc->gl.egl.surface,
1154b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.egl.display);
1164b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" GL vendor: %s", dc->gl.gl.vendor);
1174b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" GL renderer: %s", dc->gl.gl.renderer);
1184b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" GL Version: %s", dc->gl.gl.version);
1194b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" GL Extensions: %s", dc->gl.gl.extensions);
1204b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGE(" GL int Versions %i %i", dc->gl.gl.majorVersion, dc->gl.gl.minorVersion);
1214b3de47071d875faaa7d419d050a464b09538797Jason Sams
1224b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("MAX Textures %i, %i  %i", dc->gl.gl.maxVertexTextureUnits,
1234b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentTextureImageUnits, dc->gl.gl.maxTextureImageUnits);
1244b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("MAX Attribs %i", dc->gl.gl.maxVertexAttribs);
1254b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("MAX Uniforms %i, %i", dc->gl.gl.maxVertexUniformVectors,
1264b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentUniformVectors);
1274b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("MAX Varyings %i", dc->gl.gl.maxVaryingVectors);
1284b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1294b3de47071d875faaa7d419d050a464b09538797Jason Sams
1304b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLShutdown(const Context *rsc) {
13187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
1324b3de47071d875faaa7d419d050a464b09538797Jason Sams
133a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache->cleanupAll();
134a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.shaderCache;
135a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
136a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.vertexArrayState;
137a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
1384b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("%p, deinitEGL", rsc);
1394b3de47071d875faaa7d419d050a464b09538797Jason Sams
1404b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context != EGL_NO_CONTEXT) {
1414b3de47071d875faaa7d419d050a464b09538797Jason Sams        eglMakeCurrent(dc->gl.egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1424b3de47071d875faaa7d419d050a464b09538797Jason Sams        eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surfaceDefault);
1434b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface != EGL_NO_SURFACE) {
1444b3de47071d875faaa7d419d050a464b09538797Jason Sams            eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
1454b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1464b3de47071d875faaa7d419d050a464b09538797Jason Sams        eglDestroyContext(dc->gl.egl.display, dc->gl.egl.context);
1474b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroyContext");
1484b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1494b3de47071d875faaa7d419d050a464b09538797Jason Sams
1504b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount--;
1514b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!gGLContextCount) {
1524b3de47071d875faaa7d419d050a464b09538797Jason Sams        eglTerminate(dc->gl.egl.display);
1534b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1544b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1554b3de47071d875faaa7d419d050a464b09538797Jason Sams
1564b3de47071d875faaa7d419d050a464b09538797Jason Samsbool rsdGLInit(const Context *rsc) {
15787fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
1584b3de47071d875faaa7d419d050a464b09538797Jason Sams
1594b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.numConfigs = -1;
1604b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint configAttribs[128];
1614b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint *configAttribsPtr = configAttribs;
1624b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
1634b3de47071d875faaa7d419d050a464b09538797Jason Sams
1644b3de47071d875faaa7d419d050a464b09538797Jason Sams    memset(configAttribs, 0, sizeof(configAttribs));
1654b3de47071d875faaa7d419d050a464b09538797Jason Sams
1664b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_SURFACE_TYPE;
1674b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[1] = EGL_WINDOW_BIT;
1684b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1694b3de47071d875faaa7d419d050a464b09538797Jason Sams
1704b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
1714b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
1724b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1734b3de47071d875faaa7d419d050a464b09538797Jason Sams
1744b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mUserSurfaceConfig.depthMin > 0) {
1754b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_DEPTH_SIZE;
1764b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = rsc->mUserSurfaceConfig.depthMin;
1774b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
1784b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1794b3de47071d875faaa7d419d050a464b09538797Jason Sams
1804b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mDev->mForceSW) {
1814b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
1824b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = EGL_SLOW_CONFIG;
1834b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
1844b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1854b3de47071d875faaa7d419d050a464b09538797Jason Sams
1864b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_NONE;
1874b3de47071d875faaa7d419d050a464b09538797Jason Sams    rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
1884b3de47071d875faaa7d419d050a464b09538797Jason Sams
1894b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("%p initEGL start", rsc);
1904b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1914b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglGetDisplay");
1924b3de47071d875faaa7d419d050a464b09538797Jason Sams
1934b3de47071d875faaa7d419d050a464b09538797Jason Sams    eglInitialize(dc->gl.egl.display, &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
1944b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglInitialize");
1954b3de47071d875faaa7d419d050a464b09538797Jason Sams
1964b3de47071d875faaa7d419d050a464b09538797Jason Sams    PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
1974b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mUserSurfaceConfig.alphaMin == 0) {
1984b3de47071d875faaa7d419d050a464b09538797Jason Sams        pf = PIXEL_FORMAT_RGBX_8888;
1994b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2004b3de47071d875faaa7d419d050a464b09538797Jason Sams
2014b3de47071d875faaa7d419d050a464b09538797Jason Sams    status_t err = EGLUtils::selectConfigForPixelFormat(dc->gl.egl.display, configAttribs,
2024b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                        pf, &dc->gl.egl.config);
2034b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (err) {
2044b3de47071d875faaa7d419d050a464b09538797Jason Sams       LOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
2054b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2064b3de47071d875faaa7d419d050a464b09538797Jason Sams    //if (props.mLogVisual) {
2079db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
2084b3de47071d875faaa7d419d050a464b09538797Jason Sams        printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
2099db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
2104b3de47071d875faaa7d419d050a464b09538797Jason Sams    //}
2114b3de47071d875faaa7d419d050a464b09538797Jason Sams
2124b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
2134b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          EGL_NO_CONTEXT, context_attribs2);
2144b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreateContext");
2154b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context == EGL_NO_CONTEXT) {
2164b3de47071d875faaa7d419d050a464b09538797Jason Sams        LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
2174b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
2184b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2194b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount++;
2204b3de47071d875faaa7d419d050a464b09538797Jason Sams
2214b3de47071d875faaa7d419d050a464b09538797Jason Sams
2224b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
2234b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
2244b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                        pbuffer_attribs);
2254b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreatePbufferSurface");
2264b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
2274b3de47071d875faaa7d419d050a464b09538797Jason Sams        LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
2284b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
2294b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
2304b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2314b3de47071d875faaa7d419d050a464b09538797Jason Sams
2324b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLBoolean ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
2334b3de47071d875faaa7d419d050a464b09538797Jason Sams                                    dc->gl.egl.surfaceDefault, dc->gl.egl.context);
2344b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (ret == EGL_FALSE) {
2354b3de47071d875faaa7d419d050a464b09538797Jason Sams        LOGE("eglMakeCurrent returned EGL_FALSE");
2364b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
2374b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
2384b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
2394b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2404b3de47071d875faaa7d419d050a464b09538797Jason Sams
2414b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.version = glGetString(GL_VERSION);
2424b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.vendor = glGetString(GL_VENDOR);
2434b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.renderer = glGetString(GL_RENDERER);
2444b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.extensions = glGetString(GL_EXTENSIONS);
2454b3de47071d875faaa7d419d050a464b09538797Jason Sams
2464b3de47071d875faaa7d419d050a464b09538797Jason Sams    //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
2474b3de47071d875faaa7d419d050a464b09538797Jason Sams    //LOGV("GL Version %s", mGL.mVersion);
2484b3de47071d875faaa7d419d050a464b09538797Jason Sams    //LOGV("GL Vendor %s", mGL.mVendor);
2494b3de47071d875faaa7d419d050a464b09538797Jason Sams    //LOGV("GL Renderer %s", mGL.mRenderer);
2504b3de47071d875faaa7d419d050a464b09538797Jason Sams    //LOGV("GL Extensions %s", mGL.mExtensions);
2514b3de47071d875faaa7d419d050a464b09538797Jason Sams
2524b3de47071d875faaa7d419d050a464b09538797Jason Sams    const char *verptr = NULL;
2534b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (strlen((const char *)dc->gl.gl.version) > 9) {
2544b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES-CM", 12)) {
2554b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 12;
2564b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
2574b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES ", 10)) {
2584b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 9;
2594b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
2604b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2614b3de47071d875faaa7d419d050a464b09538797Jason Sams
2624b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!verptr) {
2634b3de47071d875faaa7d419d050a464b09538797Jason Sams        LOGE("Error, OpenGL ES Lite not supported");
2644b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
2654b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
2664b3de47071d875faaa7d419d050a464b09538797Jason Sams    } else {
2674b3de47071d875faaa7d419d050a464b09538797Jason Sams        sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
2684b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2694b3de47071d875faaa7d419d050a464b09538797Jason Sams
2704b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &dc->gl.gl.maxVertexAttribs);
2714b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &dc->gl.gl.maxVertexUniformVectors);
2724b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxVertexTextureUnits);
2734b3de47071d875faaa7d419d050a464b09538797Jason Sams
2744b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VARYING_VECTORS, &dc->gl.gl.maxVaryingVectors);
2754b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxTextureImageUnits);
2764b3de47071d875faaa7d419d050a464b09538797Jason Sams
2774b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxFragmentTextureImageUnits);
2784b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &dc->gl.gl.maxFragmentUniformVectors);
2794b3de47071d875faaa7d419d050a464b09538797Jason Sams
2804b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.OES_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
2814b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                "GL_OES_texture_npot");
2824b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.GL_IMG_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
2834b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                   "GL_IMG_texture_npot");
2844b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)dc->gl.gl.extensions,
2854b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                            "GL_NV_texture_npot_2D_mipmap");
2864b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.EXT_texture_max_aniso = 1.0f;
2874b3de47071d875faaa7d419d050a464b09538797Jason Sams    bool hasAniso = NULL != strstr((const char *)dc->gl.gl.extensions,
2884b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   "GL_EXT_texture_filter_anisotropic");
2894b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (hasAniso) {
2904b3de47071d875faaa7d419d050a464b09538797Jason Sams        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &dc->gl.gl.EXT_texture_max_aniso);
2914b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2924b3de47071d875faaa7d419d050a464b09538797Jason Sams
2939db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
2949db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines        DumpDebug(dc);
2959db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
2964b3de47071d875faaa7d419d050a464b09538797Jason Sams
297a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache = new RsdShaderCache();
298a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState = new RsdVertexArrayState();
299a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs);
300a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
3014b3de47071d875faaa7d419d050a464b09538797Jason Sams    LOGV("initGLThread end %p", rsc);
3024b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
3034b3de47071d875faaa7d419d050a464b09538797Jason Sams}
3044b3de47071d875faaa7d419d050a464b09538797Jason Sams
3054b3de47071d875faaa7d419d050a464b09538797Jason Sams
3064b3de47071d875faaa7d419d050a464b09538797Jason Samsbool rsdGLSetSurface(const Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
30787fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
3084b3de47071d875faaa7d419d050a464b09538797Jason Sams
3094b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLBoolean ret;
3104b3de47071d875faaa7d419d050a464b09538797Jason Sams    // WAR: Some drivers fail to handle 0 size surfaces correcntly.
3114b3de47071d875faaa7d419d050a464b09538797Jason Sams    // Use the pbuffer to avoid this pitfall.
3124b3de47071d875faaa7d419d050a464b09538797Jason Sams    if ((dc->gl.egl.surface != NULL) || (w == 0) || (h == 0)) {
3134b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
3144b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surfaceDefault, dc->gl.egl.context);
3154b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
3164b3de47071d875faaa7d419d050a464b09538797Jason Sams
3174b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
3184b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroySurface", ret);
3194b3de47071d875faaa7d419d050a464b09538797Jason Sams
3204b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = NULL;
3214b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = 1;
3224b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = 1;
3234b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3244b3de47071d875faaa7d419d050a464b09538797Jason Sams
3254b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.wndSurface = sur;
3264b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.wndSurface != NULL) {
3274b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = w;
3284b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = h;
3294b3de47071d875faaa7d419d050a464b09538797Jason Sams
3304b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
3314b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                    dc->gl.wndSurface, NULL);
3324b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglCreateWindowSurface");
3334b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface == EGL_NO_SURFACE) {
3344b3de47071d875faaa7d419d050a464b09538797Jason Sams            LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
3354b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3364b3de47071d875faaa7d419d050a464b09538797Jason Sams
3374b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
3384b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surface, dc->gl.egl.context);
3394b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
3404b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3414b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
3424b3de47071d875faaa7d419d050a464b09538797Jason Sams}
3434b3de47071d875faaa7d419d050a464b09538797Jason Sams
3444b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLSwap(const android::renderscript::Context *rsc) {
34587fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
3464b3de47071d875faaa7d419d050a464b09538797Jason Sams    eglSwapBuffers(dc->gl.egl.display, dc->gl.egl.surface);
3474b3de47071d875faaa7d419d050a464b09538797Jason Sams}
3484b3de47071d875faaa7d419d050a464b09538797Jason Sams
349