1 // Copyright 2018 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 #include "GrallocDispatch.h"
15
16 #include "AndroidHostCommon.h"
17 #include "Ashmem.h"
18
19 #include <dlfcn.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 static struct gralloc_implementation* s_global_gralloc_impl = 0;
24
25 extern "C" {
26
load_gralloc_module(const char * path,struct gralloc_implementation * impl_out)27 EXPORT void load_gralloc_module(
28 const char* path,
29 struct gralloc_implementation* impl_out) {
30
31 memset(impl_out, 0x0, sizeof(*impl_out));
32
33 impl_out->lib = dlopen(path, RTLD_NOW);
34
35 if (!impl_out->lib) {
36 fprintf(stderr, "%s: failed to load gralloc module from %s\n", __func__, path);
37 return;
38 }
39
40 struct hw_module_t* hw_module = (struct hw_module_t*)dlsym(
41 impl_out->lib, HAL_MODULE_INFO_SYM_AS_STR);
42
43 gralloc_open(hw_module, &impl_out->alloc_dev);
44
45 if (!impl_out->alloc_dev) {
46 fprintf(stderr, "%s: Failed to load alloc device from %p\n",
47 __func__, impl_out->lib);
48 return;
49 }
50
51 impl_out->alloc_module = reinterpret_cast<gralloc_module_t*>(
52 impl_out->alloc_dev->common.module);
53 }
54
unload_gralloc_module(const struct gralloc_implementation * impl)55 EXPORT void unload_gralloc_module(
56 const struct gralloc_implementation* impl) {
57
58 if (!impl->lib) return;
59
60 gralloc_close(impl->alloc_dev);
61
62 dlclose(impl->lib);
63
64 ashmem_teardown();
65
66 if (s_global_gralloc_impl == impl) {
67 s_global_gralloc_impl = 0;
68 }
69 }
70
set_global_gralloc_module(struct gralloc_implementation * impl)71 EXPORT void set_global_gralloc_module(
72 struct gralloc_implementation* impl) {
73 s_global_gralloc_impl = impl;
74 }
75
76 EXPORT struct gralloc_implementation*
get_global_gralloc_module(void)77 get_global_gralloc_module(void) {
78 return s_global_gralloc_impl;
79 }
80
81 } // extern "C"
82