1 /* 2 * Copyright (C) 2011 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 #include "RenderContext.h" 17 #include "FBConfig.h" 18 #include "FrameBuffer.h" 19 #include "EGLDispatch.h" 20 #include "GLDispatch.h" 21 22 RenderContext *RenderContext::create(int p_config, 23 RenderContextPtr p_shareContext, 24 bool p_isGL2) 25 { 26 const FBConfig *fbconf = FBConfig::get(p_config); 27 if (!fbconf) { 28 return NULL; 29 } 30 31 RenderContext *c = new RenderContext(); 32 if (!c) { 33 return NULL; 34 } 35 36 EGLContext share = EGL_NO_CONTEXT; 37 if (p_shareContext.Ptr() != NULL) { 38 share = p_shareContext->getEGLContext(); 39 } 40 41 GLint glContextAttribs[] = { 42 EGL_CONTEXT_CLIENT_VERSION, 1, 43 EGL_NONE 44 }; 45 46 if (p_isGL2) { 47 glContextAttribs[1] = 2; 48 c->m_isGL2 = true; 49 } 50 51 c->m_ctx = s_egl.eglCreateContext(FrameBuffer::getFB()->getDisplay(), 52 fbconf->getEGLConfig(), share, 53 glContextAttribs); 54 55 if (c->m_ctx == EGL_NO_CONTEXT) { 56 delete c; 57 return NULL; 58 } 59 60 c->m_config = p_config; 61 return c; 62 } 63 64 RenderContext::RenderContext() : 65 m_ctx(EGL_NO_CONTEXT), 66 m_config(0), 67 m_isGL2(false) 68 { 69 } 70 71 RenderContext::~RenderContext() 72 { 73 if (m_ctx != EGL_NO_CONTEXT) { 74 s_egl.eglDestroyContext(FrameBuffer::getFB()->getDisplay(), m_ctx); 75 } 76 } 77