EglManager.h revision 694d499662838123f474f41b31dea84ec5d563f0
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
25namespace android {
26namespace uirenderer {
27namespace renderthread {
28
29class RenderThread;
30class EglManager;
31
32class Frame {
33public:
34    EGLint width() const { return mWidth; }
35    EGLint height() const { return mHeight; }
36
37    // See: https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_buffer_age.txt
38    // for what this means
39    EGLint bufferAge() const { return mBufferAge; }
40
41private:
42    friend class EglManager;
43
44    EGLSurface mSurface;
45    EGLint mWidth;
46    EGLint mHeight;
47    EGLint mBufferAge;
48
49    // Maps from 0,0 in top-left to 0,0 in bottom-left
50    // If out is not an EGLint[4] you're going to have a bad time
51    void map(const SkRect& in, EGLint* out) const;
52};
53
54// This class contains the shared global EGL objects, such as EGLDisplay
55// and EGLConfig, which are re-used by CanvasContext
56class EglManager {
57public:
58    static const char* eglErrorString();
59    // Returns true on success, false on failure
60    void initialize();
61
62    bool hasEglContext();
63
64    EGLSurface createSurface(EGLNativeWindowType window);
65    void destroySurface(EGLSurface surface);
66
67    void destroy();
68
69    bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
70    // Returns true if the current surface changed, false if it was already current
71    bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
72    Frame beginFrame(EGLSurface surface);
73    void damageFrame(const Frame& frame, const SkRect& dirty);
74    // If this returns true it is mandatory that swapBuffers is called
75    // if damageFrame is called without subsequent calls to damageFrame().
76    // See EGL_KHR_partial_update for more information
77    bool damageRequiresSwap();
78    bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
79
80    // Returns true iff the surface is now preserving buffers.
81    bool setPreserveBuffer(EGLSurface surface, bool preserve);
82
83    void fence();
84
85private:
86    friend class RenderThread;
87    explicit EglManager(RenderThread& thread);
88    // EglContext is never destroyed, method is purposely not implemented
89    ~EglManager();
90
91    void initExtensions();
92    void createPBufferSurface();
93    void loadConfig();
94    void createContext();
95    EGLint queryBufferAge(EGLSurface surface);
96
97    RenderThread& mRenderThread;
98
99    EGLDisplay mEglDisplay;
100    EGLConfig mEglConfig;
101    EGLContext mEglContext;
102    EGLSurface mPBufferSurface;
103
104    EGLSurface mCurrentSurface;
105
106    enum class SwapBehavior {
107        Discard,
108        Preserved,
109        BufferAge,
110    };
111    SwapBehavior mSwapBehavior = SwapBehavior::Discard;
112};
113
114} /* namespace renderthread */
115} /* namespace uirenderer */
116} /* namespace android */
117
118#endif /* EGLMANAGER_H */
119