Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2015 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 package org.webrtc;
     29 
     30 import android.opengl.GLES20;
     31 
     32 /**
     33  * Helper class for handling OpenGL framebuffer with only color attachment and no depth or stencil
     34  * buffer. Intended for simple tasks such as texture copy, texture downscaling, and texture color
     35  * conversion.
     36  */
     37 // TODO(magjed): Add unittests for this class.
     38 public class GlTextureFrameBuffer {
     39   private final int frameBufferId;
     40   private final int textureId;
     41   private final int pixelFormat;
     42   private int width;
     43   private int height;
     44 
     45   /**
     46    * Generate texture and framebuffer resources. An EGLContext must be bound on the current thread
     47    * when calling this function. The framebuffer is not complete until setSize() is called.
     48    */
     49   public GlTextureFrameBuffer(int pixelFormat) {
     50     switch (pixelFormat) {
     51       case GLES20.GL_LUMINANCE:
     52       case GLES20.GL_RGB:
     53       case GLES20.GL_RGBA:
     54         this.pixelFormat = pixelFormat;
     55         break;
     56       default:
     57         throw new IllegalArgumentException("Invalid pixel format: " + pixelFormat);
     58     }
     59 
     60     textureId = GlUtil.generateTexture(GLES20.GL_TEXTURE_2D);
     61     this.width = 0;
     62     this.height = 0;
     63 
     64     // Create framebuffer object and bind it.
     65     final int frameBuffers[] = new int[1];
     66     GLES20.glGenFramebuffers(1, frameBuffers, 0);
     67     frameBufferId = frameBuffers[0];
     68     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId);
     69     GlUtil.checkNoGLES2Error("Generate framebuffer");
     70 
     71     // Attach the texture to the framebuffer as color attachment.
     72     GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
     73         GLES20.GL_TEXTURE_2D, textureId, 0);
     74     GlUtil.checkNoGLES2Error("Attach texture to framebuffer");
     75 
     76     // Restore normal framebuffer.
     77     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
     78   }
     79 
     80   /**
     81    * (Re)allocate texture. Will do nothing if the requested size equals the current size. An
     82    * EGLContext must be bound on the current thread when calling this function. Must be called at
     83    * least once before using the framebuffer. May be called multiple times to change size.
     84    */
     85   public void setSize(int width, int height) {
     86     if (width == 0 || height == 0) {
     87       throw new IllegalArgumentException("Invalid size: " + width + "x" + height);
     88     }
     89     if (width == this.width && height == this.height) {
     90       return;
     91     }
     92     this.width = width;
     93     this.height = height;
     94 
     95     // Bind our framebuffer.
     96     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId);
     97     GlUtil.checkNoGLES2Error("glBindFramebuffer");
     98 
     99     // Allocate texture.
    100     GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    101     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    102     GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, pixelFormat, width, height, 0, pixelFormat,
    103         GLES20.GL_UNSIGNED_BYTE, null);
    104 
    105     // Check that the framebuffer is in a good state.
    106     final int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    107     if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
    108       throw new IllegalStateException("Framebuffer not complete, status: " + status);
    109     }
    110 
    111     // Restore normal framebuffer.
    112     GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    113     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    114   }
    115 
    116   public int getWidth() {
    117     return width;
    118   }
    119 
    120   public int getHeight() {
    121     return height;
    122   }
    123 
    124   public int getFrameBufferId() {
    125     return frameBufferId;
    126   }
    127 
    128   public int getTextureId() {
    129     return textureId;
    130   }
    131 
    132   /**
    133    * Release texture and framebuffer. An EGLContext must be bound on the current thread when calling
    134    * this function. This object should not be used after this call.
    135    */
    136   public void release() {
    137     GLES20.glDeleteTextures(1, new int[] {textureId}, 0);
    138     GLES20.glDeleteFramebuffers(1, new int[] {frameBufferId}, 0);
    139     width = 0;
    140     height = 0;
    141   }
    142 }
    143