rsdGL.cpp revision da7a148171869f392b5813635532c6ed5a752912
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>
19b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian
20b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian#include <system/window.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 <string.h>
344b3de47071d875faaa7d419d050a464b09538797Jason Sams
354b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdCore.h"
364b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsdGL.h"
374b3de47071d875faaa7d419d050a464b09538797Jason Sams
384b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <malloc.h>
394b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "rsContext.h"
40a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdShaderCache.h"
41a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdVertexArray.h"
42a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk#include "rsdFrameBufferObj.h"
434b3de47071d875faaa7d419d050a464b09538797Jason Sams
445575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk#include <gui/SurfaceTextureClient.h>
455575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk
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) {
52e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian    struct EGLUtils {
53e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian        static const char *strerror(EGLint err) {
54e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian            switch (err){
55e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_SUCCESS:           return "EGL_SUCCESS";
56e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
57e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
58e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
59e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
60e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
61e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
62e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
63e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
64e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
65e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
66e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
67e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
68e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
69e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
70e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                default: return "UNKNOWN";
71e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian            }
72e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian        }
73e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian    };
74e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
754b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (returnVal != EGL_TRUE) {
764b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "%s() returned %d\n", op, returnVal);
774b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
784b3de47071d875faaa7d419d050a464b09538797Jason Sams
794b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
804b3de47071d875faaa7d419d050a464b09538797Jason Sams            = eglGetError()) {
814b3de47071d875faaa7d419d050a464b09538797Jason Sams        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
824b3de47071d875faaa7d419d050a464b09538797Jason Sams                error);
834b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
844b3de47071d875faaa7d419d050a464b09538797Jason Sams}
854b3de47071d875faaa7d419d050a464b09538797Jason Sams
864b3de47071d875faaa7d419d050a464b09538797Jason Samsstatic void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
874b3de47071d875faaa7d419d050a464b09538797Jason Sams
884b3de47071d875faaa7d419d050a464b09538797Jason Sams#define X(VAL) {VAL, #VAL}
894b3de47071d875faaa7d419d050a464b09538797Jason Sams    struct {EGLint attribute; const char* name;} names[] = {
904b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BUFFER_SIZE),
914b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_SIZE),
924b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BLUE_SIZE),
934b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_GREEN_SIZE),
944b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RED_SIZE),
954b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_DEPTH_SIZE),
964b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_STENCIL_SIZE),
974b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_CAVEAT),
984b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFIG_ID),
994b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LEVEL),
1004b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_HEIGHT),
1014b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_PIXELS),
1024b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_PBUFFER_WIDTH),
1034b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_RENDERABLE),
1044b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_ID),
1054b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_NATIVE_VISUAL_TYPE),
1064b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLES),
1074b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SAMPLE_BUFFERS),
1084b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_SURFACE_TYPE),
1094b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_TYPE),
1104b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_RED_VALUE),
1114b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_GREEN_VALUE),
1124b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_TRANSPARENT_BLUE_VALUE),
1134b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGB),
1144b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_BIND_TO_TEXTURE_RGBA),
1154b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MIN_SWAP_INTERVAL),
1164b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_MAX_SWAP_INTERVAL),
1174b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_LUMINANCE_SIZE),
1184b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_ALPHA_MASK_SIZE),
1194b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_COLOR_BUFFER_TYPE),
1204b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_RENDERABLE_TYPE),
1214b3de47071d875faaa7d419d050a464b09538797Jason Sams    X(EGL_CONFORMANT),
1224b3de47071d875faaa7d419d050a464b09538797Jason Sams   };
1234b3de47071d875faaa7d419d050a464b09538797Jason Sams#undef X
1244b3de47071d875faaa7d419d050a464b09538797Jason Sams
1254b3de47071d875faaa7d419d050a464b09538797Jason Sams    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
1264b3de47071d875faaa7d419d050a464b09538797Jason Sams        EGLint value = -1;
12767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        EGLBoolean returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
12867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        if (returnVal) {
1296598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block            ALOGV(" %s: %d (0x%x)", names[j].name, value, value);
1304b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1314b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1324b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1334b3de47071d875faaa7d419d050a464b09538797Jason Sams
13487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Samsstatic void DumpDebug(RsdHal *dc) {
135af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" EGL ver %i %i", dc->gl.egl.majorVersion, dc->gl.egl.minorVersion);
136af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" EGL context %p  surface %p,  Display=%p", dc->gl.egl.context, dc->gl.egl.surface,
1374b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.egl.display);
138af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL vendor: %s", dc->gl.gl.vendor);
139af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL renderer: %s", dc->gl.gl.renderer);
140af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL Version: %s", dc->gl.gl.version);
141af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL Extensions: %s", dc->gl.gl.extensions);
142af12ac6a08651464f8d823add667c706f993b587Steve Block    ALOGE(" GL int Versions %i %i", dc->gl.gl.majorVersion, dc->gl.gl.minorVersion);
1434b3de47071d875faaa7d419d050a464b09538797Jason Sams
1446598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Textures %i, %i  %i", dc->gl.gl.maxVertexTextureUnits,
1454b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentTextureImageUnits, dc->gl.gl.maxTextureImageUnits);
1466598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Attribs %i", dc->gl.gl.maxVertexAttribs);
1476598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Uniforms %i, %i", dc->gl.gl.maxVertexUniformVectors,
1484b3de47071d875faaa7d419d050a464b09538797Jason Sams         dc->gl.gl.maxFragmentUniformVectors);
1496598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("MAX Varyings %i", dc->gl.gl.maxVaryingVectors);
1504b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1514b3de47071d875faaa7d419d050a464b09538797Jason Sams
1524b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLShutdown(const Context *rsc) {
15387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
1544b3de47071d875faaa7d419d050a464b09538797Jason Sams
155a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache->cleanupAll();
156a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.shaderCache;
157a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.vertexArrayState;
158a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
1594b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context != EGL_NO_CONTEXT) {
1602382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglMakeCurrent, dc->gl.egl.display,
1612382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                    EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1622382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surfaceDefault);
1634b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface != EGL_NO_SURFACE) {
1642382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surface);
1654b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1662382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroyContext, dc->gl.egl.display, dc->gl.egl.context);
1674b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroyContext");
1684b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1694b3de47071d875faaa7d419d050a464b09538797Jason Sams
1704b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount--;
1714b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!gGLContextCount) {
1722382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglTerminate, dc->gl.egl.display);
1734b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1744b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1754b3de47071d875faaa7d419d050a464b09538797Jason Sams
176da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchoukvoid getConfigData(const Context *rsc,
177da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk                   EGLint *configAttribs, size_t configAttribsLen,
178da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk                   uint32_t numSamples) {
179da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    memset(configAttribs, 0, configAttribsLen*sizeof(*configAttribs));
1804b3de47071d875faaa7d419d050a464b09538797Jason Sams
1814b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint *configAttribsPtr = 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
221da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    if (numSamples > 1) {
222da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[0] = EGL_SAMPLE_BUFFERS;
223da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[1] = 1;
224da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[2] = EGL_SAMPLES;
225da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[3] = numSamples;
226da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr += 4;
227da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    }
228da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
2294b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_NONE;
230da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    rsAssert(configAttribsPtr < (configAttribs + configAttribsLen));
231da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk}
232da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
233da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchoukbool rsdGLInit(const Context *rsc) {
234da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
235da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
236da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    dc->gl.egl.numConfigs = -1;
237da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
238da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    EGLint configAttribs[128];
239da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
2404b3de47071d875faaa7d419d050a464b09538797Jason Sams
2416598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initEGL start", rsc);
2422382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglGetDisplay", __LINE__, __FILE__);
2434b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
2444b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglGetDisplay");
2454b3de47071d875faaa7d419d050a464b09538797Jason Sams
2462382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglInitialize, dc->gl.egl.display,
2472382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
2484b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglInitialize");
2494b3de47071d875faaa7d419d050a464b09538797Jason Sams
25067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLBoolean ret;
25167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
25267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLint numConfigs = -1, n = 0;
2532382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
254da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
255da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    // Try minding a multisample config that matches the user request
256da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    uint32_t minSample = rsc->mUserSurfaceConfig.samplesMin;
257da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    uint32_t prefSample = rsc->mUserSurfaceConfig.samplesPref;
258da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    for (uint32_t sampleCount = prefSample; sampleCount >= minSample; sampleCount--) {
259da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        getConfigData(rsc, configAttribs, (sizeof(configAttribs) / sizeof(EGLint)), sampleCount);
260da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
261da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        checkEglError("eglGetConfigs", ret);
262da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        if (numConfigs > 0) {
263da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk            break;
264da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        }
265da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    }
26667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2675f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams    eglSwapInterval(dc->gl.egl.display, 0);
2685f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
26967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    if (numConfigs) {
27067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        EGLConfig* const configs = new EGLConfig[numConfigs];
27167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2722382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
27367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        ret = eglChooseConfig(dc->gl.egl.display,
27467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                configAttribs, configs, numConfigs, &n);
27567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        if (!ret || !n) {
27667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            checkEglError("eglChooseConfig", ret);
277af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
27867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
27967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
28067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // The first config is guaranteed to over-satisfy the constraints
28167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        dc->gl.egl.config = configs[0];
28267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
28367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // go through the list and skip configs that over-satisfy our needs
28467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        for (int i=0 ; i<n ; i++) {
28567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.alphaMin <= 0) {
28667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint alphaSize;
28767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
28867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_ALPHA_SIZE, &alphaSize);
28967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (alphaSize > 0) {
29067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
29167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
29267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
29367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
29467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.depthMin <= 0) {
29567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint depthSize;
29667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
29767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_DEPTH_SIZE, &depthSize);
29867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (depthSize > 0) {
29967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
30067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
30167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
30267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
30367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // Found one!
30467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            dc->gl.egl.config = configs[i];
30567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            break;
30667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
3074b3de47071d875faaa7d419d050a464b09538797Jason Sams
30867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        delete [] configs;
3094b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
31067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
3114b3de47071d875faaa7d419d050a464b09538797Jason Sams    //if (props.mLogVisual) {
3129db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
3134b3de47071d875faaa7d419d050a464b09538797Jason Sams        printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
3149db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
3154b3de47071d875faaa7d419d050a464b09538797Jason Sams    //}
3164b3de47071d875faaa7d419d050a464b09538797Jason Sams
3172382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglCreateContext", __LINE__, __FILE__);
3184b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
3194b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          EGL_NO_CONTEXT, context_attribs2);
3204b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreateContext");
3214b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context == EGL_NO_CONTEXT) {
322af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
3232382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3244b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3254b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3264b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount++;
3274b3de47071d875faaa7d419d050a464b09538797Jason Sams
3285575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk    sp<SurfaceTexture> st(new SurfaceTexture(123));
3295575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk    sp<SurfaceTextureClient> stc(new SurfaceTextureClient(st));
3305575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk    dc->gl.egl.surfaceDefault = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
3315575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk                                                       static_cast<ANativeWindow*>(stc.get()),
3325575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk                                                       NULL);
3334b3de47071d875faaa7d419d050a464b09538797Jason Sams
3345575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk    checkEglError("eglCreateWindowSurface");
3354b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
3365575cf16fa6e263ead96526683aed2f2cfd8ac96Alex Sakhartchouk        ALOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
3374b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3382382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3394b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3404b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3414b3de47071d875faaa7d419d050a464b09538797Jason Sams
3422382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
34367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
34467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                         dc->gl.egl.surfaceDefault, dc->gl.egl.context);
3454b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (ret == EGL_FALSE) {
346af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("eglMakeCurrent returned EGL_FALSE");
3474b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
3484b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3492382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3504b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3514b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3524b3de47071d875faaa7d419d050a464b09538797Jason Sams
3534b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.version = glGetString(GL_VERSION);
3544b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.vendor = glGetString(GL_VENDOR);
3554b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.renderer = glGetString(GL_RENDERER);
3564b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.extensions = glGetString(GL_EXTENSIONS);
3574b3de47071d875faaa7d419d050a464b09538797Jason Sams
3586598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
3596598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Version %s", mGL.mVersion);
3606598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Vendor %s", mGL.mVendor);
3616598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Renderer %s", mGL.mRenderer);
3626598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Extensions %s", mGL.mExtensions);
3634b3de47071d875faaa7d419d050a464b09538797Jason Sams
3644b3de47071d875faaa7d419d050a464b09538797Jason Sams    const char *verptr = NULL;
3654b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (strlen((const char *)dc->gl.gl.version) > 9) {
3664b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES-CM", 12)) {
3674b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 12;
3684b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3694b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES ", 10)) {
3704b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 9;
3714b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3724b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3734b3de47071d875faaa7d419d050a464b09538797Jason Sams
3744b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!verptr) {
375af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Error, OpenGL ES Lite not supported");
3764b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
3772382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL(NULL, 0, NULL);
3784b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3794b3de47071d875faaa7d419d050a464b09538797Jason Sams    } else {
3804b3de47071d875faaa7d419d050a464b09538797Jason Sams        sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
3814b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3824b3de47071d875faaa7d419d050a464b09538797Jason Sams
3834b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &dc->gl.gl.maxVertexAttribs);
3844b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &dc->gl.gl.maxVertexUniformVectors);
3854b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxVertexTextureUnits);
3864b3de47071d875faaa7d419d050a464b09538797Jason Sams
3874b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VARYING_VECTORS, &dc->gl.gl.maxVaryingVectors);
3884b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxTextureImageUnits);
3894b3de47071d875faaa7d419d050a464b09538797Jason Sams
3904b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxFragmentTextureImageUnits);
3914b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &dc->gl.gl.maxFragmentUniformVectors);
3924b3de47071d875faaa7d419d050a464b09538797Jason Sams
3934b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.OES_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
3944b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                "GL_OES_texture_npot");
395917027582745bbf27335a70360a4e6da51961978Mathias Agopian    dc->gl.gl.IMG_texture_npot = NULL != strstr((const char *)dc->gl.gl.extensions,
3964b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                   "GL_IMG_texture_npot");
397917027582745bbf27335a70360a4e6da51961978Mathias Agopian    dc->gl.gl.NV_texture_npot_2D_mipmap = NULL != strstr((const char *)dc->gl.gl.extensions,
3984b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                            "GL_NV_texture_npot_2D_mipmap");
3994b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.EXT_texture_max_aniso = 1.0f;
4004b3de47071d875faaa7d419d050a464b09538797Jason Sams    bool hasAniso = NULL != strstr((const char *)dc->gl.gl.extensions,
4014b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   "GL_EXT_texture_filter_anisotropic");
4024b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (hasAniso) {
4034b3de47071d875faaa7d419d050a464b09538797Jason Sams        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &dc->gl.gl.EXT_texture_max_aniso);
4044b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4054b3de47071d875faaa7d419d050a464b09538797Jason Sams
4069db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
4079db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines        DumpDebug(dc);
4089db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
4094b3de47071d875faaa7d419d050a464b09538797Jason Sams
410a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache = new RsdShaderCache();
411a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState = new RsdVertexArrayState();
412a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs);
413a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    dc->gl.currentFrameBuffer = NULL;
4149719bd4a0187c400ba868712612fe66da4635aacJason Sams    dc->mHasGraphics = true;
415a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
4166598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initGLThread end", rsc);
4172382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL(NULL, 0, NULL);
4184b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
4194b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4204b3de47071d875faaa7d419d050a464b09538797Jason Sams
4214b3de47071d875faaa7d419d050a464b09538797Jason Sams
4227257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchoukbool rsdGLSetSurface(const Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
42387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
4244b3de47071d875faaa7d419d050a464b09538797Jason Sams
4254b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLBoolean ret;
4264b3de47071d875faaa7d419d050a464b09538797Jason Sams    // WAR: Some drivers fail to handle 0 size surfaces correcntly.
4274b3de47071d875faaa7d419d050a464b09538797Jason Sams    // Use the pbuffer to avoid this pitfall.
4284b3de47071d875faaa7d419d050a464b09538797Jason Sams    if ((dc->gl.egl.surface != NULL) || (w == 0) || (h == 0)) {
4292382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4304b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
4314b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surfaceDefault, dc->gl.egl.context);
4324b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4334b3de47071d875faaa7d419d050a464b09538797Jason Sams
4342382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglDestroySurface", __LINE__, __FILE__);
4354b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
4364b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroySurface", ret);
4374b3de47071d875faaa7d419d050a464b09538797Jason Sams
4384b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = NULL;
4394b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = 1;
4404b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = 1;
4414b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4424b3de47071d875faaa7d419d050a464b09538797Jason Sams
443c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams    if (dc->gl.wndSurface != NULL) {
444c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        dc->gl.wndSurface->decStrong(NULL);
445c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams    }
446c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams
4477257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    dc->gl.wndSurface = (ANativeWindow *)sur;
4484b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.wndSurface != NULL) {
449c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        dc->gl.wndSurface->incStrong(NULL);
4504b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.width = w;
4514b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.height = h;
4524b3de47071d875faaa7d419d050a464b09538797Jason Sams
4532382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglCreateWindowSurface", __LINE__, __FILE__);
4544b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
4554b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                    dc->gl.wndSurface, NULL);
4564b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglCreateWindowSurface");
4574b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface == EGL_NO_SURFACE) {
458af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
4594b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
4604b3de47071d875faaa7d419d050a464b09538797Jason Sams
4612382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4624b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
4634b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surface, dc->gl.egl.context);
4644b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4654b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4662382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL(NULL, 0, NULL);
4674b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
4684b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4694b3de47071d875faaa7d419d050a464b09538797Jason Sams
4704b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLSwap(const android::renderscript::Context *rsc) {
47187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
4722382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
4734b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4744b3de47071d875faaa7d419d050a464b09538797Jason Sams
4759719bd4a0187c400ba868712612fe66da4635aacJason Samsvoid rsdGLSetPriority(const Context *rsc, int32_t priority) {
4769719bd4a0187c400ba868712612fe66da4635aacJason Sams    if (priority > 0) {
4779719bd4a0187c400ba868712612fe66da4635aacJason Sams        // Mark context as low priority.
4789719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("low pri");
4799719bd4a0187c400ba868712612fe66da4635aacJason Sams    } else {
4809719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("normal pri");
4819719bd4a0187c400ba868712612fe66da4635aacJason Sams    }
4829719bd4a0187c400ba868712612fe66da4635aacJason Sams}
4839719bd4a0187c400ba868712612fe66da4635aacJason Sams
484c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchoukvoid rsdGLCheckError(const android::renderscript::Context *rsc,
485c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                     const char *msg, bool isFatal) {
486c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    GLenum err = glGetError();
487c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    if (err != GL_NO_ERROR) {
488c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        char buf[1024];
489c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
490c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
491c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        if (isFatal) {
492c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
493c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        } else {
494c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            switch (err) {
495c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            case GL_OUT_OF_MEMORY:
496c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_OUT_OF_MEMORY, buf);
497c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
498c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            default:
499c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_DRIVER, buf);
500c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
501c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            }
502c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        }
503c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
504af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, %s", rsc, buf);
505c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    }
506c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
507c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk}
508