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",
37 __func__, impl_out->lib);
38 return;
39 }
40
41 struct hw_module_t* hw_module = (struct hw_module_t*)dlsym(
42 impl_out->lib, HAL_MODULE_INFO_SYM_AS_STR);
43
44 gralloc_open(hw_module, &impl_out->alloc_dev);
45
46 if (!impl_out->alloc_dev) {
47 fprintf(stderr, "%s: Failed to load alloc device from %p\n",
48 __func__, impl_out->lib);
49 return;
50 }
51
52 impl_out->alloc_module = reinterpret_cast<gralloc_module_t*>(
53 impl_out->alloc_dev->common.module);
54 }
55
unload_gralloc_module(const struct gralloc_implementation * impl)56 EXPORT void unload_gralloc_module(
57 const struct gralloc_implementation* impl) {
58
59 if (!impl->lib) return;
60
61 gralloc_close(impl->alloc_dev);
62
63 dlclose(impl->lib);
64
65 ashmem_teardown();
66
67 if (s_global_gralloc_impl == impl) {
68 s_global_gralloc_impl = 0;
69 }
70 }
71
set_global_gralloc_module(struct gralloc_implementation * impl)72 EXPORT void set_global_gralloc_module(
73 struct gralloc_implementation* impl) {
74 s_global_gralloc_impl = impl;
75 }
76
77 EXPORT struct gralloc_implementation*
get_global_gralloc_module(void)78 get_global_gralloc_module(void) {
79 return s_global_gralloc_impl;
80 }
81
82 } // extern "C"
83