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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #define LOG_TAG "GrallocMapperPassthrough"
18 #include "GrallocMapper.h"
22 #include <unordered_map>
26 #include <hardware/gralloc1.h>
34 namespace implementation {
38 using android::hardware::graphics::allocator::V2_0::Error;
39 using android::hardware::graphics::common::V1_0::PixelFormat;
41 class GrallocMapperHal : public IMapper {
43 GrallocMapperHal(const hw_module_t* module);
47 Return<Error> retain(const hidl_handle& bufferHandle) override;
48 Return<Error> release(const hidl_handle& bufferHandle) override;
49 Return<void> getDimensions(const hidl_handle& bufferHandle,
50 getDimensions_cb hidl_cb) override;
51 Return<void> getFormat(const hidl_handle& bufferHandle,
52 getFormat_cb hidl_cb) override;
53 Return<void> getLayerCount(const hidl_handle& bufferHandle,
54 getLayerCount_cb hidl_cb) override;
55 Return<void> getProducerUsageMask(const hidl_handle& bufferHandle,
56 getProducerUsageMask_cb hidl_cb) override;
57 Return<void> getConsumerUsageMask(const hidl_handle& bufferHandle,
58 getConsumerUsageMask_cb hidl_cb) override;
59 Return<void> getBackingStore(const hidl_handle& bufferHandle,
60 getBackingStore_cb hidl_cb) override;
61 Return<void> getStride(const hidl_handle& bufferHandle,
62 getStride_cb hidl_cb) override;
63 Return<void> lock(const hidl_handle& bufferHandle,
64 uint64_t producerUsageMask, uint64_t consumerUsageMask,
65 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
66 lock_cb hidl_cb) override;
67 Return<void> lockFlex(const hidl_handle& bufferHandle,
68 uint64_t producerUsageMask, uint64_t consumerUsageMask,
69 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
70 lockFlex_cb hidl_cb) override;
71 Return<void> unlock(const hidl_handle& bufferHandle,
72 unlock_cb hidl_cb) override;
75 void initCapabilities();
78 void initDispatch(gralloc1_function_descriptor_t desc, T* outPfn);
81 static gralloc1_rect_t asGralloc1Rect(const IMapper::Rect& rect);
82 static bool dupFence(const hidl_handle& fenceHandle, int* outFd);
84 gralloc1_device_t* mDevice;
91 GRALLOC1_PFN_RETAIN retain;
92 GRALLOC1_PFN_RELEASE release;
93 GRALLOC1_PFN_GET_DIMENSIONS getDimensions;
94 GRALLOC1_PFN_GET_FORMAT getFormat;
95 GRALLOC1_PFN_GET_LAYER_COUNT getLayerCount;
96 GRALLOC1_PFN_GET_PRODUCER_USAGE getProducerUsage;
97 GRALLOC1_PFN_GET_CONSUMER_USAGE getConsumerUsage;
98 GRALLOC1_PFN_GET_BACKING_STORE getBackingStore;
99 GRALLOC1_PFN_GET_STRIDE getStride;
100 GRALLOC1_PFN_GET_NUM_FLEX_PLANES getNumFlexPlanes;
101 GRALLOC1_PFN_LOCK lock;
102 GRALLOC1_PFN_LOCK_FLEX lockFlex;
103 GRALLOC1_PFN_UNLOCK unlock;
107 std::unordered_map<buffer_handle_t, size_t> mBufferReferenceCounts;
110 GrallocMapperHal::GrallocMapperHal(const hw_module_t* module)
111 : mDevice(nullptr), mCapabilities(), mDispatch()
113 int status = gralloc1_open(module, &mDevice);
115 LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s",
123 GrallocMapperHal::~GrallocMapperHal()
125 gralloc1_close(mDevice);
128 void GrallocMapperHal::initCapabilities()
131 mDevice->getCapabilities(mDevice, &count, nullptr);
133 std::vector<int32_t> caps(count);
134 mDevice->getCapabilities(mDevice, &count, caps.data());
137 for (auto cap : caps) {
139 case GRALLOC1_CAPABILITY_LAYERED_BUFFERS:
140 mCapabilities.layeredBuffers = true;
149 void GrallocMapperHal::initDispatch(gralloc1_function_descriptor_t desc,
152 auto pfn = mDevice->getFunction(mDevice, desc);
154 LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc);
157 *outPfn = reinterpret_cast<T>(pfn);
160 void GrallocMapperHal::initDispatch()
162 initDispatch(GRALLOC1_FUNCTION_RETAIN, &mDispatch.retain);
163 initDispatch(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release);
164 initDispatch(GRALLOC1_FUNCTION_GET_DIMENSIONS, &mDispatch.getDimensions);
165 initDispatch(GRALLOC1_FUNCTION_GET_FORMAT, &mDispatch.getFormat);
166 if (mCapabilities.layeredBuffers) {
167 initDispatch(GRALLOC1_FUNCTION_GET_LAYER_COUNT,
168 &mDispatch.getLayerCount);
170 initDispatch(GRALLOC1_FUNCTION_GET_PRODUCER_USAGE,
171 &mDispatch.getProducerUsage);
172 initDispatch(GRALLOC1_FUNCTION_GET_CONSUMER_USAGE,
173 &mDispatch.getConsumerUsage);
174 initDispatch(GRALLOC1_FUNCTION_GET_BACKING_STORE,
175 &mDispatch.getBackingStore);
176 initDispatch(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride);
177 initDispatch(GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES,
178 &mDispatch.getNumFlexPlanes);
179 initDispatch(GRALLOC1_FUNCTION_LOCK, &mDispatch.lock);
180 initDispatch(GRALLOC1_FUNCTION_LOCK_FLEX, &mDispatch.lockFlex);
181 initDispatch(GRALLOC1_FUNCTION_UNLOCK, &mDispatch.unlock);
184 gralloc1_rect_t GrallocMapperHal::asGralloc1Rect(const IMapper::Rect& rect)
186 return gralloc1_rect_t{rect.left, rect.top, rect.width, rect.height};
189 bool GrallocMapperHal::dupFence(const hidl_handle& fenceHandle, int* outFd)
191 auto handle = fenceHandle.getNativeHandle();
192 if (!handle || handle->numFds == 0) {
197 if (handle->numFds > 1) {
198 ALOGE("invalid fence handle with %d fds", handle->numFds);
202 *outFd = dup(handle->data[0]);
203 return (*outFd >= 0);
206 Return<Error> GrallocMapperHal::retain(const hidl_handle& bufferHandle)
208 int32_t err = mDispatch.retain(mDevice, bufferHandle);
209 if (err == GRALLOC1_ERROR_NONE) {
210 auto nativeHandle = bufferHandle.getNativeHandle();
211 std::lock_guard<std::mutex> lock(mMutex);
213 ++mBufferReferenceCounts[nativeHandle];
215 return static_cast<Error>(err);
218 Return<Error> GrallocMapperHal::release(const hidl_handle& bufferHandle)
220 int32_t err = mDispatch.release(mDevice, bufferHandle);
221 if (err == GRALLOC1_ERROR_NONE) {
222 auto nativeHandle = bufferHandle.getNativeHandle();
223 std::lock_guard<std::mutex> lock(mMutex);
225 auto iter = mBufferReferenceCounts.find(bufferHandle);
226 if (iter == mBufferReferenceCounts.end()) {
227 // this should never happen
228 err = GRALLOC1_ERROR_BAD_HANDLE;
229 } else if (--iter->second == 0) {
230 native_handle_close(nativeHandle);
231 native_handle_delete(const_cast<native_handle_t*>(nativeHandle));
233 mBufferReferenceCounts.erase(iter);
237 return static_cast<Error>(err);
240 Return<void> GrallocMapperHal::getDimensions(const hidl_handle& bufferHandle,
241 getDimensions_cb hidl_cb)
245 int32_t err = mDispatch.getDimensions(mDevice, bufferHandle,
248 hidl_cb(static_cast<Error>(err), width, height);
252 Return<void> GrallocMapperHal::getFormat(const hidl_handle& bufferHandle,
253 getFormat_cb hidl_cb)
255 int32_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
256 int32_t err = mDispatch.getFormat(mDevice, bufferHandle, &format);
258 hidl_cb(static_cast<Error>(err), static_cast<PixelFormat>(format));
262 Return<void> GrallocMapperHal::getLayerCount(const hidl_handle& bufferHandle,
263 getLayerCount_cb hidl_cb)
265 int32_t err = GRALLOC1_ERROR_NONE;
267 if (mCapabilities.layeredBuffers) {
268 err = mDispatch.getLayerCount(mDevice, bufferHandle, &count);
271 hidl_cb(static_cast<Error>(err), count);
275 Return<void> GrallocMapperHal::getProducerUsageMask(
276 const hidl_handle& bufferHandle, getProducerUsageMask_cb hidl_cb)
279 int32_t err = mDispatch.getProducerUsage(mDevice, bufferHandle, &mask);
281 hidl_cb(static_cast<Error>(err), mask);
285 Return<void> GrallocMapperHal::getConsumerUsageMask(
286 const hidl_handle& bufferHandle, getConsumerUsageMask_cb hidl_cb)
289 int32_t err = mDispatch.getConsumerUsage(mDevice, bufferHandle, &mask);
291 hidl_cb(static_cast<Error>(err), mask);
295 Return<void> GrallocMapperHal::getBackingStore(
296 const hidl_handle& bufferHandle, getBackingStore_cb hidl_cb)
299 int32_t err = mDispatch.getBackingStore(mDevice, bufferHandle, &store);
301 hidl_cb(static_cast<Error>(err), store);
305 Return<void> GrallocMapperHal::getStride(const hidl_handle& bufferHandle,
306 getStride_cb hidl_cb)
309 int32_t err = mDispatch.getStride(mDevice, bufferHandle, &stride);
311 hidl_cb(static_cast<Error>(err), stride);
315 Return<void> GrallocMapperHal::lock(const hidl_handle& bufferHandle,
316 uint64_t producerUsageMask, uint64_t consumerUsageMask,
317 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
320 gralloc1_rect_t rect = asGralloc1Rect(accessRegion);
323 if (!dupFence(acquireFence, &fence)) {
324 hidl_cb(Error::NO_RESOURCES, nullptr);
328 void* data = nullptr;
329 int32_t err = mDispatch.lock(mDevice, bufferHandle, producerUsageMask,
330 consumerUsageMask, &rect, &data, fence);
331 if (err != GRALLOC1_ERROR_NONE) {
335 hidl_cb(static_cast<Error>(err), data);
339 Return<void> GrallocMapperHal::lockFlex(const hidl_handle& bufferHandle,
340 uint64_t producerUsageMask, uint64_t consumerUsageMask,
341 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
344 FlexLayout layout_reply{};
346 uint32_t planeCount = 0;
347 int32_t err = mDispatch.getNumFlexPlanes(mDevice, bufferHandle,
349 if (err != GRALLOC1_ERROR_NONE) {
350 hidl_cb(static_cast<Error>(err), layout_reply);
354 gralloc1_rect_t rect = asGralloc1Rect(accessRegion);
357 if (!dupFence(acquireFence, &fence)) {
358 hidl_cb(Error::NO_RESOURCES, layout_reply);
362 std::vector<android_flex_plane_t> planes(planeCount);
363 android_flex_layout_t layout{};
364 layout.num_planes = planes.size();
365 layout.planes = planes.data();
367 err = mDispatch.lockFlex(mDevice, bufferHandle, producerUsageMask,
368 consumerUsageMask, &rect, &layout, fence);
369 if (err == GRALLOC1_ERROR_NONE) {
370 layout_reply.format = static_cast<FlexFormat>(layout.format);
372 planes.resize(layout.num_planes);
373 layout_reply.planes.setToExternal(
374 reinterpret_cast<FlexPlane*>(planes.data()), planes.size());
379 hidl_cb(static_cast<Error>(err), layout_reply);
383 Return<void> GrallocMapperHal::unlock(const hidl_handle& bufferHandle,
387 int32_t err = mDispatch.unlock(mDevice, bufferHandle, &fence);
389 NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0);
390 hidl_handle fenceHandle;
391 if (err == GRALLOC1_ERROR_NONE && fence >= 0) {
392 auto nativeHandle = native_handle_init(fenceStorage, 1, 0);
393 nativeHandle->data[0] = fence;
395 fenceHandle = nativeHandle;
398 hidl_cb(static_cast<Error>(err), fenceHandle);
402 } // anonymous namespace
404 IMapper* HIDL_FETCH_IMapper(const char* /* name */) {
405 const hw_module_t* module = nullptr;
406 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
408 ALOGE("failed to get gralloc module");
412 uint8_t major = (module->module_api_version >> 8) & 0xff;
414 ALOGE("unknown gralloc module major version %d", major);
418 return new GrallocMapperHal(module);
421 } // namespace implementation
423 } // namespace mapper
424 } // namespace graphics
425 } // namespace hardware
426 } // namespace android