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 struct ColorBufferRef {
     30     ColorBufferPtr cb;
     31     uint32_t refcount;  // number of client-side references
     32 };
     33 typedef std::map<HandleType, RenderContextPtr> RenderContextMap;
     34 typedef std::map<HandleType, WindowSurfacePtr> WindowSurfaceMap;
     35 typedef std::map<HandleType, ColorBufferRef> ColorBufferMap;
     36 
     37 struct FrameBufferCaps
     38 {
     39     bool hasGL2;
     40     bool has_eglimage_texture_2d;
     41     bool has_eglimage_renderbuffer;
     42     EGLint eglMajor;
     43     EGLint eglMinor;
     44 };
     45 
     46 class FrameBuffer
     47 {
     48 public:
     49     static bool initialize(int width, int height, OnPostFn onPost, void* onPostContext);
     50     static bool setupSubWindow(FBNativeWindowType p_window,
     51                                 int x, int y,
     52                                 int width, int height, float zRot);
     53     static bool removeSubWindow();
     54     static void finalize();
     55     static FrameBuffer *getFB() { return s_theFrameBuffer; }
     56 
     57     const FrameBufferCaps &getCaps() const { return m_caps; }
     58 
     59     int getWidth() const { return m_width; }
     60     int getHeight() const { return m_height; }
     61 
     62     HandleType createRenderContext(int p_config, HandleType p_share, bool p_isGL2 = false);
     63     HandleType createWindowSurface(int p_config, int p_width, int p_height);
     64     HandleType createColorBuffer(int p_width, int p_height, GLenum p_internalFormat);
     65     void DestroyRenderContext(HandleType p_context);
     66     void DestroyWindowSurface(HandleType p_surface);
     67     void openColorBuffer(HandleType p_colorbuffer);
     68     void closeColorBuffer(HandleType p_colorbuffer);
     69 
     70     bool  bindContext(HandleType p_context, HandleType p_drawSurface, HandleType p_readSurface);
     71     bool  setWindowSurfaceColorBuffer(HandleType p_surface, HandleType p_colorbuffer);
     72     bool  flushWindowSurfaceColorBuffer(HandleType p_surface);
     73     bool  bindColorBufferToTexture(HandleType p_colorbuffer);
     74     bool  bindColorBufferToRenderbuffer(HandleType p_colorbuffer);
     75     bool updateColorBuffer(HandleType p_colorbuffer,
     76                            int x, int y, int width, int height,
     77                            GLenum format, GLenum type, void *pixels);
     78 
     79     bool post(HandleType p_colorbuffer, bool needLock = true);
     80     bool repost();
     81 
     82     EGLDisplay getDisplay() const { return m_eglDisplay; }
     83     EGLNativeWindowType getSubWindow() const { return m_subWin; }
     84     bool bind_locked();
     85     bool unbind_locked();
     86 
     87     void setDisplayRotation(float zRot) {
     88         m_zRot = zRot;
     89         repost();
     90     }
     91 
     92 private:
     93     FrameBuffer(int p_width, int p_height, OnPostFn onPost, void* onPostContext);
     94     ~FrameBuffer();
     95     HandleType genHandle();
     96     bool bindSubwin_locked();
     97     void initGLState();
     98 
     99 private:
    100     static FrameBuffer *s_theFrameBuffer;
    101     static HandleType s_nextHandle;
    102     int m_x;
    103     int m_y;
    104     int m_width;
    105     int m_height;
    106     android::Mutex m_lock;
    107     FBNativeWindowType m_nativeWindow;
    108     FrameBufferCaps m_caps;
    109     EGLDisplay m_eglDisplay;
    110     RenderContextMap m_contexts;
    111     WindowSurfaceMap m_windows;
    112     ColorBufferMap m_colorbuffers;
    113 
    114     EGLSurface m_eglSurface;
    115     EGLContext m_eglContext;
    116     EGLSurface m_pbufSurface;
    117     EGLContext m_pbufContext;
    118 
    119     EGLContext m_prevContext;
    120     EGLSurface m_prevReadSurf;
    121     EGLSurface m_prevDrawSurf;
    122     EGLNativeWindowType m_subWin;
    123     EGLNativeDisplayType m_subWinDisplay;
    124     EGLConfig  m_eglConfig;
    125     HandleType m_lastPostedColorBuffer;
    126     float      m_zRot;
    127     bool       m_eglContextInitialized;
    128 
    129     int m_statsNumFrames;
    130     long long m_statsStartTime;
    131     bool m_fpsStats;
    132 
    133     OnPostFn m_onPost;
    134     void* m_onPostContext;
    135     unsigned char* m_fbImage;
    136 };
    137 #endif
    138