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 17 #pragma once 18 19 #include <memory> 20 #include <unordered_map> 21 #include <unordered_set> 22 23 #include <EGL/egl.h> 24 25 #include "GLDecoderContextData.h" 26 #include "Handle.h" 27 #include "aemu/base/files/Stream.h" 28 29 namespace gfxstream { 30 namespace gl { 31 32 // Tracks all the possible OpenGL ES API versions. 33 enum GLESApi { 34 GLESApi_CM = 1, 35 GLESApi_2 = 2, 36 GLESApi_3_0 = 3, 37 GLESApi_3_1 = 4, 38 GLESApi_3_2 = 5, 39 }; 40 41 // A class used to model a guest EGLContext. This simply wraps a host 42 // EGLContext, associated with an GLDecoderContextData instance that is 43 // used to store copies of guest-side arrays. 44 class EmulatedEglContext { 45 public: 46 // Create a new EmulatedEglContext instance. 47 // |display| is the host EGLDisplay handle. 48 // |config| is the host EGLConfig to use. 49 // |sharedContext| is either EGL_NO_CONTEXT of a host EGLContext handle. 50 // |version| specifies the GLES version as a GLESApi. 51 static std::unique_ptr<EmulatedEglContext> create(EGLDisplay display, 52 EGLConfig config, 53 EGLContext sharedContext, 54 HandleType hndl, 55 GLESApi = GLESApi_CM); 56 57 // Destructor. 58 ~EmulatedEglContext(); 59 60 // Retrieve host EGLContext value. getEGLContext()61 EGLContext getEGLContext() const { return mContext; } 62 63 // Return the GLES version it is trying to emulate in this context. 64 // This can be different from the underlying version when using 65 // GLES12Translator. 66 GLESApi clientVersion() const; 67 68 // Retrieve GLDecoderContextData instance reference for this 69 // EmulatedEglContext instance. decoderContextData()70 GLDecoderContextData& decoderContextData() { return mContextData; } 71 getHndl()72 HandleType getHndl() const { return mHndl; } 73 74 void onSave(android::base::Stream* stream); 75 static std::unique_ptr<EmulatedEglContext> onLoad(android::base::Stream* stream, 76 EGLDisplay display); 77 private: 78 EmulatedEglContext(EGLDisplay display, 79 EGLContext context, 80 HandleType hndl, 81 GLESApi version, 82 void* emulatedGLES1Context); 83 84 // Implementation of create 85 // |stream| is the stream to load from when restoring a snapshot, 86 // set |stream| to nullptr if it is not loading from a snapshot 87 static std::unique_ptr<EmulatedEglContext> createImpl(EGLDisplay display, 88 EGLConfig config, 89 EGLContext sharedContext, 90 HandleType hndl, 91 GLESApi version, 92 android::base::Stream *stream); 93 94 EGLDisplay mDisplay; 95 EGLContext mContext; 96 HandleType mHndl; 97 GLESApi mVersion; 98 GLDecoderContextData mContextData; 99 }; 100 101 typedef std::shared_ptr<EmulatedEglContext> EmulatedEglContextPtr; 102 typedef std::unordered_map<HandleType, EmulatedEglContextPtr> EmulatedEglContextMap; 103 typedef std::unordered_set<HandleType> EmulatedEglContextSet; 104 105 } // namespace gl 106 } // namespace gfxstream 107