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