EglManager.h revision cd55852fcd840f7f4c4d7a0a7253a2995c77afa2
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 Frame;
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    static const char* eglErrorString();
37    // Returns true on success, false on failure
38    void initialize();
39
40    bool hasEglContext();
41
42    EGLSurface createSurface(EGLNativeWindowType window);
43    void destroySurface(EGLSurface surface);
44
45    void destroy();
46
47    bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
48    // Returns true if the current surface changed, false if it was already current
49    bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
50    Frame beginFrame(EGLSurface surface);
51    void damageFrame(const Frame& frame, const SkRect& dirty);
52    // If this returns true it is mandatory that swapBuffers is called
53    // if damageFrame is called without subsequent calls to damageFrame().
54    // See EGL_KHR_partial_update for more information
55    bool damageRequiresSwap();
56    bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
57
58    // Returns true iff the surface is now preserving buffers.
59    bool setPreserveBuffer(EGLSurface surface, bool preserve);
60
61    void fence();
62
63private:
64    friend class RenderThread;
65    explicit EglManager(RenderThread& thread);
66    // EglContext is never destroyed, method is purposely not implemented
67    ~EglManager();
68
69    void initExtensions();
70    void createPBufferSurface();
71    void loadConfig();
72    void createContext();
73    EGLint queryBufferAge(EGLSurface surface);
74
75    RenderThread& mRenderThread;
76
77    EGLDisplay mEglDisplay;
78    EGLConfig mEglConfig;
79    EGLContext mEglContext;
80    EGLSurface mPBufferSurface;
81
82    EGLSurface mCurrentSurface;
83
84    enum class SwapBehavior {
85        Discard,
86        Preserved,
87        BufferAge,
88    };
89    SwapBehavior mSwapBehavior = SwapBehavior::Discard;
90};
91
92} /* namespace renderthread */
93} /* namespace uirenderer */
94} /* namespace android */
95
96#endif /* EGLMANAGER_H */
97