John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 21 | #include <SkRect.h> |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 22 | #include <ui/GraphicBuffer.h> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | namespace renderthread { |
| 28 | |
| 29 | class RenderThread; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 30 | class EglManager; |
| 31 | |
| 32 | class Frame { |
| 33 | public: |
| 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 | |
| 41 | private: |
| 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 Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 53 | |
| 54 | // This class contains the shared global EGL objects, such as EGLDisplay |
| 55 | // and EGLConfig, which are re-used by CanvasContext |
| 56 | class EglManager { |
| 57 | public: |
| 58 | // Returns true on success, false on failure |
| 59 | void initialize(); |
| 60 | |
| 61 | bool hasEglContext(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 62 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 63 | 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 Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 70 | bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 71 | Frame beginFrame(EGLSurface surface); |
| 72 | void damageFrame(const Frame& frame, const SkRect& dirty); |
| 73 | bool swapBuffers(const Frame& frame, const SkRect& screenDirty); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 74 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 75 | // Returns true iff the surface is now preserving buffers. |
| 76 | bool setPreserveBuffer(EGLSurface surface, bool preserve); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 77 | |
| 78 | void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); |
| 79 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 80 | void fence(); |
| 81 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 82 | private: |
| 83 | friend class RenderThread; |
| 84 | |
| 85 | EglManager(RenderThread& thread); |
| 86 | // EglContext is never destroyed, method is purposely not implemented |
| 87 | ~EglManager(); |
| 88 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 89 | void initExtensions(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 90 | void createPBufferSurface(); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 91 | void loadConfig(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 92 | void createContext(); |
| 93 | void initAtlas(); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 94 | EGLint queryBufferAge(EGLSurface surface); |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 95 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 96 | RenderThread& mRenderThread; |
| 97 | |
| 98 | EGLDisplay mEglDisplay; |
| 99 | EGLConfig mEglConfig; |
| 100 | EGLContext mEglContext; |
| 101 | EGLSurface mPBufferSurface; |
| 102 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 103 | EGLSurface mCurrentSurface; |
| 104 | |
| 105 | sp<GraphicBuffer> mAtlasBuffer; |
| 106 | int64_t* mAtlasMap; |
| 107 | size_t mAtlasMapSize; |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 108 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 109 | enum class SwapBehavior { |
| 110 | Discard, |
| 111 | Preserved, |
| 112 | BufferAge, |
| 113 | }; |
| 114 | SwapBehavior mSwapBehavior = SwapBehavior::Discard; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } /* namespace renderthread */ |
| 118 | } /* namespace uirenderer */ |
| 119 | } /* namespace android */ |
| 120 | |
| 121 | #endif /* EGLMANAGER_H */ |