1 /*
2  * Copyright (C) 2014 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 EGLMANAGER_H
17 #define EGLMANAGER_H
18 
19 #include <cutils/compiler.h>
20 #include <EGL/egl.h>
21 #include <ui/GraphicBuffer.h>
22 #include <utils/StrongPointer.h>
23 
24 namespace android {
25 namespace uirenderer {
26 namespace renderthread {
27 
28 class RenderThread;
29 
30 // This class contains the shared global EGL objects, such as EGLDisplay
31 // and EGLConfig, which are re-used by CanvasContext
32 class EglManager {
33 public:
34     // Returns true on success, false on failure
35     void initialize();
36 
37     bool hasEglContext();
38     void requireGlContext();
39 
40     void usePBufferSurface();
41     EGLSurface createSurface(EGLNativeWindowType window);
42     void destroySurface(EGLSurface surface);
43 
44     void destroy();
45 
isCurrent(EGLSurface surface)46     bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
47     // Returns true if the current surface changed, false if it was already current
48     bool makeCurrent(EGLSurface surface);
49     void beginFrame(EGLSurface surface, EGLint* width, EGLint* height);
50     bool swapBuffers(EGLSurface surface);
51     void cancelFrame();
52 
53     // Returns true iff the surface is now preserving buffers.
54     bool setPreserveBuffer(EGLSurface surface, bool preserve);
55 
56     void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
57 
58 private:
59     friend class RenderThread;
60 
61     EglManager(RenderThread& thread);
62     // EglContext is never destroyed, method is purposely not implemented
63     ~EglManager();
64 
65     void loadConfig();
66     void createContext();
67     void initAtlas();
68 
69     RenderThread& mRenderThread;
70 
71     EGLDisplay mEglDisplay;
72     EGLConfig mEglConfig;
73     EGLContext mEglContext;
74     EGLSurface mPBufferSurface;
75 
76     const bool mAllowPreserveBuffer;
77     bool mCanSetPreserveBuffer;
78 
79     EGLSurface mCurrentSurface;
80 
81     sp<GraphicBuffer> mAtlasBuffer;
82     int64_t* mAtlasMap;
83     size_t mAtlasMapSize;
84 
85     // Whether or not we are in the middle of drawing a frame. This is used
86     // to avoid switching surfaces mid-frame if requireGlContext() is called
87     // TODO: Need to be better about surface/context management so that this isn't
88     // necessary
89     bool mInFrame;
90 };
91 
92 } /* namespace renderthread */
93 } /* namespace uirenderer */
94 } /* namespace android */
95 
96 #endif /* EGLMANAGER_H */
97