Home | History | Annotate | Download | only in RenderEngine
      1 /*
      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>
     20 #include <GLES2/gl2ext.h>
     21 
     22 #include <utils/String8.h>
     23 #include <utils/Trace.h>
     24 
     25 #include <cutils/compiler.h>
     26 
     27 #include "GLES20RenderEngine.h"
     28 #include "Program.h"
     29 #include "ProgramCache.h"
     30 #include "Description.h"
     31 #include "Mesh.h"
     32 #include "Texture.h"
     33 
     34 // ---------------------------------------------------------------------------
     35 namespace android {
     36 // ---------------------------------------------------------------------------
     37 
     38 GLES20RenderEngine::GLES20RenderEngine() :
     39         mVpWidth(0), mVpHeight(0) {
     40 
     41     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
     42     glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
     43 
     44     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
     45     glPixelStorei(GL_PACK_ALIGNMENT, 4);
     46 
     47     struct pack565 {
     48         inline uint16_t operator() (int r, int g, int b) const {
     49             return (r<<11)|(g<<5)|b;
     50         }
     51     } pack565;
     52 
     53     const uint16_t protTexData[] = { 0 };
     54     glGenTextures(1, &mProtectedTexName);
     55     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
     56     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     57     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     58     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     59     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     60     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
     61             GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
     62 
     63     //mColorBlindnessCorrection = M;
     64 }
     65 
     66 GLES20RenderEngine::~GLES20RenderEngine() {
     67 }
     68 
     69 
     70 size_t GLES20RenderEngine::getMaxTextureSize() const {
     71     return mMaxTextureSize;
     72 }
     73 
     74 size_t GLES20RenderEngine::getMaxViewportDims() const {
     75     return
     76         mMaxViewportDims[0] < mMaxViewportDims[1] ?
     77             mMaxViewportDims[0] : mMaxViewportDims[1];
     78 }
     79 
     80 void GLES20RenderEngine::setViewportAndProjection(
     81         size_t vpw, size_t vph, size_t w, size_t h, bool yswap) {
     82     mat4 m;
     83     if (yswap)  m = mat4::ortho(0, w, h, 0, 0, 1);
     84     else        m = mat4::ortho(0, w, 0, h, 0, 1);
     85 
     86     glViewport(0, 0, vpw, vph);
     87     mState.setProjectionMatrix(m);
     88     mVpWidth = vpw;
     89     mVpHeight = vph;
     90 }
     91 
     92 void GLES20RenderEngine::setupLayerBlending(
     93     bool premultipliedAlpha, bool opaque, int alpha) {
     94 
     95     mState.setPremultipliedAlpha(premultipliedAlpha);
     96     mState.setOpaque(opaque);
     97     mState.setPlaneAlpha(alpha / 255.0f);
     98 
     99     if (alpha < 0xFF || !opaque) {
    100         glEnable(GL_BLEND);
    101         glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    102     } else {
    103         glDisable(GL_BLEND);
    104     }
    105 }
    106 
    107 void GLES20RenderEngine::setupDimLayerBlending(int alpha) {
    108     mState.setPlaneAlpha(1.0f);
    109     mState.setPremultipliedAlpha(true);
    110     mState.setOpaque(false);
    111     mState.setColor(0, 0, 0, alpha/255.0f);
    112     mState.disableTexture();
    113 
    114     if (alpha == 0xFF) {
    115         glDisable(GL_BLEND);
    116     } else {
    117         glEnable(GL_BLEND);
    118         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    119     }
    120 }
    121 
    122 void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
    123     GLuint target = texture.getTextureTarget();
    124     glBindTexture(target, texture.getTextureName());
    125     GLenum filter = GL_NEAREST;
    126     if (texture.getFiltering()) {
    127         filter = GL_LINEAR;
    128     }
    129     glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    130     glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    131     glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
    132     glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
    133 
    134     mState.setTexture(texture);
    135 }
    136 
    137 void GLES20RenderEngine::setupLayerBlackedOut() {
    138     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
    139     Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
    140     texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
    141     mState.setTexture(texture);
    142 }
    143 
    144 void GLES20RenderEngine::disableTexturing() {
    145     mState.disableTexture();
    146 }
    147 
    148 void GLES20RenderEngine::disableBlending() {
    149     glDisable(GL_BLEND);
    150 }
    151 
    152 
    153 void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
    154         uint32_t* texName, uint32_t* fbName, uint32_t* status) {
    155     GLuint tname, name;
    156     // turn our EGLImage into a texture
    157     glGenTextures(1, &tname);
    158     glBindTexture(GL_TEXTURE_2D, tname);
    159     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
    160 
    161     // create a Framebuffer Object to render into
    162     glGenFramebuffers(1, &name);
    163     glBindFramebuffer(GL_FRAMEBUFFER, name);
    164     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
    165 
    166     *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    167     *texName = tname;
    168     *fbName = name;
    169 }
    170 
    171 void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
    172     glBindFramebuffer(GL_FRAMEBUFFER, 0);
    173     glDeleteFramebuffers(1, &fbName);
    174     glDeleteTextures(1, &texName);
    175 }
    176 
    177 void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
    178     mState.setPlaneAlpha(1.0f);
    179     mState.setPremultipliedAlpha(true);
    180     mState.setOpaque(false);
    181     mState.setColor(r, g, b, a);
    182     mState.disableTexture();
    183     glDisable(GL_BLEND);
    184 }
    185 
    186 void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
    187 
    188     ProgramCache::getInstance().useProgram(mState);
    189 
    190     if (mesh.getTexCoordsSize()) {
    191         glEnableVertexAttribArray(Program::texCoords);
    192         glVertexAttribPointer(Program::texCoords,
    193                 mesh.getTexCoordsSize(),
    194                 GL_FLOAT, GL_FALSE,
    195                 mesh.getByteStride(),
    196                 mesh.getTexCoords());
    197     }
    198 
    199     glVertexAttribPointer(Program::position,
    200             mesh.getVertexSize(),
    201             GL_FLOAT, GL_FALSE,
    202             mesh.getByteStride(),
    203             mesh.getPositions());
    204 
    205     glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
    206 
    207     if (mesh.getTexCoordsSize()) {
    208         glDisableVertexAttribArray(Program::texCoords);
    209     }
    210 }
    211 
    212 void GLES20RenderEngine::beginGroup(const mat4& colorTransform) {
    213 
    214     GLuint tname, name;
    215     // create the texture
    216     glGenTextures(1, &tname);
    217     glBindTexture(GL_TEXTURE_2D, tname);
    218     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    219     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    220     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    221     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    222     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mVpWidth, mVpHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    223 
    224     // create a Framebuffer Object to render into
    225     glGenFramebuffers(1, &name);
    226     glBindFramebuffer(GL_FRAMEBUFFER, name);
    227     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
    228 
    229     Group group;
    230     group.texture = tname;
    231     group.fbo = name;
    232     group.width = mVpWidth;
    233     group.height = mVpHeight;
    234     group.colorTransform = colorTransform;
    235 
    236     mGroupStack.push(group);
    237 }
    238 
    239 void GLES20RenderEngine::endGroup() {
    240 
    241     const Group group(mGroupStack.top());
    242     mGroupStack.pop();
    243 
    244     // activate the previous render target
    245     GLuint fbo = 0;
    246     if (!mGroupStack.isEmpty()) {
    247         fbo = mGroupStack.top().fbo;
    248     }
    249     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    250 
    251     // set our state
    252     Texture texture(Texture::TEXTURE_2D, group.texture);
    253     texture.setDimensions(group.width, group.height);
    254     glBindTexture(GL_TEXTURE_2D, group.texture);
    255 
    256     mState.setPlaneAlpha(1.0f);
    257     mState.setPremultipliedAlpha(true);
    258     mState.setOpaque(false);
    259     mState.setTexture(texture);
    260     mState.setColorMatrix(group.colorTransform);
    261     glDisable(GL_BLEND);
    262 
    263     Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2, 2);
    264     Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
    265     Mesh::VertexArray<vec2> texCoord(mesh.getTexCoordArray<vec2>());
    266     position[0] = vec2(0, 0);
    267     position[1] = vec2(group.width, 0);
    268     position[2] = vec2(group.width, group.height);
    269     position[3] = vec2(0, group.height);
    270     texCoord[0] = vec2(0, 0);
    271     texCoord[1] = vec2(1, 0);
    272     texCoord[2] = vec2(1, 1);
    273     texCoord[3] = vec2(0, 1);
    274     drawMesh(mesh);
    275 
    276     // reset color matrix
    277     mState.setColorMatrix(mat4());
    278 
    279     // free our fbo and texture
    280     glDeleteFramebuffers(1, &group.fbo);
    281     glDeleteTextures(1, &group.texture);
    282 }
    283 
    284 void GLES20RenderEngine::dump(String8& result) {
    285     RenderEngine::dump(result);
    286 }
    287 
    288 // ---------------------------------------------------------------------------
    289 }; // namespace android
    290 // ---------------------------------------------------------------------------
    291 
    292 #if defined(__gl_h_)
    293 #error "don't include gl/gl.h in this file"
    294 #endif
    295