blob: 507673adf26e69a03e6dc673150137fd2f8d9212 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
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
John Reck3b202512014-06-23 13:13:08 -070019#include <EGL/egl.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000020#include <EGL/eglext.h>
John Reckd04794a2015-05-08 10:04:36 -070021#include <SkRect.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <cutils/compiler.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000023#include <ui/Fence.h>
John Reck3b202512014-06-23 13:13:08 -070024#include <ui/GraphicBuffer.h>
25#include <utils/StrongPointer.h>
26
27namespace android {
28namespace uirenderer {
29namespace renderthread {
30
Greg Danielcd558522016-11-17 13:31:40 -050031class Frame;
John Reck3b202512014-06-23 13:13:08 -070032class RenderThread;
33
34// This class contains the shared global EGL objects, such as EGLDisplay
35// and EGLConfig, which are re-used by CanvasContext
36class EglManager {
37public:
John Reck1e510712018-04-23 08:15:03 -070038 explicit EglManager();
39
40 ~EglManager();
41
sergeyv694d4992016-10-27 10:23:13 -070042 static const char* eglErrorString();
John Reck1e510712018-04-23 08:15:03 -070043
John Reck3b202512014-06-23 13:13:08 -070044 void initialize();
45
46 bool hasEglContext();
John Reck3b202512014-06-23 13:13:08 -070047
Romain Guy26a2b972017-04-17 09:39:51 -070048 EGLSurface createSurface(EGLNativeWindowType window, bool wideColorGamut);
John Reck3b202512014-06-23 13:13:08 -070049 void destroySurface(EGLSurface surface);
50
51 void destroy();
52
53 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
54 // Returns true if the current surface changed, false if it was already current
John Reck1e510712018-04-23 08:15:03 -070055 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr, bool force = false);
John Reck149173d2015-08-10 09:52:29 -070056 Frame beginFrame(EGLSurface surface);
57 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080058 // If this returns true it is mandatory that swapBuffers is called
59 // if damageFrame is called without subsequent calls to damageFrame().
60 // See EGL_KHR_partial_update for more information
61 bool damageRequiresSwap();
John Reck149173d2015-08-10 09:52:29 -070062 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070063
John Reck1125d1f2014-10-23 11:02:19 -070064 // Returns true iff the surface is now preserving buffers.
65 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070066
John Reck55156372015-01-21 07:46:37 -080067 void fence();
68
John Recke170fb62018-05-07 08:12:07 -070069 EGLDisplay eglDisplay() const { return mEglDisplay; }
70
Stan Iliev564ca3e2018-09-04 22:00:00 +000071 // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
72 // support is missing, block the CPU on the fence.
73 status_t fenceWait(sp<Fence>& fence);
74
75 // Creates a fence that is signaled, when all the pending GL commands are flushed.
76 // Depending on installed extensions, the result is either Android native fence or EGL fence.
77 status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, sp<Fence>& nativeFence);
78
John Reck3b202512014-06-23 13:13:08 -070079private:
John Reck3b202512014-06-23 13:13:08 -070080
John Reck149173d2015-08-10 09:52:29 -070081 void initExtensions();
John Reckd7db4d72015-05-20 07:18:55 -070082 void createPBufferSurface();
Romain Guy26a2b972017-04-17 09:39:51 -070083 void loadConfigs();
John Reck3b202512014-06-23 13:13:08 -070084 void createContext();
John Reck149173d2015-08-10 09:52:29 -070085 EGLint queryBufferAge(EGLSurface surface);
Season Li13d1b4a2015-07-29 17:16:19 -070086
John Reck3b202512014-06-23 13:13:08 -070087 EGLDisplay mEglDisplay;
88 EGLConfig mEglConfig;
Romain Guy26a2b972017-04-17 09:39:51 -070089 EGLConfig mEglConfigWideGamut;
John Reck3b202512014-06-23 13:13:08 -070090 EGLContext mEglContext;
91 EGLSurface mPBufferSurface;
92
John Reck3b202512014-06-23 13:13:08 -070093 EGLSurface mCurrentSurface;
94
John Reck149173d2015-08-10 09:52:29 -070095 enum class SwapBehavior {
96 Discard,
97 Preserved,
98 BufferAge,
99 };
100 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700101};
102
103} /* namespace renderthread */
104} /* namespace uirenderer */
105} /* namespace android */
106
107#endif /* EGLMANAGER_H */