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/PixelFormat.h>
18b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian
19b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian#include <system/window.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"
394edf030cbb7c6ac08dc563335c2af73c20f6e2e5Alex Sakhartchouk#include "rsDevice.h"
40a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdShaderCache.h"
41a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk#include "rsdVertexArray.h"
42a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk#include "rsdFrameBufferObj.h"
434b3de47071d875faaa7d419d050a464b09538797Jason Sams
4433c622e98b57c5021c92133543ff00ffdbbcafe7Mathias Agopian#include <gui/Surface.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
15544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    rsdGLSetSurface(rsc, 0, 0, nullptr);
156a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache->cleanupAll();
157a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.shaderCache;
158a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    delete dc->gl.vertexArrayState;
159a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
1604b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context != EGL_NO_CONTEXT) {
1612382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglMakeCurrent, dc->gl.egl.display,
1622382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                    EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1632382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surfaceDefault);
1644b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface != EGL_NO_SURFACE) {
1652382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surface);
1664b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
1672382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglDestroyContext, dc->gl.egl.display, dc->gl.egl.context);
1684b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroyContext");
1694b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1704b3de47071d875faaa7d419d050a464b09538797Jason Sams
1714b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount--;
1724b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!gGLContextCount) {
1732382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(eglTerminate, dc->gl.egl.display);
1744b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
1754b3de47071d875faaa7d419d050a464b09538797Jason Sams}
1764b3de47071d875faaa7d419d050a464b09538797Jason Sams
177da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchoukvoid getConfigData(const Context *rsc,
178da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk                   EGLint *configAttribs, size_t configAttribsLen,
179da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk                   uint32_t numSamples) {
180da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    memset(configAttribs, 0, configAttribsLen*sizeof(*configAttribs));
1814b3de47071d875faaa7d419d050a464b09538797Jason Sams
1824b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLint *configAttribsPtr = configAttribs;
1834b3de47071d875faaa7d419d050a464b09538797Jason Sams
1844b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_SURFACE_TYPE;
185a06461be26c0807fdf3cbdf25a7e7c2d92fac96bjon.parr    configAttribsPtr[1] = EGL_PBUFFER_BIT;
1864b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1874b3de47071d875faaa7d419d050a464b09538797Jason Sams
1884b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
1894b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
1904b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr += 2;
1914b3de47071d875faaa7d419d050a464b09538797Jason Sams
19267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_RED_SIZE;
19367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
19467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
19567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
19667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_GREEN_SIZE;
19767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
19867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
19967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
20067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[0] = EGL_BLUE_SIZE;
20167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr[1] = 8;
20267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    configAttribsPtr += 2;
20367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
20467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    if (rsc->mUserSurfaceConfig.alphaMin > 0) {
20567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr[0] = EGL_ALPHA_SIZE;
20667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr[1] = rsc->mUserSurfaceConfig.alphaMin;
20767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        configAttribsPtr += 2;
20867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    }
20967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2104b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mUserSurfaceConfig.depthMin > 0) {
2114b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_DEPTH_SIZE;
2124b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = rsc->mUserSurfaceConfig.depthMin;
2134b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
2144b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2154b3de47071d875faaa7d419d050a464b09538797Jason Sams
2164b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (rsc->mDev->mForceSW) {
2174b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
2184b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr[1] = EGL_SLOW_CONFIG;
2194b3de47071d875faaa7d419d050a464b09538797Jason Sams        configAttribsPtr += 2;
2204b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
2214b3de47071d875faaa7d419d050a464b09538797Jason Sams
222da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    if (numSamples > 1) {
223da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[0] = EGL_SAMPLE_BUFFERS;
224da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[1] = 1;
225da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[2] = EGL_SAMPLES;
226da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr[3] = numSamples;
227da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        configAttribsPtr += 4;
228da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    }
229da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
2304b3de47071d875faaa7d419d050a464b09538797Jason Sams    configAttribsPtr[0] = EGL_NONE;
231da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    rsAssert(configAttribsPtr < (configAttribs + configAttribsLen));
232da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk}
233da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
234da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchoukbool rsdGLInit(const Context *rsc) {
235da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
236da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
237da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    dc->gl.egl.numConfigs = -1;
238da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
239da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    EGLint configAttribs[128];
240da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
2414b3de47071d875faaa7d419d050a464b09538797Jason Sams
2426598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initEGL start", rsc);
2432382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglGetDisplay", __LINE__, __FILE__);
2444b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
2454b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglGetDisplay");
2464b3de47071d875faaa7d419d050a464b09538797Jason Sams
2472382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglInitialize, dc->gl.egl.display,
2482382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
2494b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglInitialize");
2504b3de47071d875faaa7d419d050a464b09538797Jason Sams
25167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLBoolean ret;
25267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
25367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    EGLint numConfigs = -1, n = 0;
2542382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
255da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk
256da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    // Try minding a multisample config that matches the user request
257da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    uint32_t minSample = rsc->mUserSurfaceConfig.samplesMin;
258da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    uint32_t prefSample = rsc->mUserSurfaceConfig.samplesPref;
259da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    for (uint32_t sampleCount = prefSample; sampleCount >= minSample; sampleCount--) {
260da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        getConfigData(rsc, configAttribs, (sizeof(configAttribs) / sizeof(EGLint)), sampleCount);
261da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
262da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        checkEglError("eglGetConfigs", ret);
263da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        if (numConfigs > 0) {
264da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk            break;
265da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk        }
266da7a148171869f392b5813635532c6ed5a752912Alex Sakhartchouk    }
26767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
26867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    if (numConfigs) {
26967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        EGLConfig* const configs = new EGLConfig[numConfigs];
27067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2712382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
27267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        ret = eglChooseConfig(dc->gl.egl.display,
27367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                configAttribs, configs, numConfigs, &n);
27467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        if (!ret || !n) {
27567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            checkEglError("eglChooseConfig", ret);
276af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
27767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
27867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
27967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // The first config is guaranteed to over-satisfy the constraints
28067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        dc->gl.egl.config = configs[0];
28167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
28267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // go through the list and skip configs that over-satisfy our needs
28367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        for (int i=0 ; i<n ; i++) {
28467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.alphaMin <= 0) {
28567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint alphaSize;
28667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
28767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_ALPHA_SIZE, &alphaSize);
28867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (alphaSize > 0) {
28967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
29067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
29167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
29267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
29367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            if (rsc->mUserSurfaceConfig.depthMin <= 0) {
29467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                EGLint depthSize;
29567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                eglGetConfigAttrib(dc->gl.egl.display,
29667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                        configs[i], EGL_DEPTH_SIZE, &depthSize);
29767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                if (depthSize > 0) {
29867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    continue;
29967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                }
30067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            }
30167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
30267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // Found one!
30367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            dc->gl.egl.config = configs[i];
30467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            break;
30567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        }
3064b3de47071d875faaa7d419d050a464b09538797Jason Sams
30767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        delete [] configs;
3084b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
30967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
3104b3de47071d875faaa7d419d050a464b09538797Jason Sams    //if (props.mLogVisual) {
3119db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
3124b3de47071d875faaa7d419d050a464b09538797Jason Sams        printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
3139db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
3144b3de47071d875faaa7d419d050a464b09538797Jason Sams    //}
3154b3de47071d875faaa7d419d050a464b09538797Jason Sams
3162382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglCreateContext", __LINE__, __FILE__);
3174b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
3184b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          EGL_NO_CONTEXT, context_attribs2);
3194b3de47071d875faaa7d419d050a464b09538797Jason Sams    checkEglError("eglCreateContext");
3204b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.context == EGL_NO_CONTEXT) {
321af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
32244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rsc->setWatchdogGL(nullptr, 0, nullptr);
3234b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3244b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3254b3de47071d875faaa7d419d050a464b09538797Jason Sams    gGLContextCount++;
3264b3de47071d875faaa7d419d050a464b09538797Jason Sams
3273318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
3283318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian    rsc->setWatchdogGL("eglCreatePbufferSurface", __LINE__, __FILE__);
3293318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian    dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
3303318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian            pbuffer_attribs);
3313318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian    checkEglError("eglCreatePbufferSurface");
3324b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
3333318d1809dfce03eb876d6a2eba39018a0e7d7beMathias Agopian        ALOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
3344b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
33544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rsc->setWatchdogGL(nullptr, 0, nullptr);
3364b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3374b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3384b3de47071d875faaa7d419d050a464b09538797Jason Sams
3392382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
34067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian    ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
34167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                         dc->gl.egl.surfaceDefault, dc->gl.egl.context);
3424b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (ret == EGL_FALSE) {
343af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("eglMakeCurrent returned EGL_FALSE");
3444b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
3454b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
34644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rsc->setWatchdogGL(nullptr, 0, nullptr);
3474b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3484b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3494b3de47071d875faaa7d419d050a464b09538797Jason Sams
3504b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.version = glGetString(GL_VERSION);
3514b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.vendor = glGetString(GL_VENDOR);
3524b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.renderer = glGetString(GL_RENDERER);
3534b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.extensions = glGetString(GL_EXTENSIONS);
3544b3de47071d875faaa7d419d050a464b09538797Jason Sams
3556598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
3566598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Version %s", mGL.mVersion);
3576598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Vendor %s", mGL.mVendor);
3586598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Renderer %s", mGL.mRenderer);
3596598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    //ALOGV("GL Extensions %s", mGL.mExtensions);
3604b3de47071d875faaa7d419d050a464b09538797Jason Sams
36144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    const char *verptr = nullptr;
3624b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (strlen((const char *)dc->gl.gl.version) > 9) {
3634b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES-CM", 12)) {
3644b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 12;
3654b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3664b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (!memcmp(dc->gl.gl.version, "OpenGL ES ", 10)) {
3674b3de47071d875faaa7d419d050a464b09538797Jason Sams            verptr = (const char *)dc->gl.gl.version + 9;
3684b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
3694b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3704b3de47071d875faaa7d419d050a464b09538797Jason Sams
3714b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (!verptr) {
372af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Error, OpenGL ES Lite not supported");
3734b3de47071d875faaa7d419d050a464b09538797Jason Sams        rsdGLShutdown(rsc);
37444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        rsc->setWatchdogGL(nullptr, 0, nullptr);
3754b3de47071d875faaa7d419d050a464b09538797Jason Sams        return false;
3764b3de47071d875faaa7d419d050a464b09538797Jason Sams    } else {
3774b3de47071d875faaa7d419d050a464b09538797Jason Sams        sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
3784b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
3794b3de47071d875faaa7d419d050a464b09538797Jason Sams
3804b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &dc->gl.gl.maxVertexAttribs);
3814b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &dc->gl.gl.maxVertexUniformVectors);
3824b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxVertexTextureUnits);
3834b3de47071d875faaa7d419d050a464b09538797Jason Sams
3844b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_VARYING_VECTORS, &dc->gl.gl.maxVaryingVectors);
3854b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxTextureImageUnits);
3864b3de47071d875faaa7d419d050a464b09538797Jason Sams
3874b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &dc->gl.gl.maxFragmentTextureImageUnits);
3884b3de47071d875faaa7d419d050a464b09538797Jason Sams    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &dc->gl.gl.maxFragmentUniformVectors);
3894b3de47071d875faaa7d419d050a464b09538797Jason Sams
39044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    dc->gl.gl.OES_texture_npot = nullptr != strstr((const char *)dc->gl.gl.extensions,
3914b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                "GL_OES_texture_npot");
39244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    dc->gl.gl.IMG_texture_npot = nullptr != strstr((const char *)dc->gl.gl.extensions,
3934b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                   "GL_IMG_texture_npot");
39444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    dc->gl.gl.NV_texture_npot_2D_mipmap = nullptr != strstr((const char *)dc->gl.gl.extensions,
3954b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                            "GL_NV_texture_npot_2D_mipmap");
3964b3de47071d875faaa7d419d050a464b09538797Jason Sams    dc->gl.gl.EXT_texture_max_aniso = 1.0f;
39744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    bool hasAniso = nullptr != strstr((const char *)dc->gl.gl.extensions,
3984b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   "GL_EXT_texture_filter_anisotropic");
3994b3de47071d875faaa7d419d050a464b09538797Jason Sams    if (hasAniso) {
4004b3de47071d875faaa7d419d050a464b09538797Jason Sams        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &dc->gl.gl.EXT_texture_max_aniso);
4014b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4024b3de47071d875faaa7d419d050a464b09538797Jason Sams
4039db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    if (0) {
4049db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines        DumpDebug(dc);
4059db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines    }
4064b3de47071d875faaa7d419d050a464b09538797Jason Sams
407a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.shaderCache = new RsdShaderCache();
408a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState = new RsdVertexArrayState();
409a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    dc->gl.vertexArrayState->init(dc->gl.gl.maxVertexAttribs);
41044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    dc->gl.currentFrameBuffer = nullptr;
4119719bd4a0187c400ba868712612fe66da4635aacJason Sams    dc->mHasGraphics = true;
412a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
4136598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block    ALOGV("%p initGLThread end", rsc);
41444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    rsc->setWatchdogGL(nullptr, 0, nullptr);
4154b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
4164b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4174b3de47071d875faaa7d419d050a464b09538797Jason Sams
4184b3de47071d875faaa7d419d050a464b09538797Jason Sams
419b322033c13487a174bb9c26466e9684d1ff4de8dJason Samsbool rsdGLSetInternalSurface(const Context *rsc, RsNativeWindow sur) {
42087fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
4214b3de47071d875faaa7d419d050a464b09538797Jason Sams
4224b3de47071d875faaa7d419d050a464b09538797Jason Sams    EGLBoolean ret;
42344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (dc->gl.egl.surface != nullptr) {
4242382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4254b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
4264b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surfaceDefault, dc->gl.egl.context);
4274b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4284b3de47071d875faaa7d419d050a464b09538797Jason Sams
4292382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglDestroySurface", __LINE__, __FILE__);
4304b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
4314b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglDestroySurface", ret);
4324b3de47071d875faaa7d419d050a464b09538797Jason Sams
43344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        dc->gl.egl.surface = nullptr;
4344b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
4354b3de47071d875faaa7d419d050a464b09538797Jason Sams
43644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (dc->gl.currentWndSurface != nullptr) {
43744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        dc->gl.currentWndSurface->decStrong(nullptr);
438c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams    }
439c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams
440b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    dc->gl.currentWndSurface = (ANativeWindow *)sur;
44144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (dc->gl.currentWndSurface != nullptr) {
44244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        dc->gl.currentWndSurface->incStrong(nullptr);
4434b3de47071d875faaa7d419d050a464b09538797Jason Sams
4442382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglCreateWindowSurface", __LINE__, __FILE__);
4454b3de47071d875faaa7d419d050a464b09538797Jason Sams        dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
44644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes                                                    dc->gl.currentWndSurface, nullptr);
4474b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglCreateWindowSurface");
4484b3de47071d875faaa7d419d050a464b09538797Jason Sams        if (dc->gl.egl.surface == EGL_NO_SURFACE) {
449af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
4504b3de47071d875faaa7d419d050a464b09538797Jason Sams        }
4514b3de47071d875faaa7d419d050a464b09538797Jason Sams
4522382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
4534b3de47071d875faaa7d419d050a464b09538797Jason Sams        ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
4544b3de47071d875faaa7d419d050a464b09538797Jason Sams                             dc->gl.egl.surface, dc->gl.egl.context);
4554b3de47071d875faaa7d419d050a464b09538797Jason Sams        checkEglError("eglMakeCurrent", ret);
4564b3de47071d875faaa7d419d050a464b09538797Jason Sams    }
45744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    rsc->setWatchdogGL(nullptr, 0, nullptr);
4584b3de47071d875faaa7d419d050a464b09538797Jason Sams    return true;
4594b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4604b3de47071d875faaa7d419d050a464b09538797Jason Sams
461b322033c13487a174bb9c26466e9684d1ff4de8dJason Samsbool rsdGLSetSurface(const Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
462b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
463b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
46444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (dc->gl.wndSurface != nullptr) {
46544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        dc->gl.wndSurface->decStrong(nullptr);
46644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        dc->gl.wndSurface = nullptr;
467b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    }
468b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    if(w && h) {
469b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        // WAR: Some drivers fail to handle 0 size surfaces correctly. Use the
470b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        // pbuffer to avoid this pitfall.
471b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        dc->gl.wndSurface = (ANativeWindow *)sur;
47244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        if (dc->gl.wndSurface != nullptr) {
47344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes            dc->gl.wndSurface->incStrong(nullptr);
474b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        }
475b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    }
476b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
477b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    return rsdGLSetInternalSurface(rsc, sur);
478b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams}
479b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
4804b3de47071d875faaa7d419d050a464b09538797Jason Samsvoid rsdGLSwap(const android::renderscript::Context *rsc) {
48187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
4822382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
4834b3de47071d875faaa7d419d050a464b09538797Jason Sams}
4844b3de47071d875faaa7d419d050a464b09538797Jason Sams
4859719bd4a0187c400ba868712612fe66da4635aacJason Samsvoid rsdGLSetPriority(const Context *rsc, int32_t priority) {
4869719bd4a0187c400ba868712612fe66da4635aacJason Sams    if (priority > 0) {
4879719bd4a0187c400ba868712612fe66da4635aacJason Sams        // Mark context as low priority.
4889719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("low pri");
4899719bd4a0187c400ba868712612fe66da4635aacJason Sams    } else {
4909719bd4a0187c400ba868712612fe66da4635aacJason Sams        ALOGV("normal pri");
4919719bd4a0187c400ba868712612fe66da4635aacJason Sams    }
4929719bd4a0187c400ba868712612fe66da4635aacJason Sams}
4939719bd4a0187c400ba868712612fe66da4635aacJason Sams
494c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchoukvoid rsdGLCheckError(const android::renderscript::Context *rsc,
495c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                     const char *msg, bool isFatal) {
496c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    GLenum err = glGetError();
497c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    if (err != GL_NO_ERROR) {
498c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        char buf[1024];
499c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
500c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
501c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        if (isFatal) {
502c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
503c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        } else {
504c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            switch (err) {
505c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            case GL_OUT_OF_MEMORY:
506c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_OUT_OF_MEMORY, buf);
507c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
508c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            default:
509c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                rsc->setError(RS_ERROR_DRIVER, buf);
510c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                break;
511c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            }
512c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        }
513c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
514af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("%p, %s", rsc, buf);
515c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk    }
516c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
517c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk}
518653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
519653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchoukvoid rsdGLClearColor(const android::renderscript::Context *rsc,
520653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk                     float r, float g, float b, float a) {
521653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glClearColor, r, g, b, a);
522653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glClear, GL_COLOR_BUFFER_BIT);
523653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk}
524653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
525653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchoukvoid rsdGLClearDepth(const android::renderscript::Context *rsc, float v) {
526653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glClearDepthf, v);
527653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glClear, GL_DEPTH_BUFFER_BIT);
528653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk}
529653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
530653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchoukvoid rsdGLFinish(const android::renderscript::Context *rsc) {
531653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glFinish);
532653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk}
533653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
534653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchoukvoid rsdGLDrawQuadTexCoords(const android::renderscript::Context *rsc,
535653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk                            float x1, float y1, float z1, float u1, float v1,
536653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk                            float x2, float y2, float z2, float u2, float v2,
537653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk                            float x3, float y3, float z3, float u3, float v3,
538653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk                            float x4, float y4, float z4, float u4, float v4) {
539653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
540653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
541653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
542653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
543653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RsdVertexArray::Attrib attribs[2];
544e195a3f57ace3b66d313a6ee88c6e93d5c9d87f4Tim Murray
545e195a3f57ace3b66d313a6ee88c6e93d5c9d87f4Tim Murray    attribs[0].set(GL_FLOAT, 3, 12, false, (size_t)vtx, "ATTRIB_position");
546e195a3f57ace3b66d313a6ee88c6e93d5c9d87f4Tim Murray    attribs[1].set(GL_FLOAT, 2, 8, false, (size_t)tex, "ATTRIB_texture0");
547653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
548653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RsdVertexArray va(attribs, 2);
549653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    va.setup(rsc);
550653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk
551653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
552653b53ebcd2bedc94ac486080d2e82e1920232e3Alex Sakhartchouk}
553