blob: e9741efa9a88e77f7b48ba84fff297a5341513d1 [file] [log] [blame]
Yin-Chia Yeh248ed702017-01-23 17:27:26 -08001/*
2 * Copyright (C) 2017 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 "HandleImporter"
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080018#include "HandleImporter.h"
Steven Moreland4e7a3072017-04-06 12:15:23 -070019#include <log/log.h>
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080020
21namespace android {
22namespace hardware {
23namespace camera {
24namespace common {
25namespace V1_0 {
26namespace helper {
27
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070028using MapperError = android::hardware::graphics::mapper::V2_0::Error;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080029
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070030HandleImporter::HandleImporter() : mInitialized(false) {}
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080031
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070032void HandleImporter::initializeLocked() {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080033 if (mInitialized) {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080034 return;
35 }
36
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070037 mMapper = IMapper::getService();
38 if (mMapper == nullptr) {
39 ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__);
40 return;
41 }
42
43 mInitialized = true;
44 return;
45}
46
47void HandleImporter::cleanup() {
48 mMapper.clear();
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080049 mInitialized = false;
50}
51
52// In IComposer, any buffer_handle_t is owned by the caller and we need to
53// make a clone for hwcomposer2. We also need to translate empty handle
54// to nullptr. This function does that, in-place.
55bool HandleImporter::importBuffer(buffer_handle_t& handle) {
56 if (!handle->numFds && !handle->numInts) {
57 handle = nullptr;
58 return true;
59 }
60
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070061 Mutex::Autolock lock(mLock);
62 if (!mInitialized) {
63 initializeLocked();
64 }
65
66 if (mMapper == nullptr) {
67 ALOGE("%s: mMapper is null!", __FUNCTION__);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080068 return false;
69 }
70
Yin-Chia Yeh519c1672017-04-21 14:59:31 -070071 MapperError error;
72 buffer_handle_t importedHandle;
73 auto ret = mMapper->importBuffer(
74 hidl_handle(handle),
75 [&](const auto& tmpError, const auto& tmpBufferHandle) {
76 error = tmpError;
77 importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle);
78 });
79
80 if (!ret.isOk()) {
81 ALOGE("%s: mapper importBuffer failed: %s",
82 __FUNCTION__, ret.description().c_str());
83 return false;
84 }
85
86 if (error != MapperError::NONE) {
87 return false;
88 }
89
90 handle = importedHandle;
91
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080092 return true;
93}
94
95void HandleImporter::freeBuffer(buffer_handle_t handle) {
96 if (!handle) {
97 return;
98 }
99
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700100 Mutex::Autolock lock(mLock);
101 if (mMapper == nullptr) {
102 ALOGE("%s: mMapper is null!", __FUNCTION__);
103 return;
104 }
105
106 auto ret = mMapper->freeBuffer(const_cast<native_handle_t*>(handle));
107 if (!ret.isOk()) {
108 ALOGE("%s: mapper freeBuffer failed: %s",
109 __FUNCTION__, ret.description().c_str());
110 }
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800111}
112
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700113bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800114 if (handle == nullptr || handle->numFds == 0) {
115 fd = -1;
116 } else if (handle->numFds == 1) {
117 fd = dup(handle->data[0]);
118 if (fd < 0) {
119 ALOGE("failed to dup fence fd %d", handle->data[0]);
120 return false;
121 }
122 } else {
123 ALOGE("invalid fence handle with %d file descriptors",
124 handle->numFds);
125 return false;
126 }
127
128 return true;
129}
130
Yin-Chia Yeh519c1672017-04-21 14:59:31 -0700131void HandleImporter::closeFence(int fd) const {
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800132 if (fd >= 0) {
133 close(fd);
134 }
135}
136
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700137YCbCrLayout HandleImporter::lockYCbCr(
138 buffer_handle_t& buf, uint64_t cpuUsage,
139 const IMapper::Rect& accessRegion) {
140 Mutex::Autolock lock(mLock);
141 YCbCrLayout layout = {};
142
143 if (!mInitialized) {
144 initializeLocked();
145 }
146
147 if (mMapper == nullptr) {
148 ALOGE("%s: mMapper is null!", __FUNCTION__);
149 return layout;
150 }
151
152 hidl_handle acquireFenceHandle;
153 auto buffer = const_cast<native_handle_t*>(buf);
154 mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle,
155 [&](const auto& tmpError, const auto& tmpLayout) {
156 if (tmpError == MapperError::NONE) {
157 layout = tmpLayout;
158 } else {
159 ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, tmpError);
160 }
161 });
162
163 ALOGV("%s: layout y %p cb %p cr %p y_str %d c_str %d c_step %d",
164 __FUNCTION__, layout.y, layout.cb, layout.cr,
165 layout.yStride, layout.cStride, layout.chromaStep);
166 return layout;
167}
168
169int HandleImporter::unlock(buffer_handle_t& buf) {
170 int releaseFence = -1;
171 auto buffer = const_cast<native_handle_t*>(buf);
172 mMapper->unlock(
173 buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
174 if (tmpError == MapperError::NONE) {
175 auto fenceHandle = tmpReleaseFence.getNativeHandle();
176 if (fenceHandle) {
177 if (fenceHandle->numInts != 0 || fenceHandle->numFds != 1) {
178 ALOGE("%s: bad release fence numInts %d numFds %d",
179 __FUNCTION__, fenceHandle->numInts, fenceHandle->numFds);
180 return;
181 }
182 releaseFence = dup(fenceHandle->data[0]);
183 if (releaseFence <= 0) {
184 ALOGE("%s: bad release fence FD %d",
185 __FUNCTION__, releaseFence);
186 }
187 }
188 } else {
189 ALOGE("%s: failed to unlock error %d!", __FUNCTION__, tmpError);
190 }
191 });
192
193 return releaseFence;
194}
195
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800196} // namespace helper
197} // namespace V1_0
198} // namespace common
199} // namespace camera
200} // namespace hardware
201} // namespace android