blob: 69f3ed014c5304970e03c094dd1bbbfc264e3277 [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>
John Reck3b202512014-06-23 13:13:08 -070024#include <utils/StrongPointer.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040025
Peiyong Lin1f6aa122018-09-10 16:28:08 -070026#include "IRenderPipeline.h"
John Reck8e539ca2018-11-15 15:21:29 -080027#include "utils/Result.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 Lin3bff1352018-12-11 07:56:07 -080050 Result<EGLSurface, EGLint> createSurface(EGLNativeWindowType window, ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -050051 sk_sp<SkColorSpace> colorSpace);
John Reck3b202512014-06-23 13:13:08 -070052 void destroySurface(EGLSurface surface);
53
54 void destroy();
55
56 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
57 // Returns true if the current surface changed, false if it was already current
John Reck1e510712018-04-23 08:15:03 -070058 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr, bool force = false);
John Reck149173d2015-08-10 09:52:29 -070059 Frame beginFrame(EGLSurface surface);
60 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080061 // If this returns true it is mandatory that swapBuffers is called
62 // if damageFrame is called without subsequent calls to damageFrame().
63 // See EGL_KHR_partial_update for more information
64 bool damageRequiresSwap();
John Reck149173d2015-08-10 09:52:29 -070065 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070066
John Reck1125d1f2014-10-23 11:02:19 -070067 // Returns true iff the surface is now preserving buffers.
68 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070069
John Reck55156372015-01-21 07:46:37 -080070 void fence();
71
John Recke170fb62018-05-07 08:12:07 -070072 EGLDisplay eglDisplay() const { return mEglDisplay; }
73
Stan Iliev564ca3e2018-09-04 22:00:00 +000074 // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
75 // support is missing, block the CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -040076 status_t fenceWait(int fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +000077
78 // Creates a fence that is signaled, when all the pending GL commands are flushed.
79 // Depending on installed extensions, the result is either Android native fence or EGL fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -040080 status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence);
Stan Iliev564ca3e2018-09-04 22:00:00 +000081
John Reck3b202512014-06-23 13:13:08 -070082private:
Peiyong Lin3bff1352018-12-11 07:56:07 -080083 enum class SwapBehavior {
84 Discard,
85 Preserved,
86 BufferAge,
87 };
88
89 static EGLConfig load8BitsConfig(EGLDisplay display, SwapBehavior swapBehavior);
90 static EGLConfig loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior);
John Reckb36bfdd2020-07-23 13:47:49 -070091 static EGLConfig load1010102Config(EGLDisplay display, SwapBehavior swapBehavior);
John Reck3b202512014-06-23 13:13:08 -070092
John Reck149173d2015-08-10 09:52:29 -070093 void initExtensions();
John Reckd7db4d72015-05-20 07:18:55 -070094 void createPBufferSurface();
Romain Guy26a2b972017-04-17 09:39:51 -070095 void loadConfigs();
John Reck3b202512014-06-23 13:13:08 -070096 void createContext();
John Reck149173d2015-08-10 09:52:29 -070097 EGLint queryBufferAge(EGLSurface surface);
Season Li13d1b4a2015-07-29 17:16:19 -070098
John Reck3b202512014-06-23 13:13:08 -070099 EGLDisplay mEglDisplay;
100 EGLConfig mEglConfig;
John Reckb36bfdd2020-07-23 13:47:49 -0700101 EGLConfig mEglConfigF16;
102 EGLConfig mEglConfig1010102;
John Reck3b202512014-06-23 13:13:08 -0700103 EGLContext mEglContext;
104 EGLSurface mPBufferSurface;
John Reck3b202512014-06-23 13:13:08 -0700105 EGLSurface mCurrentSurface;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800106 bool mHasWideColorGamutSupport;
John Reck149173d2015-08-10 09:52:29 -0700107 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700108};
109
110} /* namespace renderthread */
111} /* namespace uirenderer */
112} /* namespace android */
113
114#endif /* EGLMANAGER_H */