| 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 |
#include <bluegl/BlueGL.h> |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <mutex> |
| 21 |
|
| 22 |
#include "private_BlueGL.h" |
| 23 |
|
| 24 |
namespace bluegl { |
| 25 |
|
| 26 |
// OS Dependent. |
| 27 |
extern bool initBinder(); |
| 28 |
extern void* loadFunction(const char* name); |
| 29 |
extern void shutdownBinder(); |
| 30 |
|
| 31 |
static void undefined() { |
| 32 |
#ifndef NDEBUG |
| 33 |
std::cerr << "calling missing GL function!" << std::endl; |
| 34 |
#endif |
| 35 |
} |
| 36 |
|
| 37 |
int bind() { |
| 38 |
std::lock_guard<std::mutex> lock(g_library_mutex); |
| 39 |
g_library_refcount++; |
| 40 |
|
| 41 |
if (g_library_refcount == 1) { |
| 42 |
if (initBinder()) { |
| 43 |
for (unsigned int i = 0; i < blueCoreNumFunctions; i++) { |
| 44 |
*g_gl_stubs[i].api_call = loadFunction(g_gl_stubs[i].api_name); |
| 45 |
if (*g_gl_stubs[i].api_call == nullptr) { |
| 46 |
*g_gl_stubs[i].api_call = (void*)&undefined; |
| 47 |
} |
| 48 |
} |
| 49 |
return 0; |
| 50 |
} else { |
| 51 |
std::cerr << "Could not init binder." << std::endl; |
| 52 |
} |
| 53 |
} else { |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
return -1; |
| 58 |
} |
| 59 |
|
| 60 |
void unbind() { |
| 61 |
std::lock_guard<std::mutex> lock(g_library_mutex); |
| 62 |
uint32_t refcount = g_library_refcount; |
| 63 |
if (refcount > 0) { |
| 64 |
g_library_refcount--; |
| 65 |
} |
| 66 |
|
| 67 |
if (refcount == 1) { |
| 68 |
shutdownBinder(); |
| 69 |
|
| 70 |
for (unsigned int i = 0; i < 2574; i++) { |
| 71 |
*g_gl_stubs[i].api_call = nullptr; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
}; // namespace bluegl |