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> 21 #include <SkRect.h> 22 #include <ui/GraphicBuffer.h> 23 #include <utils/StrongPointer.h> 24 25 namespace android { 26 namespace uirenderer { 27 namespace renderthread { 28 29 class Frame; 30 class RenderThread; 31 32 // This class contains the shared global EGL objects, such as EGLDisplay 33 // and EGLConfig, which are re-used by CanvasContext 34 class EglManager { 35 public: 36 static const char* eglErrorString(); 37 // Returns true on success, false on failure 38 void initialize(); 39 40 bool hasEglContext(); 41 42 EGLSurface createSurface(EGLNativeWindowType window, bool wideColorGamut); 43 void destroySurface(EGLSurface surface); 44 45 void destroy(); 46 isCurrent(EGLSurface surface)47 bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; } 48 // Returns true if the current surface changed, false if it was already current 49 bool makeCurrent(EGLSurface surface, EGLint* errOut = nullptr); 50 Frame beginFrame(EGLSurface surface); 51 void damageFrame(const Frame& frame, const SkRect& dirty); 52 // If this returns true it is mandatory that swapBuffers is called 53 // if damageFrame is called without subsequent calls to damageFrame(). 54 // See EGL_KHR_partial_update for more information 55 bool damageRequiresSwap(); 56 bool swapBuffers(const Frame& frame, const SkRect& screenDirty); 57 58 // Returns true iff the surface is now preserving buffers. 59 bool setPreserveBuffer(EGLSurface surface, bool preserve); 60 61 void fence(); 62 63 private: 64 friend class RenderThread; 65 explicit EglManager(RenderThread& thread); 66 // EglContext is never destroyed, method is purposely not implemented 67 ~EglManager(); 68 69 void initExtensions(); 70 void createPBufferSurface(); 71 void loadConfigs(); 72 void createContext(); 73 EGLint queryBufferAge(EGLSurface surface); 74 75 RenderThread& mRenderThread; 76 77 EGLDisplay mEglDisplay; 78 EGLConfig mEglConfig; 79 EGLConfig mEglConfigWideGamut; 80 EGLContext mEglContext; 81 EGLSurface mPBufferSurface; 82 83 EGLSurface mCurrentSurface; 84 85 enum class SwapBehavior { 86 Discard, 87 Preserved, 88 BufferAge, 89 }; 90 SwapBehavior mSwapBehavior = SwapBehavior::Discard; 91 }; 92 93 } /* namespace renderthread */ 94 } /* namespace uirenderer */ 95 } /* namespace android */ 96 97 #endif /* EGLMANAGER_H */ 98