| 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 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 29 | static constexpr Error kDefaultError = Error::NO_RESOURCES; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 30 |  | 
|  | 31 | Mapper::Mapper() | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 32 | { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 33 | mMapper = IMapper::getService("gralloc-mapper"); | 
|  | 34 | if (mMapper != nullptr && mMapper->isRemote()) { | 
|  | 35 | LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 36 | } | 
|  | 37 | } | 
|  | 38 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 39 | Error Mapper::retain(buffer_handle_t handle) const | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 40 | { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 41 | auto ret = mMapper->retain(handle); | 
|  | 42 | return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | void Mapper::release(buffer_handle_t handle) const | 
|  | 46 | { | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 47 | auto ret = mMapper->release(handle); | 
|  | 48 |  | 
|  | 49 | auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 50 | ALOGE_IF(error != Error::NONE, | 
|  | 51 | "release(%p) failed with %d", handle, error); | 
|  | 52 | } | 
|  | 53 |  | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 54 | Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const | 
|  | 55 | { | 
|  | 56 | Error error = kDefaultError; | 
|  | 57 | mMapper->getStride(handle, | 
|  | 58 | [&](const auto& tmpError, const auto& tmpStride) | 
|  | 59 | { | 
|  | 60 | error = tmpError; | 
|  | 61 | if (error != Error::NONE) { | 
|  | 62 | return; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | *outStride = tmpStride; | 
|  | 66 | }); | 
|  | 67 |  | 
|  | 68 | return error; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | Error Mapper::lock(buffer_handle_t handle, | 
|  | 72 | uint64_t producerUsageMask, | 
|  | 73 | uint64_t consumerUsageMask, | 
|  | 74 | const IMapper::Rect& accessRegion, | 
|  | 75 | int acquireFence, void** outData) const | 
|  | 76 | { | 
|  | 77 | hardware::hidl_handle acquireFenceHandle; | 
|  | 78 |  | 
|  | 79 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 80 | if (acquireFence >= 0) { | 
|  | 81 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 82 | h->data[0] = acquireFence; | 
|  | 83 | acquireFenceHandle = h; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | Error error = kDefaultError; | 
|  | 87 | mMapper->lock(handle, producerUsageMask, consumerUsageMask, | 
|  | 88 | accessRegion, acquireFenceHandle, | 
|  | 89 | [&](const auto& tmpError, const auto& tmpData) | 
|  | 90 | { | 
|  | 91 | error = tmpError; | 
|  | 92 | if (error != Error::NONE) { | 
|  | 93 | return; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | *outData = tmpData; | 
|  | 97 | }); | 
|  | 98 |  | 
|  | 99 | if (error == Error::NONE && acquireFence >= 0) { | 
|  | 100 | close(acquireFence); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | return error; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | Error Mapper::lock(buffer_handle_t handle, | 
|  | 107 | uint64_t producerUsageMask, | 
|  | 108 | uint64_t consumerUsageMask, | 
|  | 109 | const IMapper::Rect& accessRegion, | 
|  | 110 | int acquireFence, FlexLayout* outLayout) const | 
|  | 111 | { | 
|  | 112 | hardware::hidl_handle acquireFenceHandle; | 
|  | 113 |  | 
|  | 114 | NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); | 
|  | 115 | if (acquireFence >= 0) { | 
|  | 116 | auto h = native_handle_init(acquireFenceStorage, 1, 0); | 
|  | 117 | h->data[0] = acquireFence; | 
|  | 118 | acquireFenceHandle = h; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | Error error = kDefaultError; | 
|  | 122 | mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask, | 
|  | 123 | accessRegion, acquireFenceHandle, | 
|  | 124 | [&](const auto& tmpError, const auto& tmpLayout) | 
|  | 125 | { | 
|  | 126 | error = tmpError; | 
|  | 127 | if (error != Error::NONE) { | 
|  | 128 | return; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | *outLayout = tmpLayout; | 
|  | 132 | }); | 
|  | 133 |  | 
|  | 134 | if (error == Error::NONE && acquireFence >= 0) { | 
|  | 135 | close(acquireFence); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | return error; | 
|  | 139 | } | 
|  | 140 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 141 | int Mapper::unlock(buffer_handle_t handle) const | 
|  | 142 | { | 
| Fabien Sanglard | 521698f | 2017-01-10 13:45:45 -0800 | [diff] [blame] | 143 | int releaseFence = -1; | 
| Chia-I Wu | 3166947 | 2016-12-07 14:55:24 +0800 | [diff] [blame] | 144 |  | 
|  | 145 | Error error = kDefaultError; | 
|  | 146 | mMapper->unlock(handle, | 
|  | 147 | [&](const auto& tmpError, const auto& tmpReleaseFence) | 
|  | 148 | { | 
|  | 149 | error = tmpError; | 
|  | 150 | if (error != Error::NONE) { | 
|  | 151 | return; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); | 
|  | 155 | if (fenceHandle && fenceHandle->numFds == 1) { | 
|  | 156 | int fd = dup(fenceHandle->data[0]); | 
|  | 157 | if (fd >= 0) { | 
|  | 158 | releaseFence = fd; | 
|  | 159 | } else { | 
|  | 160 | error = Error::NO_RESOURCES; | 
|  | 161 | } | 
|  | 162 | } else { | 
|  | 163 | releaseFence = -1; | 
|  | 164 | } | 
|  | 165 | }); | 
|  | 166 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 167 | if (error != Error::NONE) { | 
|  | 168 | ALOGE("unlock(%p) failed with %d", handle, error); | 
|  | 169 | releaseFence = -1; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | return releaseFence; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | } // namespace Gralloc2 | 
|  | 176 |  | 
|  | 177 | } // namespace android |