1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
* Copyright © 2011 Linaro Limited
*
* This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
*
* glmark2 is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* glmark2. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexandros Frantzis (glmark2)
* Jesse Barker
*/
#include "canvas-android.h"
#include "log.h"
#include "options.h"
#include "gl-headers.h"
#include <fstream>
#include <sstream>
/******************
* Public methods *
******************/
bool
CanvasAndroid::init()
{
EGLint attribs[] = {
EGL_CONFIG_ID, 0,
EGL_NONE
};
/* Init the GLAD entrypoints */
if (!egl_lib_.open_from_alternatives({"libEGL.so", "libEGL.so.1" })) {
Log::error("Error loading EGL library\n");
return false;
}
if (gladLoadEGLUserPtr(EGL_NO_DISPLAY, load_proc, &egl_lib_) == 0) {
Log::error("Loading EGL entry points failed\n");
return false;
}
/* Get the current EGL config */
EGLDisplay egl_display(eglGetCurrentDisplay());
/* Reinitialize GLAD with a known display */
if (gladLoadEGLUserPtr(egl_display, load_proc, &egl_lib_) == 0) {
Log::error("Loading EGL entry points with display failed\n");
return false;
}
EGLContext egl_context(eglGetCurrentContext());
EGLConfig egl_config(0);
EGLint num_configs;
eglQueryContext(egl_display, egl_context, EGL_CONFIG_ID, &(attribs[1]));
eglChooseConfig(egl_display, attribs, &egl_config, 1, &num_configs);
/* Before calling GLES functions, init GLAD GLES */
if (!gles_lib_.open("libGLESv2.so")) {
Log::error("Error loading GLES library\n");
return false;
}
if (!gladLoadGLES2UserPtr(load_proc, &gles_lib_)) {
Log::error("Loading GLESv2 entry points failed.\n");
return false;
}
resize(width_, height_);
if (!eglSwapInterval(egl_display, 0))
Log::info("** Failed to set swap interval. Results may be bounded above by refresh rate.\n");
init_gl_extensions();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
clear();
eglGetConfigAttrib(egl_display, egl_config, EGL_BUFFER_SIZE, &chosen_config_.buffer);
eglGetConfigAttrib(egl_display, egl_config, EGL_RED_SIZE, &chosen_config_.red);
eglGetConfigAttrib(egl_display, egl_config, EGL_GREEN_SIZE, &chosen_config_.green);
eglGetConfigAttrib(egl_display, egl_config, EGL_BLUE_SIZE, &chosen_config_.blue);
eglGetConfigAttrib(egl_display, egl_config, EGL_ALPHA_SIZE, &chosen_config_.alpha);
eglGetConfigAttrib(egl_display, egl_config, EGL_DEPTH_SIZE, &chosen_config_.depth);
eglGetConfigAttrib(egl_display, egl_config, EGL_STENCIL_SIZE, &chosen_config_.stencil);
if (Options::show_debug) {
int id, native_id;
eglGetConfigAttrib(egl_display, egl_config, EGL_CONFIG_ID, &id);
eglGetConfigAttrib(egl_display, egl_config, EGL_NATIVE_VISUAL_ID, &native_id);
Log::debug("EGL chosen config ID: 0x%x Native Visual ID: 0x%x\n"
" Buffer: %d bits\n"
" Red: %d bits\n"
" Green: %d bits\n"
" Blue: %d bits\n"
" Alpha: %d bits\n"
" Depth: %d bits\n"
" Stencil: %d bits\n",
id, native_id,
chosen_config_.buffer,
chosen_config_.red, chosen_config_.green, chosen_config_.blue,
chosen_config_.alpha, chosen_config_.depth, chosen_config_.stencil);
}
return true;
}
void
CanvasAndroid::visible(bool visible)
{
static_cast<void>(visible);
}
void
CanvasAndroid::clear()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepthf(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void
CanvasAndroid::update()
{
}
void
CanvasAndroid::print_info()
{
std::stringstream ss;
ss << " OpenGL Information" << std::endl;
ss << " GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl;
ss << " GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl;
ss << " GL_VERSION: " << glGetString(GL_VERSION) << std::endl;
ss << " Surface Config: " << "buf=" << chosen_config_.buffer
<< " r=" << chosen_config_.red << " g=" << chosen_config_.green
<< " b=" << chosen_config_.blue << " a=" << chosen_config_.alpha
<< " depth=" << chosen_config_.depth
<< " stencil=" << chosen_config_.stencil << std::endl;
ss << " Surface Size: " << width_ << "x" << height_ << std::endl;
Log::info("%s", ss.str().c_str());
}
Canvas::Pixel
CanvasAndroid::read_pixel(int x, int y)
{
uint8_t pixel[4];
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
return Canvas::Pixel(pixel[0], pixel[1], pixel[2], pixel[3]);
}
void
CanvasAndroid::write_to_file(std::string &filename)
{
char *pixels = new char[width_ * height_ * 4];
for (int i = 0; i < height_; i++) {
glReadPixels(0, i, width_, 1, GL_RGBA, GL_UNSIGNED_BYTE,
&pixels[(height_ - i - 1) * width_ * 4]);
}
std::ofstream output (filename.c_str(), std::ios::out | std::ios::binary);
output.write(pixels, 4 * width_ * height_);
delete [] pixels;
}
bool
CanvasAndroid::should_quit()
{
return false;
}
void
CanvasAndroid::resize(int width, int height)
{
width_ = width;
height_ = height;
glViewport(0, 0, width_, height_);
projection_ = LibMatrix::Mat4::perspective(60.0, width_ / static_cast<float>(height_),
1.0, 1024.0);
}
/*******************
* Private methods *
*******************/
GLADapiproc
CanvasAndroid::load_proc(void *userdata, const char *name)
{
if (eglGetProcAddress) {
GLADapiproc sym = reinterpret_cast<GLADapiproc>(eglGetProcAddress(name));
if (sym) {
return sym;
}
}
SharedLibrary* lib = reinterpret_cast<SharedLibrary*>(userdata);
return reinterpret_cast<GLADapiproc>(lib->load(name));
}
void
CanvasAndroid::init_gl_extensions()
{
GLExtensions::MapBuffer = glMapBufferOES;
GLExtensions::UnmapBuffer = glUnmapBufferOES;
GLExtensions::GenFramebuffers = glGenFramebuffers;
GLExtensions::DeleteFramebuffers = glDeleteFramebuffers;
GLExtensions::BindFramebuffer = glBindFramebuffer;
GLExtensions::FramebufferTexture2D = glFramebufferTexture2D;
GLExtensions::FramebufferRenderbuffer = glFramebufferRenderbuffer;
GLExtensions::CheckFramebufferStatus = glCheckFramebufferStatus;
GLExtensions::GenRenderbuffers = glGenRenderbuffers;
GLExtensions::DeleteRenderbuffers = glDeleteRenderbuffers;
GLExtensions::BindRenderbuffer = glBindRenderbuffer;
GLExtensions::RenderbufferStorage = glRenderbufferStorage;
GLExtensions::GenerateMipmap = glGenerateMipmap;
}
|