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