2 * Copyright 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 #include <GLES2/gl2.h>
20 #include <GLES2/gl2ext.h>
22 #include <utils/String8.h>
23 #include <utils/Trace.h>
25 #include <cutils/compiler.h>
27 #include "GLES20RenderEngine.h"
29 #include "ProgramCache.h"
30 #include "Description.h"
34 // ---------------------------------------------------------------------------
36 // ---------------------------------------------------------------------------
38 GLES20RenderEngine::GLES20RenderEngine() {
40 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
41 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
43 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
44 glPixelStorei(GL_PACK_ALIGNMENT, 4);
47 inline uint16_t operator() (int r, int g, int b) const {
48 return (r<<11)|(g<<5)|b;
52 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
53 glGenTextures(1, &mProtectedTexName);
54 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
60 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
63 GLES20RenderEngine::~GLES20RenderEngine() {
67 size_t GLES20RenderEngine::getMaxTextureSize() const {
68 return mMaxTextureSize;
71 size_t GLES20RenderEngine::getMaxViewportDims() const {
73 mMaxViewportDims[0] < mMaxViewportDims[1] ?
74 mMaxViewportDims[0] : mMaxViewportDims[1];
77 void GLES20RenderEngine::setViewportAndProjection(
78 size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
81 inline void operator() (GLfloat *m,
82 GLfloat left, GLfloat right, GLfloat bottom, GLfloat top,
83 GLfloat near, GLfloat far) const {
84 memset(m, 0, 16*sizeof(GLfloat));
85 m[ 0] = 2.0f / (right - left);
86 m[ 5] = 2.0f / (top - bottom);
87 m[10] =-2.0f / (far - near);
89 m[12] = -(right + left) / (right - left);
90 m[13] = -(top + bottom) / (top - bottom);
91 m[14] = -(far + near) / (far - near);
96 if (yswap) ortho(m, 0, w, h, 0, 0, 1);
97 else ortho(m, 0, w, 0, h, 0, 1);
99 glViewport(0, 0, vpw, vph);
100 mState.setProjectionMatrix(m);
103 void GLES20RenderEngine::setupLayerBlending(
104 bool premultipliedAlpha, bool opaque, int alpha) {
106 mState.setPremultipliedAlpha(premultipliedAlpha);
107 mState.setOpaque(opaque);
108 mState.setPlaneAlpha(alpha / 255.0f);
110 if (alpha < 0xFF || !opaque) {
112 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
118 void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
119 mState.setPlaneAlpha(alpha / 255.0f);
125 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
130 void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
131 GLuint target = texture.getTextureTarget();
132 glBindTexture(target, texture.getTextureName());
133 GLenum filter = GL_NEAREST;
134 if (texture.getFiltering()) {
137 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
138 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
139 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
140 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
142 mState.setTexture(texture);
145 void GLES20RenderEngine::setupLayerBlackedOut() {
146 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
147 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
148 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
149 mState.setTexture(texture);
152 void GLES20RenderEngine::disableTexturing() {
153 mState.disableTexture();
156 void GLES20RenderEngine::disableBlending() {
161 void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
162 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
164 // turn our EGLImage into a texture
165 glGenTextures(1, &tname);
166 glBindTexture(GL_TEXTURE_2D, tname);
167 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
169 // create a Framebuffer Object to render into
170 glGenFramebuffers(1, &name);
171 glBindFramebuffer(GL_FRAMEBUFFER, name);
172 glFramebufferTexture2D(GL_FRAMEBUFFER,
173 GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
175 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
180 void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
181 glBindFramebuffer(GL_FRAMEBUFFER, 0);
182 glDeleteFramebuffers(1, &fbName);
183 glDeleteTextures(1, &texName);
186 void GLES20RenderEngine::fillWithColor(const Mesh& mesh, float r, float g, float b, float a) {
187 mState.setColor(r, g, b, a);
191 ProgramCache::getInstance().useProgram(mState);
193 glVertexAttribPointer(Program::position,
194 mesh.getVertexSize(),
196 mesh.getByteStride(),
197 mesh.getPositions());
199 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
202 void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
204 ProgramCache::getInstance().useProgram(mState);
206 if (mesh.getTexCoordsSize()) {
207 glEnableVertexAttribArray(Program::texCoords);
208 glVertexAttribPointer(Program::texCoords,
209 mesh.getTexCoordsSize(),
211 mesh.getByteStride(),
212 mesh.getTexCoords());
215 glVertexAttribPointer(Program::position,
216 mesh.getVertexSize(),
218 mesh.getByteStride(),
219 mesh.getPositions());
221 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
223 if (mesh.getTexCoordsSize()) {
224 glDisableVertexAttribArray(Program::texCoords);
228 void GLES20RenderEngine::dump(String8& result) {
229 RenderEngine::dump(result);
232 // ---------------------------------------------------------------------------
233 }; // namespace android
234 // ---------------------------------------------------------------------------
237 #error "don't include gl/gl.h in this file"