blob: 4dd90961b4f7eedf6572b810ed64386d9180ad71 [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 Reck8e539ca2018-11-15 15:21:29 -080028#include "utils/Result.h"
John Reck3b202512014-06-23 13:13:08 -070029
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
Greg Danielcd558522016-11-17 13:31:40 -050034class Frame;
John Reck3b202512014-06-23 13:13:08 -070035class RenderThread;
36
37// This class contains the shared global EGL objects, such as EGLDisplay
38// and EGLConfig, which are re-used by CanvasContext
39class EglManager {
40public:
John Reck1e510712018-04-23 08:15:03 -070041 explicit EglManager();
42
43 ~EglManager();
44
sergeyv694d4992016-10-27 10:23:13 -070045 static const char* eglErrorString();
John Reck1e510712018-04-23 08:15:03 -070046
John Reck3b202512014-06-23 13:13:08 -070047 void initialize();
48
49 bool hasEglContext();
John Reck3b202512014-06-23 13:13:08 -070050
Peiyong Lin3bff1352018-12-11 07:56:07 -080051 Result<EGLSurface, EGLint> createSurface(EGLNativeWindowType window, ColorMode colorMode,
52 SkColorSpace::Gamut colorGamut);
John Reck3b202512014-06-23 13:13:08 -070053 void destroySurface(EGLSurface surface);
54
55 void destroy();
56
57 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
58 // Returns true if the current surface changed, false if it was already current
John Reck1e510712018-04-23 08:15:03 -070059 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr, bool force = false);
John Reck149173d2015-08-10 09:52:29 -070060 Frame beginFrame(EGLSurface surface);
61 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080062 // 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();
John Reck149173d2015-08-10 09:52:29 -070066 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070067
John Reck1125d1f2014-10-23 11:02:19 -070068 // Returns true iff the surface is now preserving buffers.
69 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070070
John Reck55156372015-01-21 07:46:37 -080071 void fence();
72
John Recke170fb62018-05-07 08:12:07 -070073 EGLDisplay eglDisplay() const { return mEglDisplay; }
74
Stan Iliev564ca3e2018-09-04 22:00:00 +000075 // 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(sp<Fence>& 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, sp<Fence>& nativeFence);
82
John Reck3b202512014-06-23 13:13:08 -070083private:
Peiyong Lin3bff1352018-12-11 07:56:07 -080084 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);
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;
Romain Guy26a2b972017-04-17 09:39:51 -0700101 EGLConfig mEglConfigWideGamut;
John Reck3b202512014-06-23 13:13:08 -0700102 EGLContext mEglContext;
103 EGLSurface mPBufferSurface;
John Reck3b202512014-06-23 13:13:08 -0700104 EGLSurface mCurrentSurface;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800105 bool mHasWideColorGamutSupport;
John Reck149173d2015-08-10 09:52:29 -0700106 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700107};
108
109} /* namespace renderthread */
110} /* namespace uirenderer */
111} /* namespace android */
112
113#endif /* EGLMANAGER_H */