1975591a7af883d866d86ab819e164c6004694744John Reck/*
2975591a7af883d866d86ab819e164c6004694744John Reck * Copyright (C) 2016 The Android Open Source Project
3975591a7af883d866d86ab819e164c6004694744John Reck *
4975591a7af883d866d86ab819e164c6004694744John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5975591a7af883d866d86ab819e164c6004694744John Reck * you may not use this file except in compliance with the License.
6975591a7af883d866d86ab819e164c6004694744John Reck * You may obtain a copy of the License at
7975591a7af883d866d86ab819e164c6004694744John Reck *
8975591a7af883d866d86ab819e164c6004694744John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9975591a7af883d866d86ab819e164c6004694744John Reck *
10975591a7af883d866d86ab819e164c6004694744John Reck * Unless required by applicable law or agreed to in writing, software
11975591a7af883d866d86ab819e164c6004694744John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12975591a7af883d866d86ab819e164c6004694744John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13975591a7af883d866d86ab819e164c6004694744John Reck * See the License for the specific language governing permissions and
14975591a7af883d866d86ab819e164c6004694744John Reck * limitations under the License.
15975591a7af883d866d86ab819e164c6004694744John Reck */
16975591a7af883d866d86ab819e164c6004694744John Reck
17975591a7af883d866d86ab819e164c6004694744John Reck#include "unwrap_gles.h"
18975591a7af883d866d86ab819e164c6004694744John Reck
19975591a7af883d866d86ab819e164c6004694744John Reck#include <EGL/egl.h>
20975591a7af883d866d86ab819e164c6004694744John Reck#include <EGL/eglext.h>
21975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES/gl.h>
22975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES/glext.h>
23975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES2/gl2.h>
24975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES2/gl2ext.h>
25975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES3/gl3.h>
26975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES3/gl31.h>
27975591a7af883d866d86ab819e164c6004694744John Reck#include <GLES3/gl32.h>
28975591a7af883d866d86ab819e164c6004694744John Reck
29975591a7af883d866d86ab819e164c6004694744John Reck#include <cutils/log.h>
30975591a7af883d866d86ab819e164c6004694744John Reck
31975591a7af883d866d86ab819e164c6004694744John Reckvoid assertNoGlErrors(const char* apicall) {
32975591a7af883d866d86ab819e164c6004694744John Reck    GLenum status = GL_NO_ERROR;
33975591a7af883d866d86ab819e164c6004694744John Reck    GLenum lastError = GL_NO_ERROR;
34975591a7af883d866d86ab819e164c6004694744John Reck    const char* lastErrorName = nullptr;
35975591a7af883d866d86ab819e164c6004694744John Reck    while ((status = glGetError()) != GL_NO_ERROR) {
36975591a7af883d866d86ab819e164c6004694744John Reck        lastError = status;
37975591a7af883d866d86ab819e164c6004694744John Reck        switch (status) {
38975591a7af883d866d86ab819e164c6004694744John Reck        case GL_INVALID_ENUM:
39975591a7af883d866d86ab819e164c6004694744John Reck            ALOGE("GL error:  GL_INVALID_ENUM");
40975591a7af883d866d86ab819e164c6004694744John Reck            lastErrorName = "GL_INVALID_ENUM";
41975591a7af883d866d86ab819e164c6004694744John Reck            break;
42975591a7af883d866d86ab819e164c6004694744John Reck        case GL_INVALID_VALUE:
43975591a7af883d866d86ab819e164c6004694744John Reck            ALOGE("GL error:  GL_INVALID_VALUE");
44975591a7af883d866d86ab819e164c6004694744John Reck            lastErrorName = "GL_INVALID_VALUE";
45975591a7af883d866d86ab819e164c6004694744John Reck            break;
46975591a7af883d866d86ab819e164c6004694744John Reck        case GL_INVALID_OPERATION:
47975591a7af883d866d86ab819e164c6004694744John Reck            ALOGE("GL error:  GL_INVALID_OPERATION");
48975591a7af883d866d86ab819e164c6004694744John Reck            lastErrorName = "GL_INVALID_OPERATION";
49975591a7af883d866d86ab819e164c6004694744John Reck            break;
50975591a7af883d866d86ab819e164c6004694744John Reck        case GL_OUT_OF_MEMORY:
51975591a7af883d866d86ab819e164c6004694744John Reck            ALOGE("GL error:  Out of memory!");
52975591a7af883d866d86ab819e164c6004694744John Reck            lastErrorName = "GL_OUT_OF_MEMORY";
53975591a7af883d866d86ab819e164c6004694744John Reck            break;
54975591a7af883d866d86ab819e164c6004694744John Reck        default:
55975591a7af883d866d86ab819e164c6004694744John Reck            ALOGE("GL error: 0x%x", status);
56975591a7af883d866d86ab819e164c6004694744John Reck            lastErrorName = "UNKNOWN";
57975591a7af883d866d86ab819e164c6004694744John Reck        }
58975591a7af883d866d86ab819e164c6004694744John Reck    }
59975591a7af883d866d86ab819e164c6004694744John Reck    LOG_ALWAYS_FATAL_IF(lastError != GL_NO_ERROR,
60975591a7af883d866d86ab819e164c6004694744John Reck            "%s error! %s (0x%x)", apicall, lastErrorName, lastError);
61975591a7af883d866d86ab819e164c6004694744John Reck}
62975591a7af883d866d86ab819e164c6004694744John Reck
63975591a7af883d866d86ab819e164c6004694744John Reck#define API_ENTRY(x) wrap_##x
64975591a7af883d866d86ab819e164c6004694744John Reck#define CALL_GL_API(x, ...) x(__VA_ARGS__); assertNoGlErrors(#x)
65975591a7af883d866d86ab819e164c6004694744John Reck#define CALL_GL_API_RETURN(x, ...) auto ret = x(__VA_ARGS__);\
66975591a7af883d866d86ab819e164c6004694744John Reck    assertNoGlErrors(#x);\
67975591a7af883d866d86ab819e164c6004694744John Reck    return ret
68975591a7af883d866d86ab819e164c6004694744John Reck
69975591a7af883d866d86ab819e164c6004694744John Reckextern "C" {
70975591a7af883d866d86ab819e164c6004694744John Reck#include <gl2_api.in>
71975591a7af883d866d86ab819e164c6004694744John Reck#include <gl2ext_api.in>
72975591a7af883d866d86ab819e164c6004694744John Reck
73975591a7af883d866d86ab819e164c6004694744John Reck// libGLESv2 handles these specially, so they are not in gl2_api.in
74975591a7af883d866d86ab819e164c6004694744John Reck
75975591a7af883d866d86ab819e164c6004694744John Reckvoid API_ENTRY(glGetBooleanv)(GLenum pname, GLboolean *data) {
76975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API(glGetBooleanv, pname, data);
77975591a7af883d866d86ab819e164c6004694744John Reck}
78975591a7af883d866d86ab819e164c6004694744John Reckvoid API_ENTRY(glGetFloatv)(GLenum pname, GLfloat *data) {
79975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API(glGetFloatv, pname, data);
80975591a7af883d866d86ab819e164c6004694744John Reck}
81975591a7af883d866d86ab819e164c6004694744John Reckvoid API_ENTRY(glGetIntegerv)(GLenum pname, GLint *data) {
82975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API(glGetIntegerv, pname, data);
83975591a7af883d866d86ab819e164c6004694744John Reck}
84975591a7af883d866d86ab819e164c6004694744John Reckconst GLubyte * API_ENTRY(glGetString)(GLenum name) {
85975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API_RETURN(glGetString, name);
86975591a7af883d866d86ab819e164c6004694744John Reck}
87975591a7af883d866d86ab819e164c6004694744John Reckconst GLubyte * API_ENTRY(glGetStringi)(GLenum name, GLuint index) {
88975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API_RETURN(glGetStringi, name, index);
89975591a7af883d866d86ab819e164c6004694744John Reck}
90975591a7af883d866d86ab819e164c6004694744John Reckvoid API_ENTRY(glGetInteger64v)(GLenum pname, GLint64 *data) {
91975591a7af883d866d86ab819e164c6004694744John Reck    CALL_GL_API(glGetInteger64v, pname, data);
92975591a7af883d866d86ab819e164c6004694744John Reck}
93975591a7af883d866d86ab819e164c6004694744John Reck}
94