blob: 55c81d42d8a0d557a4b75244ca7da200e03960c6 [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>
Peiyong Lin1f6aa122018-09-10 16:28:08 -070021#include <SkImageInfo.h>
John Reckd04794a2015-05-08 10:04:36 -070022#include <SkRect.h>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <cutils/compiler.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000024#include <ui/Fence.h>
John Reck3b202512014-06-23 13:13:08 -070025#include <ui/GraphicBuffer.h>
26#include <utils/StrongPointer.h>
Peiyong Lin1f6aa122018-09-10 16:28:08 -070027#include "IRenderPipeline.h"
John Reck3b202512014-06-23 13:13:08 -070028
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
Greg Danielcd558522016-11-17 13:31:40 -050033class Frame;
John Reck3b202512014-06-23 13:13:08 -070034class RenderThread;
35
36// This class contains the shared global EGL objects, such as EGLDisplay
37// and EGLConfig, which are re-used by CanvasContext
38class EglManager {
39public:
John Reck1e510712018-04-23 08:15:03 -070040 explicit EglManager();
41
42 ~EglManager();
43
sergeyv694d4992016-10-27 10:23:13 -070044 static const char* eglErrorString();
John Reck1e510712018-04-23 08:15:03 -070045
John Reck3b202512014-06-23 13:13:08 -070046 void initialize();
47
48 bool hasEglContext();
John Reck3b202512014-06-23 13:13:08 -070049
Peiyong Lin1f6aa122018-09-10 16:28:08 -070050 EGLSurface createSurface(EGLNativeWindowType window, ColorMode colorMode);
John Reck3b202512014-06-23 13:13:08 -070051 void destroySurface(EGLSurface surface);
52
53 void destroy();
54
55 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
56 // Returns true if the current surface changed, false if it was already current
John Reck1e510712018-04-23 08:15:03 -070057 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr, bool force = false);
John Reck149173d2015-08-10 09:52:29 -070058 Frame beginFrame(EGLSurface surface);
59 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080060 // If this returns true it is mandatory that swapBuffers is called
61 // if damageFrame is called without subsequent calls to damageFrame().
62 // See EGL_KHR_partial_update for more information
63 bool damageRequiresSwap();
John Reck149173d2015-08-10 09:52:29 -070064 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070065
John Reck1125d1f2014-10-23 11:02:19 -070066 // Returns true iff the surface is now preserving buffers.
67 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070068
John Reck55156372015-01-21 07:46:37 -080069 void fence();
70
John Recke170fb62018-05-07 08:12:07 -070071 EGLDisplay eglDisplay() const { return mEglDisplay; }
72
Stan Iliev564ca3e2018-09-04 22:00:00 +000073 // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
74 // support is missing, block the CPU on the fence.
75 status_t fenceWait(sp<Fence>& fence);
76
77 // Creates a fence that is signaled, when all the pending GL commands are flushed.
78 // Depending on installed extensions, the result is either Android native fence or EGL fence.
79 status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, sp<Fence>& nativeFence);
80
John Reck3b202512014-06-23 13:13:08 -070081private:
John Reck3b202512014-06-23 13:13:08 -070082
John Reck149173d2015-08-10 09:52:29 -070083 void initExtensions();
John Reckd7db4d72015-05-20 07:18:55 -070084 void createPBufferSurface();
Romain Guy26a2b972017-04-17 09:39:51 -070085 void loadConfigs();
John Reck3b202512014-06-23 13:13:08 -070086 void createContext();
John Reck149173d2015-08-10 09:52:29 -070087 EGLint queryBufferAge(EGLSurface surface);
Season Li13d1b4a2015-07-29 17:16:19 -070088
John Reck3b202512014-06-23 13:13:08 -070089 EGLDisplay mEglDisplay;
90 EGLConfig mEglConfig;
Romain Guy26a2b972017-04-17 09:39:51 -070091 EGLConfig mEglConfigWideGamut;
John Reck3b202512014-06-23 13:13:08 -070092 EGLContext mEglContext;
93 EGLSurface mPBufferSurface;
John Reck3b202512014-06-23 13:13:08 -070094 EGLSurface mCurrentSurface;
95
John Reck149173d2015-08-10 09:52:29 -070096 enum class SwapBehavior {
97 Discard,
98 Preserved,
99 BufferAge,
100 };
101 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700102};
103
104} /* namespace renderthread */
105} /* namespace uirenderer */
106} /* namespace android */
107
108#endif /* EGLMANAGER_H */