blob: e06fbcbc1e5cfeaa22857c74232e685f39be2601 [file] [log] [blame]
Chia-I Wu0f215c52016-10-11 11:48:21 +08001/*
2 * Copyright 2016 The Android Open Source Project
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 */
15
16#define LOG_TAG "GrallocMapperPassthrough"
17
Chia-I Wu158d5302016-10-04 06:00:12 +080018#include "GrallocMapper.h"
19
Chia-I Wu79d13ff2017-03-31 12:48:11 -070020#include "Gralloc0Mapper.h"
21#include "Gralloc1Mapper.h"
22#include "GrallocBufferDescriptor.h"
Chia-I Wu158d5302016-10-04 06:00:12 +080023
Chia-I Wu79d13ff2017-03-31 12:48:11 -070024#include <inttypes.h>
Chia-I Wu158d5302016-10-04 06:00:12 +080025
Chia-I Wu0f215c52016-10-11 11:48:21 +080026#include <log/log.h>
Chia-I Wu79d13ff2017-03-31 12:48:11 -070027#include <sync/sync.h>
Chia-I Wu0f215c52016-10-11 11:48:21 +080028
29namespace android {
30namespace hardware {
31namespace graphics {
32namespace mapper {
33namespace V2_0 {
34namespace implementation {
35
Chia-I Wu79d13ff2017-03-31 12:48:11 -070036using android::hardware::graphics::common::V1_0::BufferUsage;
Chia-I Wu158d5302016-10-04 06:00:12 +080037using android::hardware::graphics::common::V1_0::PixelFormat;
38
Chia-I Wu78b63dc2017-04-19 11:03:00 -070039namespace {
40
41class RegisteredHandlePool {
42 public:
43 bool add(buffer_handle_t bufferHandle) {
44 std::lock_guard<std::mutex> lock(mMutex);
45 return mHandles.insert(bufferHandle).second;
46 }
47
48 native_handle_t* pop(void* buffer) {
49 auto bufferHandle = static_cast<native_handle_t*>(buffer);
50
51 std::lock_guard<std::mutex> lock(mMutex);
52 return mHandles.erase(bufferHandle) == 1 ? bufferHandle : nullptr;
53 }
54
55 buffer_handle_t get(const void* buffer) {
56 auto bufferHandle = static_cast<buffer_handle_t>(buffer);
57
58 std::lock_guard<std::mutex> lock(mMutex);
59 return mHandles.count(bufferHandle) == 1 ? bufferHandle : nullptr;
60 }
61
62 private:
63 std::mutex mMutex;
64 std::unordered_set<buffer_handle_t> mHandles;
65};
66
67// GraphicBufferMapper is expected to be valid (and leaked) during process
68// termination. We need to make sure IMapper, and in turn, gRegisteredHandles
69// are valid as well. Create the registered handle pool on the heap, and let
70// it leak for simplicity.
71//
72// However, there is no way to make sure gralloc0/gralloc1 are valid. Any use
73// of static/global object in gralloc0/gralloc1 that may have been destructed
74// is potentially broken.
75RegisteredHandlePool* gRegisteredHandles = new RegisteredHandlePool;
76
77} // anonymous namespace
Chia-I Wu0f215c52016-10-11 11:48:21 +080078
Chia-I Wu79d13ff2017-03-31 12:48:11 -070079bool GrallocMapper::validateDescriptorInfo(
80 const BufferDescriptorInfo& descriptorInfo) const {
81 const uint64_t validUsageBits =
82 BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK |
83 BufferUsage::GPU_TEXTURE | BufferUsage::GPU_RENDER_TARGET |
84 BufferUsage::COMPOSER_OVERLAY | BufferUsage::COMPOSER_CLIENT_TARGET |
85 BufferUsage::PROTECTED | BufferUsage::COMPOSER_CURSOR |
86 BufferUsage::VIDEO_ENCODER | BufferUsage::CAMERA_OUTPUT |
87 BufferUsage::CAMERA_INPUT | BufferUsage::RENDERSCRIPT |
88 BufferUsage::VIDEO_DECODER | BufferUsage::SENSOR_DIRECT_DATA |
89 BufferUsage::GPU_DATA_BUFFER | BufferUsage::VENDOR_MASK |
90 (mCapabilities.highUsageBits ? BufferUsage::VENDOR_MASK_HI
91 : static_cast<BufferUsage>(0));
Chia-I Wu0f215c52016-10-11 11:48:21 +080092
Chia-I Wu79d13ff2017-03-31 12:48:11 -070093 if (!descriptorInfo.width || !descriptorInfo.height ||
94 !descriptorInfo.layerCount) {
Chia-I Wu158d5302016-10-04 06:00:12 +080095 return false;
96 }
97
Chia-I Wu79d13ff2017-03-31 12:48:11 -070098 if (!mCapabilities.layeredBuffers && descriptorInfo.layerCount > 1) {
99 return false;
Chia-I Wu939e4012016-12-12 21:51:33 +0800100 }
101
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700102 if (descriptorInfo.format == static_cast<PixelFormat>(0)) {
103 return false;
Chia-I Wu158d5302016-10-04 06:00:12 +0800104 }
105
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700106 if (descriptorInfo.usage & ~validUsageBits) {
107 // could not fail as gralloc may use the reserved bits...
108 ALOGW("buffer descriptor with invalid usage bits 0x%" PRIx64,
109 descriptorInfo.usage & ~validUsageBits);
110 }
111
112 return true;
113}
114
115Return<void> GrallocMapper::createDescriptor(
116 const BufferDescriptorInfo& descriptorInfo, createDescriptor_cb hidl_cb) {
117 if (validateDescriptorInfo(descriptorInfo)) {
118 hidl_cb(Error::NONE, grallocEncodeBufferDescriptor(descriptorInfo));
119 } else {
120 hidl_cb(Error::BAD_VALUE, BufferDescriptor());
121 }
122
Chia-I Wu158d5302016-10-04 06:00:12 +0800123 return Void();
124}
125
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700126Return<void> GrallocMapper::importBuffer(const hidl_handle& rawHandle,
127 importBuffer_cb hidl_cb) {
Chia-I Wuec745962017-04-21 15:02:22 -0700128 // because of passthrough HALs, we must not generate an error when
129 // rawHandle has been imported
Chia-I Wu158d5302016-10-04 06:00:12 +0800130
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700131 if (!rawHandle.getNativeHandle()) {
132 hidl_cb(Error::BAD_BUFFER, nullptr);
133 return Void();
134 }
Chia-I Wu158d5302016-10-04 06:00:12 +0800135
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700136 native_handle_t* bufferHandle =
137 native_handle_clone(rawHandle.getNativeHandle());
138 if (!bufferHandle) {
Chia-I Wu158d5302016-10-04 06:00:12 +0800139 hidl_cb(Error::NO_RESOURCES, nullptr);
140 return Void();
141 }
142
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700143 Error error = registerBuffer(bufferHandle);
144 if (error != Error::NONE) {
145 native_handle_close(bufferHandle);
146 native_handle_delete(bufferHandle);
147
148 hidl_cb(error, nullptr);
149 return Void();
150 }
151
152 // The newly cloned handle is already registered? This can only happen
153 // when a handle previously registered was native_handle_delete'd instead
154 // of freeBuffer'd.
Chia-I Wu78b63dc2017-04-19 11:03:00 -0700155 if (!gRegisteredHandles->add(bufferHandle)) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700156 ALOGE("handle %p has already been imported; potential fd leaking",
157 bufferHandle);
158 unregisterBuffer(bufferHandle);
159 if (!mCapabilities.unregisterImplyDelete) {
160 native_handle_close(bufferHandle);
161 native_handle_delete(bufferHandle);
162 }
163
164 hidl_cb(Error::NO_RESOURCES, nullptr);
165 return Void();
166 }
167
168 hidl_cb(Error::NONE, bufferHandle);
169 return Void();
170}
171
172Return<Error> GrallocMapper::freeBuffer(void* buffer) {
Chia-I Wu78b63dc2017-04-19 11:03:00 -0700173 native_handle_t* bufferHandle = gRegisteredHandles->pop(buffer);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700174 if (!bufferHandle) {
175 return Error::BAD_BUFFER;
176 }
177
178 unregisterBuffer(bufferHandle);
179 if (!mCapabilities.unregisterImplyDelete) {
180 native_handle_close(bufferHandle);
181 native_handle_delete(bufferHandle);
182 }
183
184 return Error::NONE;
185}
186
187void GrallocMapper::waitFenceFd(int fenceFd, const char* logname) {
188 if (fenceFd < 0) {
189 return;
190 }
191
192 const int warningTimeout = 3500;
193 const int error = sync_wait(fenceFd, warningTimeout);
194 if (error < 0 && errno == ETIME) {
195 ALOGE("%s: fence %d didn't signal in %u ms", logname, fenceFd,
196 warningTimeout);
197 sync_wait(fenceFd, -1);
198 }
199}
200
201bool GrallocMapper::getFenceFd(const hidl_handle& fenceHandle,
202 int* outFenceFd) {
203 auto handle = fenceHandle.getNativeHandle();
204 if (handle && handle->numFds > 1) {
205 ALOGE("invalid fence handle with %d fds", handle->numFds);
206 return false;
207 }
208
209 *outFenceFd = (handle && handle->numFds == 1) ? handle->data[0] : -1;
210 return true;
211}
212
213hidl_handle GrallocMapper::getFenceHandle(int fenceFd, char* handleStorage) {
214 native_handle_t* handle = nullptr;
215 if (fenceFd >= 0) {
216 handle = native_handle_init(handleStorage, 1, 0);
217 handle->data[0] = fenceFd;
218 }
219
220 return hidl_handle(handle);
221}
222
223Return<void> GrallocMapper::lock(void* buffer, uint64_t cpuUsage,
224 const IMapper::Rect& accessRegion,
225 const hidl_handle& acquireFence,
226 lock_cb hidl_cb) {
Chia-I Wu78b63dc2017-04-19 11:03:00 -0700227 buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700228 if (!bufferHandle) {
229 hidl_cb(Error::BAD_BUFFER, nullptr);
230 return Void();
231 }
232
233 int fenceFd;
234 if (!getFenceFd(acquireFence, &fenceFd)) {
235 hidl_cb(Error::BAD_VALUE, nullptr);
236 return Void();
237 }
238
Chia-I Wu158d5302016-10-04 06:00:12 +0800239 void* data = nullptr;
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700240 Error error =
241 lockBuffer(bufferHandle, cpuUsage, accessRegion, fenceFd, &data);
Chia-I Wu158d5302016-10-04 06:00:12 +0800242
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700243 hidl_cb(error, data);
Chia-I Wu158d5302016-10-04 06:00:12 +0800244 return Void();
245}
246
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700247Return<void> GrallocMapper::lockYCbCr(void* buffer, uint64_t cpuUsage,
248 const IMapper::Rect& accessRegion,
249 const hidl_handle& acquireFence,
250 lockYCbCr_cb hidl_cb) {
251 YCbCrLayout layout = {};
Chia-I Wu158d5302016-10-04 06:00:12 +0800252
Chia-I Wu78b63dc2017-04-19 11:03:00 -0700253 buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700254 if (!bufferHandle) {
255 hidl_cb(Error::BAD_BUFFER, layout);
Chia-I Wu158d5302016-10-04 06:00:12 +0800256 return Void();
257 }
258
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700259 int fenceFd;
260 if (!getFenceFd(acquireFence, &fenceFd)) {
261 hidl_cb(Error::BAD_VALUE, layout);
Chia-I Wu158d5302016-10-04 06:00:12 +0800262 return Void();
263 }
264
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700265 Error error =
266 lockBuffer(bufferHandle, cpuUsage, accessRegion, fenceFd, &layout);
Chia-I Wu158d5302016-10-04 06:00:12 +0800267
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700268 hidl_cb(error, layout);
269 return Void();
270}
Chia-I Wu158d5302016-10-04 06:00:12 +0800271
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700272Return<void> GrallocMapper::unlock(void* buffer, unlock_cb hidl_cb) {
Chia-I Wu78b63dc2017-04-19 11:03:00 -0700273 buffer_handle_t bufferHandle = gRegisteredHandles->get(buffer);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700274 if (!bufferHandle) {
275 hidl_cb(Error::BAD_BUFFER, nullptr);
276 return Void();
277 }
278
279 int fenceFd;
280 Error error = unlockBuffer(bufferHandle, &fenceFd);
281 if (error == Error::NONE) {
282 NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0);
283
284 hidl_cb(error, getFenceHandle(fenceFd, fenceStorage));
285
286 if (fenceFd >= 0) {
287 close(fenceFd);
288 }
Craig Donner0b00adf2016-10-20 17:12:58 -0700289 } else {
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700290 hidl_cb(error, nullptr);
Craig Donner0b00adf2016-10-20 17:12:58 -0700291 }
Chia-I Wu158d5302016-10-04 06:00:12 +0800292
Chia-I Wu158d5302016-10-04 06:00:12 +0800293 return Void();
Craig Donner0b00adf2016-10-20 17:12:58 -0700294}
295
Jesse Hall33c08a52017-06-07 14:37:34 -0700296namespace {
297// Load the gralloc module when this shared library is loaded, rather than
298// waiting until HIDL_FETCH_IMapper is called. This allows it (and its
299// dependencies) to be loaded by Zygote, reducing app startup time and sharing
300// pages dirtied during library load between all apps.
301struct GrallocModule {
302 const hw_module_t* module;
303 GrallocModule() : module(nullptr) {
304 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
305 ALOGE_IF(err, "failed to get gralloc module: %s (%d)", strerror(-err),
306 err);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800307 }
Jesse Hall33c08a52017-06-07 14:37:34 -0700308};
309GrallocModule gGralloc;
310} // namespace
Chia-I Wu0f215c52016-10-11 11:48:21 +0800311
Jesse Hall33c08a52017-06-07 14:37:34 -0700312IMapper* HIDL_FETCH_IMapper(const char* /* name */) {
313 if (!gGralloc.module) return nullptr;
314
315 uint8_t major = (gGralloc.module->module_api_version >> 8) & 0xff;
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700316 switch (major) {
317 case 1:
Jesse Hall33c08a52017-06-07 14:37:34 -0700318 return new Gralloc1Mapper(gGralloc.module);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700319 case 0:
Jesse Hall33c08a52017-06-07 14:37:34 -0700320 return new Gralloc0Mapper(gGralloc.module);
Chia-I Wu79d13ff2017-03-31 12:48:11 -0700321 default:
322 ALOGE("unknown gralloc module major version %d", major);
323 return nullptr;
Chia-I Wu0f215c52016-10-11 11:48:21 +0800324 }
Chia-I Wu0f215c52016-10-11 11:48:21 +0800325}
326
327} // namespace implementation
328} // namespace V2_0
329} // namespace mapper
330} // namespace graphics
331} // namespace hardware
332} // namespace android