Home | History | Annotate | Download | only in libOpenglRender
      1 /*
      2 * Copyright (C) 2011 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 #ifndef _LIBRENDER_FRAMEBUFFER_H
     17 #define _LIBRENDER_FRAMEBUFFER_H
     18 
     19 #include "libOpenglRender/render_api.h"
     20 #include "ColorBuffer.h"
     21 #include "RenderContext.h"
     22 #include "WindowSurface.h"
     23 #include <utils/threads.h>
     24 #include <map>
     25 #include <EGL/egl.h>
     26 #include <stdint.h>
     27 
     28 typedef uint32_t HandleType;
     29 typedef std::map<HandleType, RenderContextPtr> RenderContextMap;
     30 typedef std::map<HandleType, WindowSurfacePtr> WindowSurfaceMap;
     31 typedef std::map<HandleType, ColorBufferPtr> ColorBufferMap;
     32 
     33 struct FrameBufferCaps
     34 {
     35     bool hasGL2;
     36     bool has_eglimage_texture_2d;
     37     bool has_eglimage_renderbuffer;
     38     EGLint eglMajor;
     39     EGLint eglMinor;
     40 };
     41 
     42 class FrameBuffer
     43 {
     44 public:
     45     static bool initialize(int width, int height);
     46     static bool setupSubWindow(FBNativeWindowType p_window,
     47                                 int x, int y,
     48                                 int width, int height, float zRot);
     49     static bool removeSubWindow();
     50     static void finalize();
     51     static FrameBuffer *getFB() { return s_theFrameBuffer; }
     52 
     53     const FrameBufferCaps &getCaps() const { return m_caps; }
     54 
     55     int getWidth() const { return m_width; }
     56     int getHeight() const { return m_height; }
     57 
     58     HandleType createRenderContext(int p_config, HandleType p_share, bool p_isGL2 = false);
     59     HandleType createWindowSurface(int p_config, int p_width, int p_height);
     60     HandleType createColorBuffer(int p_width, int p_height, GLenum p_internalFormat);
     61     void DestroyRenderContext(HandleType p_context);
     62     void DestroyWindowSurface(HandleType p_surface);
     63     void DestroyColorBuffer(HandleType p_colorbuffer);
     64 
     65     bool  bindContext(HandleType p_context, HandleType p_drawSurface, HandleType p_readSurface);
     66     bool  setWindowSurfaceColorBuffer(HandleType p_surface, HandleType p_colorbuffer);
     67     bool  flushWindowSurfaceColorBuffer(HandleType p_surface);
     68     bool  bindColorBufferToTexture(HandleType p_colorbuffer);
     69     bool  bindColorBufferToRenderbuffer(HandleType p_colorbuffer);
     70     bool updateColorBuffer(HandleType p_colorbuffer,
     71                            int x, int y, int width, int height,
     72                            GLenum format, GLenum type, void *pixels);
     73 
     74     bool post(HandleType p_colorbuffer, bool needLock = true);
     75     bool repost();
     76 
     77     EGLDisplay getDisplay() const { return m_eglDisplay; }
     78     EGLNativeWindowType getSubWindow() const { return m_subWin; }
     79     bool bind_locked();
     80     bool unbind_locked();
     81 
     82     void setDisplayRotation(float zRot) {
     83         m_zRot = zRot;
     84         repost();
     85     }
     86 
     87 private:
     88     FrameBuffer(int p_width, int p_height);
     89     ~FrameBuffer();
     90     HandleType genHandle();
     91     bool bindSubwin_locked();
     92     void initGLState();
     93 
     94 private:
     95     static FrameBuffer *s_theFrameBuffer;
     96     static HandleType s_nextHandle;
     97     int m_x;
     98     int m_y;
     99     int m_width;
    100     int m_height;
    101     android::Mutex m_lock;
    102     FBNativeWindowType m_nativeWindow;
    103     FrameBufferCaps m_caps;
    104     EGLDisplay m_eglDisplay;
    105     RenderContextMap m_contexts;
    106     WindowSurfaceMap m_windows;
    107     ColorBufferMap m_colorbuffers;
    108 
    109     EGLSurface m_eglSurface;
    110     EGLContext m_eglContext;
    111     EGLSurface m_pbufSurface;
    112     EGLContext m_pbufContext;
    113 
    114     EGLContext m_prevContext;
    115     EGLSurface m_prevReadSurf;
    116     EGLSurface m_prevDrawSurf;
    117     EGLNativeWindowType m_subWin;
    118     EGLNativeDisplayType m_subWinDisplay;
    119     EGLConfig  m_eglConfig;
    120     HandleType m_lastPostedColorBuffer;
    121     float      m_zRot;
    122     bool       m_eglContextInitialized;
    123 
    124     int m_statsNumFrames;
    125     long long m_statsStartTime;
    126     bool m_fpsStats;
    127 };
    128 #endif
    129