1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "OpenGLESDispatch/OpenGLDispatchLoader.h"
16 
17 #include "OpenGLESDispatch/DispatchTables.h"
18 
19 namespace gfxstream {
20 namespace gl {
21 
22 GLESv1Dispatch s_gles1;
23 GLESv2Dispatch s_gles2;
24 
25 // Must be declared outside of LazyLoaded*Dispatch scope due to the use of
26 // sizeof(T) within the template definition.
sGLESv1Dispatch()27 static LazyLoadedGLESv1Dispatch* sGLESv1Dispatch() {
28     static LazyLoadedGLESv1Dispatch* l = new LazyLoadedGLESv1Dispatch;
29     return l;
30 }
sGLESv2Dispatch()31 static LazyLoadedGLESv2Dispatch* sGLESv2Dispatch() {
32     static LazyLoadedGLESv2Dispatch* l = new LazyLoadedGLESv2Dispatch;
33     return l;
34 }
sEGLDispatch()35 static LazyLoadedEGLDispatch* sEGLDispatch() {
36     static LazyLoadedEGLDispatch* l = new LazyLoadedEGLDispatch;
37     return l;
38 }
39 
40 // static
get()41 const GLESv1Dispatch* LazyLoadedGLESv1Dispatch::get() {
42     LazyLoadedGLESv1Dispatch* instance = sGLESv1Dispatch();
43     if (instance->mValid) {
44         return &s_gles1;
45     } else {
46         return nullptr;
47     }
48 }
49 
LazyLoadedGLESv1Dispatch()50 LazyLoadedGLESv1Dispatch::LazyLoadedGLESv1Dispatch() {
51     LazyLoadedEGLDispatch::get();
52     mValid = gles1_dispatch_init(&s_gles1);
53 }
54 
55 // static
get()56 const GLESv2Dispatch* LazyLoadedGLESv2Dispatch::get() {
57     LazyLoadedGLESv2Dispatch* instance = sGLESv2Dispatch();
58     if (instance->mValid) {
59         return &s_gles2;
60     } else {
61         return nullptr;
62     }
63 }
64 
LazyLoadedGLESv2Dispatch()65 LazyLoadedGLESv2Dispatch::LazyLoadedGLESv2Dispatch() {
66     LazyLoadedEGLDispatch::get();
67     mValid = gles2_dispatch_init(&s_gles2);
68 }
69 
70 // static
get()71 const EGLDispatch* LazyLoadedEGLDispatch::get() {
72     LazyLoadedEGLDispatch* instance = sEGLDispatch();
73     if (instance->mValid) {
74         return &s_egl;
75     } else {
76         return nullptr;
77     }
78 }
79 
LazyLoadedEGLDispatch()80 LazyLoadedEGLDispatch::LazyLoadedEGLDispatch() { mValid = init_egl_dispatch(); }
81 
82 }  // namespace gl
83 }  // namespace gfxstream