Extensions.cpp revision e9bc11f7121dbe373b0cbe5779ee6a12d824492c
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include <GLES2/gl2.h>
20#include <GLES2/gl2ext.h>
21
22#include <EGL/egl.h>
23#include <EGL/eglext.h>
24
25#include <utils/Log.h>
26
27#include "Debug.h"
28#include "Extensions.h"
29
30namespace android {
31
32using namespace uirenderer;
33ANDROID_SINGLETON_STATIC_INSTANCE(Extensions);
34
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Defines
39///////////////////////////////////////////////////////////////////////////////
40
41// Debug
42#if DEBUG_EXTENSIONS
43    #define EXT_LOGD(...) ALOGD(__VA_ARGS__)
44#else
45    #define EXT_LOGD(...)
46#endif
47
48///////////////////////////////////////////////////////////////////////////////
49// Constructors
50///////////////////////////////////////////////////////////////////////////////
51
52Extensions::Extensions(): Singleton<Extensions>() {
53    // Query GL extensions
54    findExtensions((const char*) glGetString(GL_EXTENSIONS), mGlExtensionList);
55    mHasNPot = hasGlExtension("GL_OES_texture_npot");
56    mHasFramebufferFetch = hasGlExtension("GL_NV_shader_framebuffer_fetch");
57    mHasDiscardFramebuffer = hasGlExtension("GL_EXT_discard_framebuffer");
58    mHasDebugMarker = hasGlExtension("GL_EXT_debug_marker");
59    mHasDebugLabel = hasGlExtension("GL_EXT_debug_label");
60    mHasTiledRendering = hasGlExtension("GL_QCOM_tiled_rendering");
61    mHas1BitStencil = hasGlExtension("GL_OES_stencil1");
62    mHas4BitStencil = hasGlExtension("GL_OES_stencil4");
63
64    // Query EGL extensions
65    findExtensions(eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS), mEglExtensionList);
66    mHasNvSystemTime = hasEglExtension("EGL_NV_system_time");
67
68    const char* version = (const char*) glGetString(GL_VERSION);
69
70    // Section 6.1.5 of the OpenGL ES specification indicates the GL version
71    // string strictly follows this format:
72    //
73    // OpenGL<space>ES<space><version number><space><vendor-specific information>
74    //
75    // In addition section 6.1.5 describes the version number thusly:
76    //
77    // "The version number is either of the form major number.minor number or
78    // major number.minor number.release number, where the numbers all have one
79    // or more digits. The release number and vendor specific information are
80    // optional."
81
82    if (sscanf(version, "OpenGL ES %d.%d", &mVersionMajor, &mVersionMinor) !=2) {
83        // If we cannot parse the version number, assume OpenGL ES 2.0
84        mVersionMajor = 2;
85        mVersionMinor = 0;
86    }
87}
88
89Extensions::~Extensions() {
90}
91
92///////////////////////////////////////////////////////////////////////////////
93// Methods
94///////////////////////////////////////////////////////////////////////////////
95
96bool Extensions::hasGlExtension(const char* extension) const {
97   const String8 s(extension);
98   return mGlExtensionList.indexOf(s) >= 0;
99}
100
101bool Extensions::hasEglExtension(const char* extension) const {
102   const String8 s(extension);
103   return mEglExtensionList.indexOf(s) >= 0;
104}
105
106void Extensions::findExtensions(const char* extensions, SortedVector<String8>& list) const {
107    const char* current = extensions;
108    const char* head = current;
109    EXT_LOGD("Available extensions:");
110    do {
111        head = strchr(current, ' ');
112        String8 s(current, head ? head - current : strlen(current));
113        if (s.length()) {
114            list.add(s);
115            EXT_LOGD("  %s", s.string());
116        }
117        current = head + 1;
118    } while (head);
119}
120
121void Extensions::dump() const {
122   ALOGD("%s", (const char*) glGetString(GL_VERSION));
123   ALOGD("Supported GL extensions:\n%s", (const char*) glGetString(GL_EXTENSIONS));
124   ALOGD("Supported EGL extensions:\n%s", eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS));
125}
126
127}; // namespace uirenderer
128}; // namespace android
129