blob: 9754758caf4adda19960ea205782ff588d797436 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
2 * Copyright 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 ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070020#include <GLES2/gl2ext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070021
22#include <utils/String8.h>
23#include <utils/Trace.h>
24
25#include <cutils/compiler.h>
26
27#include "GLES20RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070028#include "Program.h"
29#include "ProgramCache.h"
30#include "Description.h"
31#include "Mesh.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070032#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070033
34// ---------------------------------------------------------------------------
35namespace android {
36// ---------------------------------------------------------------------------
37
38GLES20RenderEngine::GLES20RenderEngine() {
39
40 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
41 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
42
43 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
44 glPixelStorei(GL_PACK_ALIGNMENT, 4);
45
46 struct pack565 {
47 inline uint16_t operator() (int r, int g, int b) const {
48 return (r<<11)|(g<<5)|b;
49 }
50 } pack565;
51
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);
61}
62
63GLES20RenderEngine::~GLES20RenderEngine() {
64}
65
66
67size_t GLES20RenderEngine::getMaxTextureSize() const {
68 return mMaxTextureSize;
69}
70
71size_t GLES20RenderEngine::getMaxViewportDims() const {
72 return
73 mMaxViewportDims[0] < mMaxViewportDims[1] ?
74 mMaxViewportDims[0] : mMaxViewportDims[1];
75}
76
77void GLES20RenderEngine::setViewportAndProjection(
78 size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
Mathias Agopiana8c386f2013-08-26 20:42:07 -070079 mat4 m;
80 if (yswap) m = mat4::ortho(0, w, h, 0, 0, 1);
81 else m = mat4::ortho(0, w, 0, h, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -070082
83 glViewport(0, 0, vpw, vph);
84 mState.setProjectionMatrix(m);
85}
86
87void GLES20RenderEngine::setupLayerBlending(
88 bool premultipliedAlpha, bool opaque, int alpha) {
89
90 mState.setPremultipliedAlpha(premultipliedAlpha);
91 mState.setOpaque(opaque);
92 mState.setPlaneAlpha(alpha / 255.0f);
93
94 if (alpha < 0xFF || !opaque) {
95 glEnable(GL_BLEND);
96 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97 } else {
98 glDisable(GL_BLEND);
99 }
100}
101
102void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
Mathias Agopianbcab7b12013-08-23 16:12:34 -0700103 mState.setPlaneAlpha(1.0f);
104 mState.setPremultipliedAlpha(true);
105 mState.setOpaque(false);
106 mState.setColor(0, 0, 0, alpha/255.0f);
107 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700108
109 if (alpha == 0xFF) {
110 glDisable(GL_BLEND);
111 } else {
112 glEnable(GL_BLEND);
113 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
114 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700115}
116
Mathias Agopian49457ac2013-08-14 18:20:17 -0700117void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
118 GLuint target = texture.getTextureTarget();
119 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700120 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700121 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700122 filter = GL_LINEAR;
123 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700124 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
125 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
126 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
127 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700128
Mathias Agopian49457ac2013-08-14 18:20:17 -0700129 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700130}
131
132void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700133 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700134 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
135 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
136 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700137}
138
139void GLES20RenderEngine::disableTexturing() {
140 mState.disableTexture();
141}
142
143void GLES20RenderEngine::disableBlending() {
144 glDisable(GL_BLEND);
145}
146
Mathias Agopian458197d2013-08-15 14:56:51 -0700147
148void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
149 uint32_t* texName, uint32_t* fbName, uint32_t* status) {
150 GLuint tname, name;
151 // turn our EGLImage into a texture
152 glGenTextures(1, &tname);
153 glBindTexture(GL_TEXTURE_2D, tname);
154 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
155
156 // create a Framebuffer Object to render into
157 glGenFramebuffers(1, &name);
158 glBindFramebuffer(GL_FRAMEBUFFER, name);
159 glFramebufferTexture2D(GL_FRAMEBUFFER,
160 GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
161
162 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
163 *texName = tname;
164 *fbName = name;
165}
166
167void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
168 glBindFramebuffer(GL_FRAMEBUFFER, 0);
169 glDeleteFramebuffers(1, &fbName);
170 glDeleteTextures(1, &texName);
171}
172
Mathias Agopian19733a32013-08-28 18:13:56 -0700173void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
174 mState.setPlaneAlpha(1.0f);
175 mState.setPremultipliedAlpha(true);
176 mState.setOpaque(false);
Mathias Agopian3f844832013-08-07 21:24:32 -0700177 mState.setColor(r, g, b, a);
Mathias Agopian19733a32013-08-28 18:13:56 -0700178 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700179 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700180}
181
182void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
183
184 ProgramCache::getInstance().useProgram(mState);
185
186 if (mesh.getTexCoordsSize()) {
187 glEnableVertexAttribArray(Program::texCoords);
188 glVertexAttribPointer(Program::texCoords,
189 mesh.getTexCoordsSize(),
190 GL_FLOAT, GL_FALSE,
191 mesh.getByteStride(),
192 mesh.getTexCoords());
193 }
194
195 glVertexAttribPointer(Program::position,
196 mesh.getVertexSize(),
197 GL_FLOAT, GL_FALSE,
198 mesh.getByteStride(),
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700199 mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700200
201 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
202
203 if (mesh.getTexCoordsSize()) {
204 glDisableVertexAttribArray(Program::texCoords);
205 }
206}
207
208void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700209 RenderEngine::dump(result);
Mathias Agopian3f844832013-08-07 21:24:32 -0700210}
211
212// ---------------------------------------------------------------------------
213}; // namespace android
214// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700215
216#if defined(__gl_h_)
217#error "don't include gl/gl.h in this file"
218#endif