EglManager.h revision 13d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0
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#include <set>
25
26namespace android {
27namespace uirenderer {
28namespace renderthread {
29
30class RenderThread;
31
32// This class contains the shared global EGL objects, such as EGLDisplay
33// and EGLConfig, which are re-used by CanvasContext
34class EglManager {
35public:
36    // Returns true on success, false on failure
37    void initialize();
38
39    bool hasEglContext();
40
41    bool hasEglExtension(const char* extension) const;
42
43    EGLSurface createSurface(EGLNativeWindowType window);
44    void destroySurface(EGLSurface surface);
45
46    void destroy();
47
48    bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
49    // Returns true if the current surface changed, false if it was already current
50    bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
51    void beginFrame(EGLSurface surface, EGLint* width, EGLint* height, EGLint* framebufferAge);
52    bool swapBuffers(EGLSurface surface, const SkRect& dirty, EGLint width, EGLint height);
53
54    // Returns true iff the surface is now preserving buffers.
55    bool setPreserveBuffer(EGLSurface surface, bool preserve);
56
57    bool useBufferAgeExt();
58
59    void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
60
61    void fence();
62
63private:
64    friend class RenderThread;
65
66    EglManager(RenderThread& thread);
67    // EglContext is never destroyed, method is purposely not implemented
68    ~EglManager();
69
70    void createPBufferSurface();
71    void loadConfig(bool useBufferAgeExt);
72    void createContext();
73    void initAtlas();
74
75    void findExtensions(const char* extensions, std::set<std::string>& list) const;
76
77    RenderThread& mRenderThread;
78
79    EGLDisplay mEglDisplay;
80    EGLConfig mEglConfig;
81    EGLContext mEglContext;
82    EGLSurface mPBufferSurface;
83
84    const bool mAllowPreserveBuffer;
85    bool mCanSetPreserveBuffer;
86
87    bool mHasBufferAgeExt;
88
89    EGLSurface mCurrentSurface;
90
91    sp<GraphicBuffer> mAtlasBuffer;
92    int64_t* mAtlasMap;
93    size_t mAtlasMapSize;
94
95    std::set<std::string> mEglExtensionList;
96};
97
98} /* namespace renderthread */
99} /* namespace uirenderer */
100} /* namespace android */
101
102#endif /* EGLMANAGER_H */
103