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);
     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     void setPostCallback(OnPostFn onPost, void* onPostContext);
     63 
     64     void getGLStrings(const char** vendor, const char** renderer, const char** version) const {
     65         *vendor = m_glVendor;
     66         *renderer = m_glRenderer;
     67         *version = m_glVersion;
     68     }
     69 
     70     HandleType createRenderContext(int p_config, HandleType p_share, bool p_isGL2 = false);
     71     HandleType createWindowSurface(int p_config, int p_width, int p_height);
     72     HandleType createColorBuffer(int p_width, int p_height, GLenum p_internalFormat);
     73     void DestroyRenderContext(HandleType p_context);
     74     void DestroyWindowSurface(HandleType p_surface);
     75     void openColorBuffer(HandleType p_colorbuffer);
     76     void closeColorBuffer(HandleType p_colorbuffer);
     77 
     78     bool  bindContext(HandleType p_context, HandleType p_drawSurface, HandleType p_readSurface);
     79     bool  setWindowSurfaceColorBuffer(HandleType p_surface, HandleType p_colorbuffer);
     80     bool  flushWindowSurfaceColorBuffer(HandleType p_surface);
     81     bool  bindColorBufferToTexture(HandleType p_colorbuffer);
     82     bool  bindColorBufferToRenderbuffer(HandleType p_colorbuffer);
     83     bool updateColorBuffer(HandleType p_colorbuffer,
     84                            int x, int y, int width, int height,
     85                            GLenum format, GLenum type, void *pixels);
     86 
     87     bool post(HandleType p_colorbuffer, bool needLock = true);
     88     bool repost();
     89 
     90     EGLDisplay getDisplay() const { return m_eglDisplay; }
     91     EGLNativeWindowType getSubWindow() const { return m_subWin; }
     92     bool bind_locked();
     93     bool unbind_locked();
     94 
     95     void setDisplayRotation(float zRot) {
     96         m_zRot = zRot;
     97         repost();
     98     }
     99 
    100 private:
    101     FrameBuffer(int p_width, int p_height);
    102     ~FrameBuffer();
    103     HandleType genHandle();
    104     bool bindSubwin_locked();
    105     void initGLState();
    106 
    107 private:
    108     static FrameBuffer *s_theFrameBuffer;
    109     static HandleType s_nextHandle;
    110     int m_x;
    111     int m_y;
    112     int m_width;
    113     int m_height;
    114     android::Mutex m_lock;
    115     FBNativeWindowType m_nativeWindow;
    116     FrameBufferCaps m_caps;
    117     EGLDisplay m_eglDisplay;
    118     RenderContextMap m_contexts;
    119     WindowSurfaceMap m_windows;
    120     ColorBufferMap m_colorbuffers;
    121 
    122     EGLSurface m_eglSurface;
    123     EGLContext m_eglContext;
    124     EGLSurface m_pbufSurface;
    125     EGLContext m_pbufContext;
    126 
    127     EGLContext m_prevContext;
    128     EGLSurface m_prevReadSurf;
    129     EGLSurface m_prevDrawSurf;
    130     EGLNativeWindowType m_subWin;
    131     EGLNativeDisplayType m_subWinDisplay;
    132     EGLConfig  m_eglConfig;
    133     HandleType m_lastPostedColorBuffer;
    134     float      m_zRot;
    135     bool       m_eglContextInitialized;
    136 
    137     int m_statsNumFrames;
    138     long long m_statsStartTime;
    139     bool m_fpsStats;
    140 
    141     OnPostFn m_onPost;
    142     void* m_onPostContext;
    143     unsigned char* m_fbImage;
    144 
    145     const char* m_glVendor;
    146     const char* m_glRenderer;
    147     const char* m_glVersion;
    148 };
    149 #endif
    150