• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <SkColorSpace.h>
22 #include <SkImageInfo.h>
23 #include <SkRect.h>
24 #include <cutils/compiler.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     static EGLConfig load1010102Config(EGLDisplay display, SwapBehavior swapBehavior);
93     static EGLConfig loadA8Config(EGLDisplay display, SwapBehavior swapBehavior);
94 
95     void initExtensions();
96     void createPBufferSurface();
97     void loadConfigs();
98     void createContext();
99     EGLint queryBufferAge(EGLSurface surface);
100 
101     EGLDisplay mEglDisplay;
102     EGLConfig mEglConfig;
103     EGLConfig mEglConfigF16;
104     EGLConfig mEglConfig1010102;
105     EGLConfig mEglConfigA8;
106     EGLContext mEglContext;
107     EGLSurface mPBufferSurface;
108     EGLSurface mCurrentSurface;
109     bool mHasWideColorGamutSupport;
110     SwapBehavior mSwapBehavior = SwapBehavior::Discard;
111 };
112 
113 } /* namespace renderthread */
114 } /* namespace uirenderer */
115 } /* namespace android */
116 
117 #endif /* EGLMANAGER_H */
118