1 /*
2  * Copyright (C) 2018 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 <cstdint>
20 #include <map>
21 
22 #include <EGL/egl.h>
23 
24 struct EglContext {
25     enum GLESApi {
26         GLESApi_CM = 1,
27         GLESApi_2 = 2,
28         GLESApi_3_0 = 3,
29         GLESApi_3_1 = 4,
30     };
31 
32     static std::map<uint32_t, EglContext*> map;
33     static uint32_t nextId;
34 
EglContextEglContext35     EglContext(EGLContext context_, uint32_t ctx_, GLESApi api_)
36         : create_ctx(ctx_), context(context_), api(api_), id(nextId++) {
37         map.emplace(id, this);
38     }
39 
~EglContextEglContext40     ~EglContext() {
41         map.erase(id);
42     }
43 
bindEglContext44     EglContext* bind(uint32_t ctx_) {
45         for (auto const& it : EglContext::map) {
46             EglContext* ctx = it.second;
47             if (ctx == this)
48                 continue;
49             if (ctx->bound_ctx == ctx_)
50                 return ctx;
51         }
52         bound_ctx = ctx_;
53         return nullptr;
54     }
55 
unbindEglContext56     void unbind() {
57         bound_ctx = 0U;
58     }
59 
disposableEglContext60     bool disposable() {
61         return context == EGL_NO_CONTEXT && bound_ctx == 0U;
62     }
63 
64     uint32_t create_ctx;
65     EGLContext context;
66     enum GLESApi api;
67     uint32_t id;
68 
69   private:
70     uint32_t bound_ctx = 0U;
71 };
72