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
18 #include "GLESVersionDetector.h"
19
20 #include "OpenGLESDispatch/EGLDispatch.h"
21 #include "OpenGLESDispatch/GLESv1Dispatch.h"
22
23 #include "base/SmallVector.h"
24
25 #include "host-common/feature_control.h"
26 #include "host-common/misc.h"
27
28 #include <assert.h>
29 #include "ErrorLog.h"
30
create(EGLDisplay display,EGLConfig config,EGLContext sharedContext,HandleType hndl,GLESApi version)31 RenderContext* RenderContext::create(EGLDisplay display,
32 EGLConfig config,
33 EGLContext sharedContext,
34 HandleType hndl,
35 GLESApi version) {
36 return createImpl(display, config, sharedContext, hndl, version, nullptr);
37 }
38
createImpl(EGLDisplay display,EGLConfig config,EGLContext sharedContext,HandleType hndl,GLESApi version,android::base::Stream * stream)39 RenderContext* RenderContext::createImpl(EGLDisplay display,
40 EGLConfig config,
41 EGLContext sharedContext,
42 HandleType hndl,
43 GLESApi version,
44 android::base::Stream *stream) {
45 GLESApi clientVersion = version;
46 int majorVersion = clientVersion;
47 int minorVersion = 0;
48
49 if (version == GLESApi_3_0) {
50 majorVersion = 3;
51 minorVersion = 0;
52 } else if (version == GLESApi_3_1) {
53 majorVersion = 3;
54 minorVersion = 1;
55 }
56
57 android::base::SmallFixedVector<EGLint, 7> contextAttribs = {
58 EGL_CONTEXT_CLIENT_VERSION, majorVersion,
59 EGL_CONTEXT_MINOR_VERSION_KHR, minorVersion,
60 };
61
62 if (shouldEnableCoreProfile()) {
63 contextAttribs.push_back(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR);
64 contextAttribs.push_back(EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR);
65 }
66
67 contextAttribs.push_back(EGL_NONE);
68
69 EGLContext context;
70 if (stream && s_egl.eglLoadContext) {
71 context = s_egl.eglLoadContext(display, &contextAttribs[0], stream);
72 } else {
73 context = s_egl.eglCreateContext(
74 display, config, sharedContext, &contextAttribs[0]);
75 }
76 if (context == EGL_NO_CONTEXT) {
77 fprintf(stderr, "%s: failed to create context (EGL_NO_CONTEXT result)\n", __func__);
78 return NULL;
79 }
80
81 return new RenderContext(display, context, hndl, clientVersion, NULL);
82 }
83
RenderContext(EGLDisplay display,EGLContext context,HandleType hndl,GLESApi version,void * emulatedGLES1Context)84 RenderContext::RenderContext(EGLDisplay display,
85 EGLContext context,
86 HandleType hndl,
87 GLESApi version,
88 void* emulatedGLES1Context) :
89 mDisplay(display),
90 mContext(context),
91 mHndl(hndl),
92 mVersion(version),
93 mContextData() { }
94
~RenderContext()95 RenderContext::~RenderContext() {
96 if (mContext != EGL_NO_CONTEXT) {
97 s_egl.eglDestroyContext(mDisplay, mContext);
98 }
99 }
100
onSave(android::base::Stream * stream)101 void RenderContext::onSave(android::base::Stream* stream) {
102 stream->putBe32(mHndl);
103 stream->putBe32(static_cast<uint32_t>(mVersion));
104 assert(s_egl.eglCreateContext);
105 if (s_egl.eglSaveContext) {
106 s_egl.eglSaveContext(mDisplay, mContext, static_cast<EGLStream>(stream));
107 }
108 }
109
onLoad(android::base::Stream * stream,EGLDisplay display)110 RenderContext *RenderContext::onLoad(android::base::Stream* stream,
111 EGLDisplay display) {
112 HandleType hndl = static_cast<HandleType>(stream->getBe32());
113 GLESApi version = static_cast<GLESApi>(stream->getBe32());
114
115 return createImpl(display, (EGLConfig)0, EGL_NO_CONTEXT, hndl, version,
116 stream);
117 }
118
clientVersion() const119 GLESApi RenderContext::clientVersion() const {
120 return mVersion;
121 }
122