blob: b12522e6bd41e366936b8b46ebe9c91ec3e533de [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
19#include <cutils/compiler.h>
20#include <EGL/egl.h>
John Reckd04794a2015-05-08 10:04:36 -070021#include <SkRect.h>
John Reck3b202512014-06-23 13:13:08 -070022#include <ui/GraphicBuffer.h>
23#include <utils/StrongPointer.h>
24
25namespace android {
26namespace uirenderer {
27namespace renderthread {
28
29class RenderThread;
John Reck149173d2015-08-10 09:52:29 -070030class EglManager;
31
32class Frame {
33public:
Stan Iliev500a0c32016-10-26 10:30:09 -040034 Frame(EGLint width, EGLint height, EGLint bufferAge)
35 : mWidth(width)
36 , mHeight(height)
37 , mBufferAge(bufferAge) { }
38
John Reck149173d2015-08-10 09:52:29 -070039 EGLint width() const { return mWidth; }
40 EGLint height() const { return mHeight; }
41
42 // See: https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_buffer_age.txt
43 // for what this means
44 EGLint bufferAge() const { return mBufferAge; }
45
46private:
Stan Iliev500a0c32016-10-26 10:30:09 -040047 Frame() {}
John Reck149173d2015-08-10 09:52:29 -070048 friend class EglManager;
49
50 EGLSurface mSurface;
51 EGLint mWidth;
52 EGLint mHeight;
53 EGLint mBufferAge;
54
55 // Maps from 0,0 in top-left to 0,0 in bottom-left
56 // If out is not an EGLint[4] you're going to have a bad time
57 void map(const SkRect& in, EGLint* out) const;
58};
John Reck3b202512014-06-23 13:13:08 -070059
60// This class contains the shared global EGL objects, such as EGLDisplay
61// and EGLConfig, which are re-used by CanvasContext
62class EglManager {
63public:
sergeyv694d4992016-10-27 10:23:13 -070064 static const char* eglErrorString();
John Reck3b202512014-06-23 13:13:08 -070065 // Returns true on success, false on failure
66 void initialize();
67
68 bool hasEglContext();
John Reck3b202512014-06-23 13:13:08 -070069
John Reck3b202512014-06-23 13:13:08 -070070 EGLSurface createSurface(EGLNativeWindowType window);
71 void destroySurface(EGLSurface surface);
72
73 void destroy();
74
75 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
76 // Returns true if the current surface changed, false if it was already current
John Reckf2dcc2a2015-07-16 09:17:59 -070077 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
John Reck149173d2015-08-10 09:52:29 -070078 Frame beginFrame(EGLSurface surface);
79 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080080 // If this returns true it is mandatory that swapBuffers is called
81 // if damageFrame is called without subsequent calls to damageFrame().
82 // See EGL_KHR_partial_update for more information
83 bool damageRequiresSwap();
John Reck149173d2015-08-10 09:52:29 -070084 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070085
John Reck1125d1f2014-10-23 11:02:19 -070086 // Returns true iff the surface is now preserving buffers.
87 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070088
John Reck55156372015-01-21 07:46:37 -080089 void fence();
90
John Reck3b202512014-06-23 13:13:08 -070091private:
92 friend class RenderThread;
Chih-Hung Hsieh49796452016-08-10 14:08:35 -070093 explicit EglManager(RenderThread& thread);
John Reck3b202512014-06-23 13:13:08 -070094 // EglContext is never destroyed, method is purposely not implemented
95 ~EglManager();
96
John Reck149173d2015-08-10 09:52:29 -070097 void initExtensions();
John Reckd7db4d72015-05-20 07:18:55 -070098 void createPBufferSurface();
John Reck149173d2015-08-10 09:52:29 -070099 void loadConfig();
John Reck3b202512014-06-23 13:13:08 -0700100 void createContext();
John Reck149173d2015-08-10 09:52:29 -0700101 EGLint queryBufferAge(EGLSurface surface);
Season Li13d1b4a2015-07-29 17:16:19 -0700102
John Reck3b202512014-06-23 13:13:08 -0700103 RenderThread& mRenderThread;
104
105 EGLDisplay mEglDisplay;
106 EGLConfig mEglConfig;
107 EGLContext mEglContext;
108 EGLSurface mPBufferSurface;
109
John Reck3b202512014-06-23 13:13:08 -0700110 EGLSurface mCurrentSurface;
111
John Reck149173d2015-08-10 09:52:29 -0700112 enum class SwapBehavior {
113 Discard,
114 Preserved,
115 BufferAge,
116 };
117 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700118};
119
120} /* namespace renderthread */
121} /* namespace uirenderer */
122} /* namespace android */
123
124#endif /* EGLMANAGER_H */