Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 | #define LOG_TAG "GrallocMapper" |
| 18 | |
| 19 | #include <array> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <ui/GrallocMapper.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace Gralloc2 { |
| 28 | |
| 29 | typedef const void*(*FetchInterface)(const char* name); |
| 30 | |
| 31 | static FetchInterface loadHalLib(const char* pkg_name) |
| 32 | { |
| 33 | static const std::array<const char*, 3> sSearchDirs = {{ |
| 34 | HAL_LIBRARY_PATH_ODM, |
| 35 | HAL_LIBRARY_PATH_VENDOR, |
| 36 | HAL_LIBRARY_PATH_SYSTEM, |
| 37 | }}; |
| 38 | static const char sSymbolName[] = "HALLIB_FETCH_Interface"; |
| 39 | |
| 40 | void* handle = nullptr; |
| 41 | std::string path; |
| 42 | for (auto dir : sSearchDirs) { |
| 43 | path = dir; |
| 44 | path += pkg_name; |
| 45 | path += ".hallib.so"; |
| 46 | handle = dlopen(path.c_str(), RTLD_LOCAL | RTLD_NOW); |
| 47 | if (handle) { |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | if (!handle) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
| 55 | void* symbol = dlsym(handle, sSymbolName); |
| 56 | if (!symbol) { |
| 57 | ALOGE("%s is missing from %s", sSymbolName, path.c_str()); |
| 58 | dlclose(handle); |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | return reinterpret_cast<FetchInterface>(symbol); |
| 63 | } |
| 64 | |
| 65 | Mapper::Mapper() |
| 66 | : mMapper(nullptr), mDevice(nullptr) |
| 67 | { |
| 68 | static const char sHalLibName[] = "android.hardware.graphics.mapper"; |
| 69 | static const char sSupportedInterface[] = |
| 70 | "android.hardware.graphics.mapper@2.0::IMapper"; |
| 71 | |
| 72 | FetchInterface fetchInterface = loadHalLib(sHalLibName); |
| 73 | if (!fetchInterface) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | mMapper = static_cast<const IMapper*>( |
| 78 | fetchInterface(sSupportedInterface)); |
| 79 | if (!mMapper) { |
| 80 | ALOGE("%s is not supported", sSupportedInterface); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (mMapper->createDevice(&mDevice) != Error::NONE) { |
| 85 | ALOGE("failed to create mapper device"); |
| 86 | mMapper = nullptr; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | Mapper::~Mapper() |
| 91 | { |
| 92 | if (mMapper) { |
| 93 | mMapper->destroyDevice(mDevice); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void Mapper::release(buffer_handle_t handle) const |
| 98 | { |
| 99 | auto error = mMapper->release(mDevice, handle); |
| 100 | ALOGE_IF(error != Error::NONE, |
| 101 | "release(%p) failed with %d", handle, error); |
| 102 | } |
| 103 | |
| 104 | int Mapper::unlock(buffer_handle_t handle) const |
| 105 | { |
| 106 | int releaseFence; |
| 107 | auto error = mMapper->unlock(mDevice, handle, &releaseFence); |
| 108 | if (error != Error::NONE) { |
| 109 | ALOGE("unlock(%p) failed with %d", handle, error); |
| 110 | releaseFence = -1; |
| 111 | } |
| 112 | |
| 113 | return releaseFence; |
| 114 | } |
| 115 | |
| 116 | } // namespace Gralloc2 |
| 117 | |
| 118 | } // namespace android |