blob: 459baed70e40a13f792166597f763e116ccae449 [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:
34 EGLint width() const { return mWidth; }
35 EGLint height() const { return mHeight; }
36
37 // See: https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_buffer_age.txt
38 // for what this means
39 EGLint bufferAge() const { return mBufferAge; }
40
41private:
42 friend class EglManager;
43
44 EGLSurface mSurface;
45 EGLint mWidth;
46 EGLint mHeight;
47 EGLint mBufferAge;
48
49 // Maps from 0,0 in top-left to 0,0 in bottom-left
50 // If out is not an EGLint[4] you're going to have a bad time
51 void map(const SkRect& in, EGLint* out) const;
52};
John Reck3b202512014-06-23 13:13:08 -070053
54// This class contains the shared global EGL objects, such as EGLDisplay
55// and EGLConfig, which are re-used by CanvasContext
56class EglManager {
57public:
58 // Returns true on success, false on failure
59 void initialize();
60
61 bool hasEglContext();
John Reck3b202512014-06-23 13:13:08 -070062
John Reck3b202512014-06-23 13:13:08 -070063 EGLSurface createSurface(EGLNativeWindowType window);
64 void destroySurface(EGLSurface surface);
65
66 void destroy();
67
68 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; }
69 // Returns true if the current surface changed, false if it was already current
John Reckf2dcc2a2015-07-16 09:17:59 -070070 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr);
John Reck149173d2015-08-10 09:52:29 -070071 Frame beginFrame(EGLSurface surface);
72 void damageFrame(const Frame& frame, const SkRect& dirty);
John Reckc96955d2016-02-26 14:56:44 -080073 // If this returns true it is mandatory that swapBuffers is called
74 // if damageFrame is called without subsequent calls to damageFrame().
75 // See EGL_KHR_partial_update for more information
76 bool damageRequiresSwap();
John Reck149173d2015-08-10 09:52:29 -070077 bool swapBuffers(const Frame& frame, const SkRect& screenDirty);
John Reck3b202512014-06-23 13:13:08 -070078
John Reck1125d1f2014-10-23 11:02:19 -070079 // Returns true iff the surface is now preserving buffers.
80 bool setPreserveBuffer(EGLSurface surface, bool preserve);
John Reck3b202512014-06-23 13:13:08 -070081
82 void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
83
John Reck55156372015-01-21 07:46:37 -080084 void fence();
85
John Reck3b202512014-06-23 13:13:08 -070086private:
87 friend class RenderThread;
88
89 EglManager(RenderThread& thread);
90 // EglContext is never destroyed, method is purposely not implemented
91 ~EglManager();
92
John Reck149173d2015-08-10 09:52:29 -070093 void initExtensions();
John Reckd7db4d72015-05-20 07:18:55 -070094 void createPBufferSurface();
John Reck149173d2015-08-10 09:52:29 -070095 void loadConfig();
John Reck3b202512014-06-23 13:13:08 -070096 void createContext();
97 void initAtlas();
John Reck149173d2015-08-10 09:52:29 -070098 EGLint queryBufferAge(EGLSurface surface);
Season Li13d1b4a2015-07-29 17:16:19 -070099
John Reck3b202512014-06-23 13:13:08 -0700100 RenderThread& mRenderThread;
101
102 EGLDisplay mEglDisplay;
103 EGLConfig mEglConfig;
104 EGLContext mEglContext;
105 EGLSurface mPBufferSurface;
106
John Reck3b202512014-06-23 13:13:08 -0700107 EGLSurface mCurrentSurface;
108
109 sp<GraphicBuffer> mAtlasBuffer;
110 int64_t* mAtlasMap;
111 size_t mAtlasMapSize;
Season Li13d1b4a2015-07-29 17:16:19 -0700112
John Reck149173d2015-08-10 09:52:29 -0700113 enum class SwapBehavior {
114 Discard,
115 Preserved,
116 BufferAge,
117 };
118 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
John Reck3b202512014-06-23 13:13:08 -0700119};
120
121} /* namespace renderthread */
122} /* namespace uirenderer */
123} /* namespace android */
124
125#endif /* EGLMANAGER_H */