rsdGL.cpp revision e255929025c4ec3fd91518350e3e7b447c745529
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/egl/android_natives.h>
204b3de47071d875faaa7d419d050a464b09538797Jason Sams
214b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sys/types.h>
224b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sys/resource.h>
234b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <sched.h>
244b3de47071d875faaa7d419d050a464b09538797Jason Sams
254b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <cutils/properties.h>
264b3de47071d875faaa7d419d050a464b09538797Jason Sams
274b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES/gl.h>
284b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES/glext.h>
294b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES2/gl2.h>
304b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <GLES2/gl2ext.h>
314b3de47071d875faaa7d419d050a464b09538797Jason Sams
324b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <string.h>
334b3de47071d875faaa7d419d050a464b09538797Jason Sams
344b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdCore.h"
354b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdGL.h"
364b3de47071d875faaa7d419d050a464b09538797Jason Sams
374b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <malloc.h>
384b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsContext.h"
39a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdShaderCache.h"
40a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdVertexArray.h"
41a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk#include "rsdFrameBufferObj.h"
424b3de47071d875faaa7d419d050a464b09538797Jason Sams
434b3de47071d875faaa7d419d050a464b09538797Jason Samsusing namespace android;
444b3de47071d875faaa7d419d050a464b09538797Jason Samsusing namespace android::renderscript;
454b3de47071d875faaa7d419d050a464b09538797Jason Sams
464b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic int32_t gGLContextCount = 0;
474b3de47071d875faaa7d419d050a464b09538797Jason Sams
484b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
49e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian    struct EGLUtils {
50e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian        static const char *strerror(EGLint err) {
51e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian            switch (err){
52e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_SUCCESS:           return "EGL_SUCCESS";
53e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
54e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
55e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
56e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
57e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
58e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
59e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
60e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
61e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
62e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
63e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
64e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
65e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
66e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
67e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                default: return "UNKNOWN";
68e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian            }
69e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian        }
70e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian    };
71e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
724b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (returnVal != EGL_TRUE) {
734b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "%s() returned %d\n", op, returnVal);
744b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
754b3de47071d875faaa7d419d050a464b09538797Jason Sams
764b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
774b3de47071d875faaa7d419d050a464b09538797Jason Sams            = eglGetError()) {
784b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
794b3de47071d875faaa7d419d050a464b09538797Jason Sams                error);
804b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
814b3de47071d875faaa7d419d050a464b09538797Jason Sams}
824b3de47071d875faaa7d419d050a464b09538797Jason Sams
834b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
844b3de47071d875faaa7d419d050a464b09538797Jason Sams
854b3de47071d875faaa7d419d050a464b09538797Jason Sams#define X(VAL) {VAL, #VAL}
864b3de47071d875faaa7d419d050a464b09538797Jason Sams    struct {EGLint attribute; const char* name;} names[] = {
874b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BUFFER_SIZE),
884b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_SIZE),
894b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BLUE_SIZE),
904b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_GREEN_SIZE),
914b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RED_SIZE),
924b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_DEPTH_SIZE),
934b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_STENCIL_SIZE),
944b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_CAVEAT),
954b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_ID),
964b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LEVEL),
974b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_HEIGHT),
984b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_PIXELS),
994b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_WIDTH),
1004b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_RENDERABLE),
1014b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_ID),
1024b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_TYPE),
1034b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLES),
1044b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLE_BUFFERS),
1054b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SURFACE_TYPE),
1064b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_TYPE),
1074b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_RED_VALUE),
1084b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_GREEN_VALUE),
1094b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_BLUE_VALUE),
1104b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGB),
1114b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGBA),
1124b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MIN_SWAP_INTERVAL),
1134b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_SWAP_INTERVAL),
1144b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LUMINANCE_SIZE),
1154b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_MASK_SIZE),
1164b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_COLOR_BUFFER_TYPE),
1174b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RENDERABLE_TYPE),
1184b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFORMANT),
1194b3de47071d875faaa7d419d050a464b09538797Jason Sams   };
1204b3de47071d875faaa7d419d050a464b09538797Jason Sams#undef X
1214b3de47071d875faaa7d419d050a464b09538797Jason Sams
1224b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
1234b3de47071d875faaa7d419d050a464b09538797Jason Sams        EGLint value = -1;
12467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        EGLBoolean returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
12567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        if (returnVal) {
1266598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block            ALOGV(" %s: %d (0x%x)", names[j].name, value, value);
1274b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1284b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1294b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1304b3de47071d875faaa7d419d050a464b09538797Jason Sams
13187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Samsstatic void DumpDebug(RsdHal *dc) {
132af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" EGL ver %i %i", dc->gl.egl.majorVersion, dc->gl.egl.minorVersion);
133af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" EGL context %p  surface %p,  Display=%p", dc->gl.egl.context, dc->gl.egl.surface,
1344b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.egl.display);
135af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL vendor: %s", dc->gl.gl.vendor);
136af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL renderer: %s", dc->gl.gl.renderer);
137af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL Version: %s", dc->gl.gl.version);
138af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL Extensions: %s", dc->gl.gl.extensions);
139af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL int Versions %i %i", dc->gl.gl.majorVersion, dc->gl.gl.minorVersion);
1404b3de47071d875faaa7d419d050a464b09538797Jason Sams
1416598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Textures %i, %i  %i", dc->gl.gl.maxVertexTextureUnits,
1424b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentTextureImageUnits, dc->gl.gl.maxTextureImageUnits);
1436598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Attribs %i", dc->gl.gl.maxVertexAttribs);
1446598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Uniforms %i, %i", dc->gl.gl.maxVertexUniformVectors,
1454b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentUniformVectors);
1466598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Varyings %i", dc->gl.gl.maxVaryingVectors);
1474b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1484b3de47071d875faaa7d419d050a464b09538797Jason Sams
1494b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLShutdown(const Context *rsc) {
15087fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
1514b3de47071d875faaa7d419d050a464b09538797Jason Sams
152a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache->cleanupAll();
153a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.shaderCache;
154a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.vertexArrayState;
155a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
1564b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context != EGL_NO_CONTEXT) {
1572382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglMakeCurrent, dc->gl.egl.display,
1582382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                    EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1592382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surfaceDefault);
1604b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface != EGL_NO_SURFACE) {
1612382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surface);
1624b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1632382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroyContext, dc->gl.egl.display, dc->gl.egl.context);
1644b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroyContext");
1654b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1664b3de47071d875faaa7d419d050a464b09538797Jason Sams
1674b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount--;
1684b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!gGLContextCount) {
1692382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglTerminate, dc->gl.egl.display);
1704b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1714b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1724b3de47071d875faaa7d419d050a464b09538797Jason Sams
1734b3de47071d875faaa7d419d050a464b09538797Jason Samsbool rsdGLInit(const Context *rsc) {
17487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
1754b3de47071d875faaa7d419d050a464b09538797Jason Sams
1764b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.numConfigs = -1;
1774b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint configAttribs[128];
1784b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint *configAttribsPtr = configAttribs;
1794b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
1804b3de47071d875faaa7d419d050a464b09538797Jason Sams
1814b3de47071d875faaa7d419d050a464b09538797Jason Sams    memset(configAttribs, 0, sizeof(configAttribs));
1824b3de47071d875faaa7d419d050a464b09538797Jason Sams
1834b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_SURFACE_TYPE;
1844b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[1] = EGL_WINDOW_BIT;
1854b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1864b3de47071d875faaa7d419d050a464b09538797Jason Sams
1874b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
1884b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
1894b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1904b3de47071d875faaa7d419d050a464b09538797Jason Sams
19167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_RED_SIZE;
19267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
19367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
19467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
19567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_GREEN_SIZE;
19667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
19767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
19867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
19967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_BLUE_SIZE;
20067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
20167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
20267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
20367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    if (rsc->mUserSurfaceConfig.alphaMin > 0) {
20467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr[0] = EGL_ALPHA_SIZE;
20567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr[1] = rsc->mUserSurfaceConfig.alphaMin;
20667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr += 2;
20767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    }
20867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2094b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mUserSurfaceConfig.depthMin > 0) {
2104b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_DEPTH_SIZE;
2114b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = rsc->mUserSurfaceConfig.depthMin;
2124b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
2134b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2144b3de47071d875faaa7d419d050a464b09538797Jason Sams
2154b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mDev->mForceSW) {
2164b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
2174b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = EGL_SLOW_CONFIG;
2184b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
2194b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2204b3de47071d875faaa7d419d050a464b09538797Jason Sams
2214b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_NONE;
2224b3de47071d875faaa7d419d050a464b09538797Jason Sams    rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
2234b3de47071d875faaa7d419d050a464b09538797Jason Sams
2246598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initEGL start", rsc);
2252382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglGetDisplay", __LINE__, __FILE__);
2264b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
2274b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglGetDisplay");
2284b3de47071d875faaa7d419d050a464b09538797Jason Sams
2292382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglInitialize, dc->gl.egl.display,
2302382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
2314b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglInitialize");
2324b3de47071d875faaa7d419d050a464b09538797Jason Sams
23367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLBoolean ret;
23467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
23567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLint numConfigs = -1, n = 0;
2362382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
23767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
23867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    checkEglError("eglGetConfigs", ret);
23967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2405f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    eglSwapInterval(dc->gl.egl.display, 0);
2415f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
24267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    if (numConfigs) {
24367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        EGLConfig* const configs = new EGLConfig[numConfigs];
24467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2452382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
24667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        ret = eglChooseConfig(dc->gl.egl.display,
24767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                configAttribs, configs, numConfigs, &n);
24867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        if (!ret || !n) {
24967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            checkEglError("eglChooseConfig", ret);
250af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
25167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
25267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
25367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // The first config is guaranteed to over-satisfy the constraints
25467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        dc->gl.egl.config = configs[0];
25567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
25667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // go through the list and skip configs that over-satisfy our needs
25767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        for (int i=0 ; i<n ; i++) {
25867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.alphaMin <= 0) {
25967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint alphaSize;
26067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
26167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_ALPHA_SIZE, &alphaSize);
26267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (alphaSize > 0) {
26367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
26467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
26567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
26667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
26767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.depthMin <= 0) {
26867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint depthSize;
26967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
27067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_DEPTH_SIZE, &depthSize);
27167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (depthSize > 0) {
27267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
27367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
27467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
27567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
27667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // Found one!
27767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            dc->gl.egl.config = configs[i];
27867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            break;
27967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
2804b3de47071d875faaa7d419d050a464b09538797Jason Sams
28167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        delete [] configs;
2824b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
28367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2844b3de47071d875faaa7d419d050a464b09538797Jason Sams    //if (props.mLogVisual) {
2859db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
2864b3de47071d875faaa7d419d050a464b09538797Jason Sams        printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
2879db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
2884b3de47071d875faaa7d419d050a464b09538797Jason Sams    //}
2894b3de47071d875faaa7d419d050a464b09538797Jason Sams
2902382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglCreateContext", __LINE__, __FILE__);
2914b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
2924b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          EGL_NO_CONTEXT, context_attribs2);
2934b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreateContext");
2944b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context == EGL_NO_CONTEXT) {
295af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
2962382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
2974b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
2984b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2994b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount++;
3004b3de47071d875faaa7d419d050a464b09538797Jason Sams
3014b3de47071d875faaa7d419d050a464b09538797Jason Sams
3024b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
3032382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglCreatePbufferSurface", __LINE__, __FILE__);
3044b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
3054b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                        pbuffer_attribs);
3064b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreatePbufferSurface");
3074b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
308af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
3094b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3102382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3114b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3124b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3134b3de47071d875faaa7d419d050a464b09538797Jason Sams
3142382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
31567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
31667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                         dc->gl.egl.surfaceDefault, dc->gl.egl.context);
3174b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (ret == EGL_FALSE) {
318af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("eglMakeCurrent returned EGL_FALSE");
3194b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
3204b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3212382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3224b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3234b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3244b3de47071d875faaa7d419d050a464b09538797Jason Sams
3254b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.version = glGetString(GL_VERSION);
3264b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.vendor = glGetString(GL_VENDOR);
3274b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.renderer = glGetString(GL_RENDERER);
3284b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.extensions = glGetString(GL_EXTENSIONS);
3294b3de47071d875faaa7d419d050a464b09538797Jason Sams
3306598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
3316598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Version %s", mGL.mVersion);
3326598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Vendor %s", mGL.mVendor);
3336598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Renderer %s", mGL.mRenderer);
3346598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Extensions %s", mGL.mExtensions);
3354b3de47071d875faaa7d419d050a464b09538797Jason Sams
3364b3de47071d875faaa7d419d050a464b09538797Jason Sams    const char *verptr = NULL;
3374b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (strlen((const char *)dc->gl.gl.version) > 9) {
3384b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES-CM", 12)) {
3394b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 12;
3404b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3414b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES ", 10)) {
3424b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 9;
3434b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3444b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3454b3de47071d875faaa7d419d050a464b09538797Jason Sams
3464b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!verptr) {
347af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Error, OpenGL ES Lite not supported");
3484b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3492382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3504b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3514b3de47071d875faaa7d419d050a464b09538797Jason Sams    } else {
3524b3de47071d875faaa7d419d050a464b09538797Jason Sams        sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
3534b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3544b3de47071d875faaa7d419d050a464b09538797Jason Sams
3554b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &dc->gl.gl.maxVertexAttribs);
3564b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &dc->gl.gl.maxVertexUniformVectors);
3574b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxVertexTextureUnits);
3584b3de47071d875faaa7d419d050a464b09538797Jason Sams
3594b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VARYING_VECTORS, &dc->gl.gl.maxVaryingVectors);
3604b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxTextureImageUnits);
3614b3de47071d875faaa7d419d050a464b09538797Jason Sams
3624b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxFragmentTextureImageUnits);
3634b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &dc->gl.gl.maxFragmentUniformVectors);
3644b3de47071d875faaa7d419d050a464b09538797Jason Sams
3654b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.OES_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
3664b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                "GL_OES_texture_npot");
367917027582745bbf27335a70360a4e6da51961978Mathias Agopian    dc->gl.gl.IMG_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
3684b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                   "GL_IMG_texture_npot");
369917027582745bbf27335a70360a4e6da51961978Mathias Agopian    dc->gl.gl.NV_texture_npot_2D_mipmap = NULL != strstr((const char *)dc->gl.gl.extensions,
3704b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                            "GL_NV_texture_npot_2D_mipmap");
3714b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.EXT_texture_max_aniso = 1.0f;
3724b3de47071d875faaa7d419d050a464b09538797Jason Sams    bool hasAniso = NULL != strstr((const char *)dc->gl.gl.extensions,
3734b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   "GL_EXT_texture_filter_anisotropic");
3744b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (hasAniso) {
3754b3de47071d875faaa7d419d050a464b09538797Jason Sams        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &dc->gl.gl.EXT_texture_max_aniso);
3764b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3774b3de47071d875faaa7d419d050a464b09538797Jason Sams
3789db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
3799db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines        DumpDebug(dc);
3809db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
3814b3de47071d875faaa7d419d050a464b09538797Jason Sams
382a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache = new RsdShaderCache();
383a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState = new RsdVertexArrayState();
384a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs);
385a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    dc->gl.currentFrameBuffer = NULL;
3869719bd4a0187c400ba868712612fe66da4635aacJason Sams    dc->mHasGraphics = true;
387a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
3886598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initGLThread end", rsc);
3892382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL(NULL, 0, NULL);
3904b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
3914b3de47071d875faaa7d419d050a464b09538797Jason Sams}
3924b3de47071d875faaa7d419d050a464b09538797Jason Sams
3934b3de47071d875faaa7d419d050a464b09538797Jason Sams
3947257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchoukbool rsdGLSetSurface(const Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
39587fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
3964b3de47071d875faaa7d419d050a464b09538797Jason Sams
3974b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLBoolean ret;
3984b3de47071d875faaa7d419d050a464b09538797Jason Sams    // WAR: Some drivers fail to handle 0 size surfaces correcntly.
3994b3de47071d875faaa7d419d050a464b09538797Jason Sams    // Use the pbuffer to avoid this pitfall.
4004b3de47071d875faaa7d419d050a464b09538797Jason Sams    if ((dc->gl.egl.surface != NULL) || (w == 0) || (h == 0)) {
4012382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4024b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
4034b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surfaceDefault, dc->gl.egl.context);
4044b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4054b3de47071d875faaa7d419d050a464b09538797Jason Sams
4062382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglDestroySurface", __LINE__, __FILE__);
4074b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
4084b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroySurface", ret);
4094b3de47071d875faaa7d419d050a464b09538797Jason Sams
4104b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = NULL;
4114b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = 1;
4124b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = 1;
4134b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4144b3de47071d875faaa7d419d050a464b09538797Jason Sams
415c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams    if (dc->gl.wndSurface != NULL) {
416c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        dc->gl.wndSurface->decStrong(NULL);
417c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams    }
418c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams
4197257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    dc->gl.wndSurface = (ANativeWindow *)sur;
4204b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.wndSurface != NULL) {
421c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        dc->gl.wndSurface->incStrong(NULL);
4224b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = w;
4234b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = h;
4244b3de47071d875faaa7d419d050a464b09538797Jason Sams
4252382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglCreateWindowSurface", __LINE__, __FILE__);
4264b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
4274b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                    dc->gl.wndSurface, NULL);
4284b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglCreateWindowSurface");
4294b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface == EGL_NO_SURFACE) {
430af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
4314b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
4324b3de47071d875faaa7d419d050a464b09538797Jason Sams
4332382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4344b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
4354b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surface, dc->gl.egl.context);
4364b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4374b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4382382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL(NULL, 0, NULL);
4394b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
4404b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4414b3de47071d875faaa7d419d050a464b09538797Jason Sams
4424b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLSwap(const android::renderscript::Context *rsc) {
44387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
4442382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
4454b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4464b3de47071d875faaa7d419d050a464b09538797Jason Sams
4479719bd4a0187c400ba868712612fe66da4635aacJason Samsvoid rsdGLSetPriority(const Context *rsc, int32_t priority) {
4489719bd4a0187c400ba868712612fe66da4635aacJason Sams    if (priority > 0) {
4499719bd4a0187c400ba868712612fe66da4635aacJason Sams        // Mark context as low priority.
4509719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("low pri");
4519719bd4a0187c400ba868712612fe66da4635aacJason Sams    } else {
4529719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("normal pri");
4539719bd4a0187c400ba868712612fe66da4635aacJason Sams    }
4549719bd4a0187c400ba868712612fe66da4635aacJason Sams}
4559719bd4a0187c400ba868712612fe66da4635aacJason Sams
456c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchoukvoid rsdGLCheckError(const android::renderscript::Context *rsc,
457c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                     const char *msg, bool isFatal) {
458c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    GLenum err = glGetError();
459c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    if (err != GL_NO_ERROR) {
460c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        char buf[1024];
461c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
462c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
463c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        if (isFatal) {
464c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
465c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        } else {
466c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            switch (err) {
467c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            case GL_OUT_OF_MEMORY:
468c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_OUT_OF_MEMORY, buf);
469c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
470c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            default:
471c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_DRIVER, buf);
472c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
473c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            }
474c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        }
475c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
476af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, %s", rsc, buf);
477c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    }
478c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
479c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk}
480