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