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 <EGL/egl.h>
20 #include <EGL/eglext.h>
21 #include <SkImageInfo.h>
22 #include <SkRect.h>
23 #include <cutils/compiler.h>
24 #include <ui/GraphicBuffer.h>
25 #include <utils/StrongPointer.h>
26 
27 #include "IRenderPipeline.h"
28 #include "utils/Result.h"
29 
30 namespace android {
31 namespace uirenderer {
32 namespace renderthread {
33 
34 class Frame;
35 class RenderThread;
36 
37 // This class contains the shared global EGL objects, such as EGLDisplay
38 // and EGLConfig, which are re-used by CanvasContext
39 class EglManager {
40 public:
41     explicit EglManager();
42 
43     ~EglManager();
44 
45     static const char* eglErrorString();
46 
47     void initialize();
48 
49     bool hasEglContext();
50 
51     Result<EGLSurface, EGLint> createSurface(EGLNativeWindowType window, ColorMode colorMode,
52                                              sk_sp<SkColorSpace> colorSpace);
53     void destroySurface(EGLSurface surface);
54 
55     void destroy();
56 
isCurrent(EGLSurface surface)57     bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
58     // Returns true if the current surface changed, false if it was already current
59     bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr, bool force = false);
60     Frame beginFrame(EGLSurface surface);
61     void damageFrame(const Frame& frame, const SkRect& dirty);
62     // If this returns true it is mandatory that swapBuffers is called
63     // if damageFrame is called without subsequent calls to damageFrame().
64     // See EGL_KHR_partial_update for more information
65     bool damageRequiresSwap();
66     bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
67 
68     // Returns true iff the surface is now preserving buffers.
69     bool setPreserveBuffer(EGLSurface surface, bool preserve);
70 
71     void fence();
72 
eglDisplay()73     EGLDisplay eglDisplay() const { return mEglDisplay; }
74 
75     // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
76     // support is missing, block the CPU on the fence.
77     status_t fenceWait(int fence);
78 
79     // Creates a fence that is signaled, when all the pending GL commands are flushed.
80     // Depending on installed extensions, the result is either Android native fence or EGL fence.
81     status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence);
82 
83 private:
84     enum class SwapBehavior {
85         Discard,
86         Preserved,
87         BufferAge,
88     };
89 
90     static EGLConfig load8BitsConfig(EGLDisplay display, SwapBehavior swapBehavior);
91     static EGLConfig loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior);
92 
93     void initExtensions();
94     void createPBufferSurface();
95     void loadConfigs();
96     void createContext();
97     EGLint queryBufferAge(EGLSurface surface);
98 
99     EGLDisplay mEglDisplay;
100     EGLConfig mEglConfig;
101     EGLConfig mEglConfigWideGamut;
102     EGLContext mEglContext;
103     EGLSurface mPBufferSurface;
104     EGLSurface mCurrentSurface;
105     bool mHasWideColorGamutSupport;
106     SwapBehavior mSwapBehavior = SwapBehavior::Discard;
107 };
108 
109 } /* namespace renderthread */
110 } /* namespace uirenderer */
111 } /* namespace android */
112 
113 #endif /* EGLMANAGER_H */
114