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 <SkRect.h>
22 #include <ui/GraphicBuffer.h>
23 #include <utils/StrongPointer.h>
24 
25 namespace android {
26 namespace uirenderer {
27 namespace renderthread {
28 
29 class RenderThread;
30 
31 // This class contains the shared global EGL objects, such as EGLDisplay
32 // and EGLConfig, which are re-used by CanvasContext
33 class EglManager {
34 public:
35     // Returns true on success, false on failure
36     void initialize();
37 
38     bool hasEglContext();
39 
40     EGLSurface createSurface(EGLNativeWindowType window);
41     void destroySurface(EGLSurface surface);
42 
43     void destroy();
44 
isCurrent(EGLSurface surface)45     bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
46     // Returns true if the current surface changed, false if it was already current
47     bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
48     void beginFrame(EGLSurface surface, EGLint* width, EGLint* height);
49     bool swapBuffers(EGLSurface surface, const SkRect& dirty, EGLint width, EGLint height);
50 
51     // Returns true iff the surface is now preserving buffers.
52     bool setPreserveBuffer(EGLSurface surface, bool preserve);
53 
54     void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
55 
56     void fence();
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 createPBufferSurface();
66     void loadConfig();
67     void createContext();
68     void initAtlas();
69 
70     RenderThread& mRenderThread;
71 
72     EGLDisplay mEglDisplay;
73     EGLConfig mEglConfig;
74     EGLContext mEglContext;
75     EGLSurface mPBufferSurface;
76 
77     const bool mAllowPreserveBuffer;
78     bool mCanSetPreserveBuffer;
79 
80     EGLSurface mCurrentSurface;
81 
82     sp<GraphicBuffer> mAtlasBuffer;
83     int64_t* mAtlasMap;
84     size_t mAtlasMapSize;
85 };
86 
87 } /* namespace renderthread */
88 } /* namespace uirenderer */
89 } /* namespace android */
90 
91 #endif /* EGLMANAGER_H */
92