EglManager.h revision d04794a9a3f9edc8b7ca336175d66eb81a8f55fa
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 25namespace android { 26namespace uirenderer { 27namespace renderthread { 28 29class RenderThread; 30 31// This class contains the shared global EGL objects, such as EGLDisplay 32// and EGLConfig, which are re-used by CanvasContext 33class EglManager { 34public: 35 // Returns true on success, false on failure 36 void initialize(); 37 38 bool hasEglContext(); 39 void requireGlContext(); 40 41 void usePBufferSurface(); 42 EGLSurface createSurface(EGLNativeWindowType window); 43 void destroySurface(EGLSurface surface); 44 45 void destroy(); 46 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); 50 void beginFrame(EGLSurface surface, EGLint* width, EGLint* height); 51 bool swapBuffers(EGLSurface surface, const SkRect& dirty, EGLint width, EGLint height); 52 void cancelFrame(); 53 54 // Returns true iff the surface is now preserving buffers. 55 bool setPreserveBuffer(EGLSurface surface, bool preserve); 56 57 void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); 58 59 void fence(); 60 61private: 62 friend class RenderThread; 63 64 EglManager(RenderThread& thread); 65 // EglContext is never destroyed, method is purposely not implemented 66 ~EglManager(); 67 68 void loadConfig(); 69 void createContext(); 70 void initAtlas(); 71 72 RenderThread& mRenderThread; 73 74 EGLDisplay mEglDisplay; 75 EGLConfig mEglConfig; 76 EGLContext mEglContext; 77 EGLSurface mPBufferSurface; 78 79 const bool mAllowPreserveBuffer; 80 bool mCanSetPreserveBuffer; 81 82 EGLSurface mCurrentSurface; 83 84 sp<GraphicBuffer> mAtlasBuffer; 85 int64_t* mAtlasMap; 86 size_t mAtlasMapSize; 87 88 // Whether or not we are in the middle of drawing a frame. This is used 89 // to avoid switching surfaces mid-frame if requireGlContext() is called 90 // TODO: Need to be better about surface/context management so that this isn't 91 // necessary 92 bool mInFrame; 93}; 94 95} /* namespace renderthread */ 96} /* namespace uirenderer */ 97} /* namespace android */ 98 99#endif /* EGLMANAGER_H */ 100