1 /*
2  * Copyright (C) 2017 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 #include "cpp/rsDispatch.h"
18 #include "rsFallbackAdaptation.h"
19 
20 #include <log/log.h>
21 #include <dlfcn.h>
22 
23 #undef LOG_TAG
24 #define LOG_TAG "RenderScript Graphics Fallback"
25 
26 dispatchTable RsFallbackAdaptation::mEntryFuncs;
27 
RsFallbackAdaptation()28 RsFallbackAdaptation::RsFallbackAdaptation()
29 {
30     void* handle = dlopen("libRS_internal.so", RTLD_LAZY | RTLD_LOCAL);
31     if (handle == NULL) {
32         ALOGE("couldn't dlopen %s.", dlerror());
33         return;
34     }
35     if (loadSymbols(handle, mEntryFuncs) == false) {
36         // If dispatch table initialization failed, the dispatch table
37         // will be reset, and calling function pointers of uninitialized
38         // dispatch table will crash the application.
39         ALOGE("Fallback dispatch table init failed!");
40         mEntryFuncs = {};
41         dlclose(handle);
42     }
43 }
44 
GetInstance()45 RsFallbackAdaptation& RsFallbackAdaptation::GetInstance()
46 {
47     // This function-local-static guarantees the instance is a singleton. The
48     // constructor of RsHidlAdaptation will only be called when GetInstance is
49     // called for the first time.
50     static RsFallbackAdaptation instance;
51     return instance;
52 }
53 
GetEntryFuncs()54 const dispatchTable* RsFallbackAdaptation::GetEntryFuncs()
55 {
56     return &mEntryFuncs;
57 }
58 
59