2013-06-07 22:35:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <GLES/gl.h>
|
2013-08-15 21:56:51 +00:00
|
|
|
#include <GLES/glext.h>
|
2013-06-07 22:35:48 +00:00
|
|
|
|
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <cutils/compiler.h>
|
|
|
|
|
|
|
|
#include "GLES11RenderEngine.h"
|
2013-08-08 04:24:32 +00:00
|
|
|
#include "Mesh.h"
|
2013-08-15 01:20:17 +00:00
|
|
|
#include "Texture.h"
|
2013-06-07 22:35:48 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
GLES11RenderEngine::GLES11RenderEngine() {
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
|
|
|
|
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 4);
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glShadeModel(GL_FLAT);
|
|
|
|
glDisable(GL_DITHER);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
struct pack565 {
|
|
|
|
inline uint16_t operator() (int r, int g, int b) const {
|
|
|
|
return (r<<11)|(g<<5)|b;
|
|
|
|
}
|
|
|
|
} pack565;
|
|
|
|
|
|
|
|
const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
|
|
|
|
glGenTextures(1, &mProtectedTexName);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
|
|
|
|
GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLES11RenderEngine::~GLES11RenderEngine() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t GLES11RenderEngine::getMaxTextureSize() const {
|
|
|
|
return mMaxTextureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GLES11RenderEngine::getMaxViewportDims() const {
|
|
|
|
return
|
|
|
|
mMaxViewportDims[0] < mMaxViewportDims[1] ?
|
|
|
|
mMaxViewportDims[0] : mMaxViewportDims[1];
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:24:32 +00:00
|
|
|
void GLES11RenderEngine::setViewportAndProjection(
|
|
|
|
size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
|
|
|
|
glViewport(0, 0, vpw, vph);
|
2013-06-07 22:35:48 +00:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
// put the origin in the left-bottom corner
|
2013-08-08 04:24:32 +00:00
|
|
|
if (yswap) glOrthof(0, w, h, 0, 0, 1);
|
|
|
|
else glOrthof(0, w, 0, h, 0, 1);
|
2013-06-07 22:35:48 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::setupLayerBlending(
|
|
|
|
bool premultipliedAlpha, bool opaque, int alpha) {
|
|
|
|
GLenum combineRGB;
|
|
|
|
GLenum combineAlpha;
|
|
|
|
GLenum src0Alpha;
|
|
|
|
GLfloat envColor[4];
|
|
|
|
|
|
|
|
if (CC_UNLIKELY(alpha < 0xFF)) {
|
|
|
|
// Cv = premultiplied ? Cs*alpha : Cs
|
2013-08-08 04:24:32 +00:00
|
|
|
// Av = !opaque ? As*alpha : As
|
2013-06-07 22:35:48 +00:00
|
|
|
combineRGB = premultipliedAlpha ? GL_MODULATE : GL_REPLACE;
|
|
|
|
combineAlpha = !opaque ? GL_MODULATE : GL_REPLACE;
|
|
|
|
src0Alpha = GL_CONSTANT;
|
|
|
|
envColor[0] = alpha * (1.0f / 255.0f);
|
|
|
|
} else {
|
|
|
|
// Cv = Cs
|
|
|
|
// Av = opaque ? 1.0 : As
|
|
|
|
combineRGB = GL_REPLACE;
|
|
|
|
combineAlpha = GL_REPLACE;
|
|
|
|
src0Alpha = opaque ? GL_CONSTANT : GL_TEXTURE;
|
|
|
|
envColor[0] = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
if (combineRGB == GL_MODULATE) {
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
|
|
|
}
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
|
|
|
|
if (combineAlpha == GL_MODULATE) {
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
|
|
|
|
}
|
|
|
|
if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) {
|
|
|
|
envColor[1] = envColor[0];
|
|
|
|
envColor[2] = envColor[0];
|
|
|
|
envColor[3] = envColor[0];
|
|
|
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha < 0xFF || !opaque) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA,
|
|
|
|
GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::setupDimLayerBlending(int alpha) {
|
|
|
|
glDisable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
if (alpha == 0xFF) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
} else {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
}
|
|
|
|
glColor4f(0, 0, 0, alpha/255.0f);
|
|
|
|
}
|
|
|
|
|
2013-08-15 01:20:17 +00:00
|
|
|
void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) {
|
|
|
|
GLuint target = texture.getTextureTarget();
|
|
|
|
glBindTexture(target, texture.getTextureName());
|
2013-06-07 22:35:48 +00:00
|
|
|
GLenum filter = GL_NEAREST;
|
2013-08-15 01:20:17 +00:00
|
|
|
if (texture.getFiltering()) {
|
2013-06-07 22:35:48 +00:00
|
|
|
filter = GL_LINEAR;
|
|
|
|
}
|
2013-08-15 01:20:17 +00:00
|
|
|
glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameterx(target, GL_TEXTURE_MAG_FILTER, filter);
|
|
|
|
glTexParameterx(target, GL_TEXTURE_MIN_FILTER, filter);
|
2013-06-07 22:35:48 +00:00
|
|
|
glMatrixMode(GL_TEXTURE);
|
2013-08-27 03:42:07 +00:00
|
|
|
glLoadMatrixf(texture.getMatrix().asArray());
|
2013-06-07 22:35:48 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glEnable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::setupLayerBlackedOut() {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
|
|
|
|
glMatrixMode(GL_TEXTURE);
|
|
|
|
glLoadIdentity();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glDisable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::disableTexturing() {
|
|
|
|
glDisable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::disableBlending() {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2013-08-15 21:56:51 +00:00
|
|
|
void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
|
|
|
|
uint32_t* texName, uint32_t* fbName, uint32_t* status) {
|
|
|
|
GLuint tname, name;
|
|
|
|
// turn our EGLImage into a texture
|
|
|
|
glGenTextures(1, &tname);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tname);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
|
|
|
|
|
|
|
|
// create a Framebuffer Object to render into
|
|
|
|
glGenFramebuffersOES(1, &name);
|
|
|
|
glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
|
|
|
|
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
|
|
|
|
GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
|
|
|
|
|
|
|
|
*status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
|
|
|
|
*texName = tname;
|
|
|
|
*fbName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
|
|
|
|
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
|
|
|
|
glDeleteFramebuffersOES(1, &fbName);
|
|
|
|
glDeleteTextures(1, &texName);
|
|
|
|
}
|
|
|
|
|
2013-08-29 01:13:56 +00:00
|
|
|
void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
|
2013-08-08 04:24:32 +00:00
|
|
|
glColor4f(r, g, b, a);
|
2013-06-07 22:35:48 +00:00
|
|
|
glDisable(GL_TEXTURE_EXTERNAL_OES);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:24:32 +00:00
|
|
|
void GLES11RenderEngine::drawMesh(const Mesh& mesh) {
|
|
|
|
if (mesh.getTexCoordsSize()) {
|
2013-06-07 22:35:48 +00:00
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
2013-08-08 04:24:32 +00:00
|
|
|
glTexCoordPointer(mesh.getTexCoordsSize(),
|
|
|
|
GL_FLOAT,
|
|
|
|
mesh.getByteStride(),
|
|
|
|
mesh.getTexCoords());
|
2013-06-07 22:35:48 +00:00
|
|
|
}
|
2013-08-08 04:24:32 +00:00
|
|
|
|
|
|
|
glVertexPointer(mesh.getVertexSize(),
|
|
|
|
GL_FLOAT,
|
|
|
|
mesh.getByteStride(),
|
2013-08-14 03:51:23 +00:00
|
|
|
mesh.getPositions());
|
2013-08-08 04:24:32 +00:00
|
|
|
|
|
|
|
glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
|
|
|
|
|
|
|
|
if (mesh.getTexCoordsSize()) {
|
2013-06-07 22:35:48 +00:00
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 04:36:12 +00:00
|
|
|
void GLES11RenderEngine::beginGroup(const mat4& colorTransform) {
|
|
|
|
// doesn't do anything in GLES 1.1
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLES11RenderEngine::endGroup() {
|
|
|
|
// doesn't do anything in GLES 1.1
|
|
|
|
}
|
|
|
|
|
2013-06-07 22:35:48 +00:00
|
|
|
void GLES11RenderEngine::dump(String8& result) {
|
2013-08-15 21:56:51 +00:00
|
|
|
RenderEngine::dump(result);
|
2013-06-07 22:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ---------------------------------------------------------------------------
|
2013-08-15 21:56:51 +00:00
|
|
|
|
|
|
|
#if defined(__gl2_h_)
|
|
|
|
#error "don't include gl2/gl2.h in this file"
|
|
|
|
#endif
|