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
|
/*
* 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.
*/
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <utils/String8.h>
#include <utils/Trace.h>
#include <cutils/compiler.h>
#include "GLES20RenderEngine.h"
#include "Program.h"
#include "ProgramCache.h"
#include "Description.h"
#include "Mesh.h"
#include "Texture.h"
// ---------------------------------------------------------------------------
namespace android {
// ---------------------------------------------------------------------------
GLES20RenderEngine::GLES20RenderEngine() {
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(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);
}
GLES20RenderEngine::~GLES20RenderEngine() {
}
size_t GLES20RenderEngine::getMaxTextureSize() const {
return mMaxTextureSize;
}
size_t GLES20RenderEngine::getMaxViewportDims() const {
return
mMaxViewportDims[0] < mMaxViewportDims[1] ?
mMaxViewportDims[0] : mMaxViewportDims[1];
}
void GLES20RenderEngine::setViewportAndProjection(
size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
struct ortho {
inline void operator() (GLfloat *m,
GLfloat left, GLfloat right, GLfloat bottom, GLfloat top,
GLfloat near, GLfloat far) const {
memset(m, 0, 16*sizeof(GLfloat));
m[ 0] = 2.0f / (right - left);
m[ 5] = 2.0f / (top - bottom);
m[10] =-2.0f / (far - near);
m[15] = 1.0f;
m[12] = -(right + left) / (right - left);
m[13] = -(top + bottom) / (top - bottom);
m[14] = -(far + near) / (far - near);
}
} ortho;
GLfloat m[16];
if (yswap) ortho(m, 0, w, h, 0, 0, 1);
else ortho(m, 0, w, 0, h, 0, 1);
glViewport(0, 0, vpw, vph);
mState.setProjectionMatrix(m);
}
void GLES20RenderEngine::setupLayerBlending(
bool premultipliedAlpha, bool opaque, int alpha) {
mState.setPremultipliedAlpha(premultipliedAlpha);
mState.setOpaque(opaque);
mState.setPlaneAlpha(alpha / 255.0f);
if (alpha < 0xFF || !opaque) {
glEnable(GL_BLEND);
glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else {
glDisable(GL_BLEND);
}
}
void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
mState.setPlaneAlpha(1.0f);
mState.setPremultipliedAlpha(true);
mState.setOpaque(false);
mState.setColor(0, 0, 0, alpha/255.0f);
mState.disableTexture();
if (alpha == 0xFF) {
glDisable(GL_BLEND);
} else {
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
}
void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
GLuint target = texture.getTextureTarget();
glBindTexture(target, texture.getTextureName());
GLenum filter = GL_NEAREST;
if (texture.getFiltering()) {
filter = GL_LINEAR;
}
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
mState.setTexture(texture);
}
void GLES20RenderEngine::setupLayerBlackedOut() {
glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
mState.setTexture(texture);
}
void GLES20RenderEngine::disableTexturing() {
mState.disableTexture();
}
void GLES20RenderEngine::disableBlending() {
glDisable(GL_BLEND);
}
void GLES20RenderEngine::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
glGenFramebuffers(1, &name);
glBindFramebuffer(GL_FRAMEBUFFER, name);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
*status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
*texName = tname;
*fbName = name;
}
void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbName);
glDeleteTextures(1, &texName);
}
void GLES20RenderEngine::fillWithColor(const Mesh& mesh, float r, float g, float b, float a) {
mState.setColor(r, g, b, a);
disableTexturing();
glDisable(GL_BLEND);
ProgramCache::getInstance().useProgram(mState);
glVertexAttribPointer(Program::position,
mesh.getVertexSize(),
GL_FLOAT, GL_FALSE,
mesh.getByteStride(),
mesh.getPositions());
glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
}
void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
ProgramCache::getInstance().useProgram(mState);
if (mesh.getTexCoordsSize()) {
glEnableVertexAttribArray(Program::texCoords);
glVertexAttribPointer(Program::texCoords,
mesh.getTexCoordsSize(),
GL_FLOAT, GL_FALSE,
mesh.getByteStride(),
mesh.getTexCoords());
}
glVertexAttribPointer(Program::position,
mesh.getVertexSize(),
GL_FLOAT, GL_FALSE,
mesh.getByteStride(),
mesh.getPositions());
glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
if (mesh.getTexCoordsSize()) {
glDisableVertexAttribArray(Program::texCoords);
}
}
void GLES20RenderEngine::dump(String8& result) {
RenderEngine::dump(result);
}
// ---------------------------------------------------------------------------
}; // namespace android
// ---------------------------------------------------------------------------
#if defined(__gl_h_)
#error "don't include gl/gl.h in this file"
#endif
|