blob: cd9db386a46a47ffef5e061ee70eec83ed88d7de [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
20#include <vector>
21
22#include <string.h>
23
Chia-I Wu0f215c52016-10-11 11:48:21 +080024#include <hardware/gralloc1.h>
25#include <log/log.h>
26
27namespace android {
28namespace hardware {
29namespace graphics {
30namespace mapper {
31namespace V2_0 {
32namespace implementation {
33
Chia-I Wu158d5302016-10-04 06:00:12 +080034namespace {
Craig Donner0b00adf2016-10-20 17:12:58 -070035
Chia-I Wu158d5302016-10-04 06:00:12 +080036using android::hardware::graphics::allocator::V2_0::Error;
37using android::hardware::graphics::common::V1_0::PixelFormat;
38
39class GrallocMapperHal : public IMapper {
Chia-I Wu0f215c52016-10-11 11:48:21 +080040public:
Chia-I Wu158d5302016-10-04 06:00:12 +080041 GrallocMapperHal(const hw_module_t* module);
42 ~GrallocMapperHal();
Chia-I Wu0f215c52016-10-11 11:48:21 +080043
44 // IMapper interface
Chia-I Wu158d5302016-10-04 06:00:12 +080045 Return<Error> retain(const hidl_handle& bufferHandle) override;
46 Return<Error> release(const hidl_handle& bufferHandle) override;
47 Return<void> getDimensions(const hidl_handle& bufferHandle,
48 getDimensions_cb hidl_cb) override;
49 Return<void> getFormat(const hidl_handle& bufferHandle,
50 getFormat_cb hidl_cb) override;
51 Return<void> getLayerCount(const hidl_handle& bufferHandle,
52 getLayerCount_cb hidl_cb) override;
53 Return<void> getProducerUsageMask(const hidl_handle& bufferHandle,
54 getProducerUsageMask_cb hidl_cb) override;
55 Return<void> getConsumerUsageMask(const hidl_handle& bufferHandle,
56 getConsumerUsageMask_cb hidl_cb) override;
57 Return<void> getBackingStore(const hidl_handle& bufferHandle,
58 getBackingStore_cb hidl_cb) override;
59 Return<void> getStride(const hidl_handle& bufferHandle,
60 getStride_cb hidl_cb) override;
61 Return<void> lock(const hidl_handle& bufferHandle,
Chia-I Wu0f215c52016-10-11 11:48:21 +080062 uint64_t producerUsageMask, uint64_t consumerUsageMask,
Chia-I Wu158d5302016-10-04 06:00:12 +080063 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
64 lock_cb hidl_cb) override;
65 Return<void> lockFlex(const hidl_handle& bufferHandle,
Chia-I Wu0f215c52016-10-11 11:48:21 +080066 uint64_t producerUsageMask, uint64_t consumerUsageMask,
Chia-I Wu158d5302016-10-04 06:00:12 +080067 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
68 lockFlex_cb hidl_cb) override;
69 Return<void> unlock(const hidl_handle& bufferHandle,
70 unlock_cb hidl_cb) override;
Chia-I Wu0f215c52016-10-11 11:48:21 +080071
72private:
Craig Donner0b00adf2016-10-20 17:12:58 -070073 void initCapabilities();
74
Chia-I Wu158d5302016-10-04 06:00:12 +080075 template<typename T>
76 void initDispatch(gralloc1_function_descriptor_t desc, T* outPfn);
Chia-I Wu0f215c52016-10-11 11:48:21 +080077 void initDispatch();
Chia-I Wu158d5302016-10-04 06:00:12 +080078
79 static gralloc1_rect_t asGralloc1Rect(const IMapper::Rect& rect);
80 static bool dupFence(const hidl_handle& fenceHandle, int* outFd);
Chia-I Wu0f215c52016-10-11 11:48:21 +080081
82 gralloc1_device_t* mDevice;
83
Chia-I Wu158d5302016-10-04 06:00:12 +080084 struct {
85 bool layeredBuffers;
86 } mCapabilities;
Craig Donner0b00adf2016-10-20 17:12:58 -070087
Chia-I Wu0f215c52016-10-11 11:48:21 +080088 struct {
89 GRALLOC1_PFN_RETAIN retain;
90 GRALLOC1_PFN_RELEASE release;
91 GRALLOC1_PFN_GET_DIMENSIONS getDimensions;
92 GRALLOC1_PFN_GET_FORMAT getFormat;
Craig Donner0b00adf2016-10-20 17:12:58 -070093 GRALLOC1_PFN_GET_LAYER_COUNT getLayerCount;
Chia-I Wu0f215c52016-10-11 11:48:21 +080094 GRALLOC1_PFN_GET_PRODUCER_USAGE getProducerUsage;
95 GRALLOC1_PFN_GET_CONSUMER_USAGE getConsumerUsage;
96 GRALLOC1_PFN_GET_BACKING_STORE getBackingStore;
97 GRALLOC1_PFN_GET_STRIDE getStride;
98 GRALLOC1_PFN_GET_NUM_FLEX_PLANES getNumFlexPlanes;
99 GRALLOC1_PFN_LOCK lock;
100 GRALLOC1_PFN_LOCK_FLEX lockFlex;
101 GRALLOC1_PFN_UNLOCK unlock;
102 } mDispatch;
103};
104
Chia-I Wu158d5302016-10-04 06:00:12 +0800105GrallocMapperHal::GrallocMapperHal(const hw_module_t* module)
106 : mDevice(nullptr), mCapabilities(), mDispatch()
Chia-I Wu0f215c52016-10-11 11:48:21 +0800107{
Chia-I Wu158d5302016-10-04 06:00:12 +0800108 int status = gralloc1_open(module, &mDevice);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800109 if (status) {
Chia-I Wu158d5302016-10-04 06:00:12 +0800110 LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s",
111 strerror(-status));
Chia-I Wu0f215c52016-10-11 11:48:21 +0800112 }
113
Craig Donner0b00adf2016-10-20 17:12:58 -0700114 initCapabilities();
Chia-I Wu0f215c52016-10-11 11:48:21 +0800115 initDispatch();
116}
117
Chia-I Wu158d5302016-10-04 06:00:12 +0800118GrallocMapperHal::~GrallocMapperHal()
Chia-I Wu0f215c52016-10-11 11:48:21 +0800119{
120 gralloc1_close(mDevice);
121}
122
Chia-I Wu158d5302016-10-04 06:00:12 +0800123void GrallocMapperHal::initCapabilities()
Craig Donner0b00adf2016-10-20 17:12:58 -0700124{
125 uint32_t count;
126 mDevice->getCapabilities(mDevice, &count, nullptr);
127
Chia-I Wu158d5302016-10-04 06:00:12 +0800128 std::vector<int32_t> caps(count);
129 mDevice->getCapabilities(mDevice, &count, caps.data());
Craig Donner0b00adf2016-10-20 17:12:58 -0700130 caps.resize(count);
131
Chia-I Wu158d5302016-10-04 06:00:12 +0800132 for (auto cap : caps) {
133 switch (cap) {
134 case GRALLOC1_CAPABILITY_LAYERED_BUFFERS:
135 mCapabilities.layeredBuffers = true;
136 break;
137 default:
138 break;
139 }
Craig Donner0b00adf2016-10-20 17:12:58 -0700140 }
Chia-I Wu0f215c52016-10-11 11:48:21 +0800141}
142
Chia-I Wu158d5302016-10-04 06:00:12 +0800143template<typename T>
144void GrallocMapperHal::initDispatch(gralloc1_function_descriptor_t desc,
145 T* outPfn)
Craig Donner0b00adf2016-10-20 17:12:58 -0700146{
Chia-I Wu158d5302016-10-04 06:00:12 +0800147 auto pfn = mDevice->getFunction(mDevice, desc);
148 if (!pfn) {
149 LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc);
150 }
151
152 *outPfn = reinterpret_cast<T>(pfn);
Craig Donner0b00adf2016-10-20 17:12:58 -0700153}
154
Chia-I Wu158d5302016-10-04 06:00:12 +0800155void GrallocMapperHal::initDispatch()
Chia-I Wu0f215c52016-10-11 11:48:21 +0800156{
Chia-I Wu158d5302016-10-04 06:00:12 +0800157 initDispatch(GRALLOC1_FUNCTION_RETAIN, &mDispatch.retain);
158 initDispatch(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release);
159 initDispatch(GRALLOC1_FUNCTION_GET_DIMENSIONS, &mDispatch.getDimensions);
160 initDispatch(GRALLOC1_FUNCTION_GET_FORMAT, &mDispatch.getFormat);
161 if (mCapabilities.layeredBuffers) {
162 initDispatch(GRALLOC1_FUNCTION_GET_LAYER_COUNT,
163 &mDispatch.getLayerCount);
164 }
165 initDispatch(GRALLOC1_FUNCTION_GET_PRODUCER_USAGE,
166 &mDispatch.getProducerUsage);
167 initDispatch(GRALLOC1_FUNCTION_GET_CONSUMER_USAGE,
168 &mDispatch.getConsumerUsage);
169 initDispatch(GRALLOC1_FUNCTION_GET_BACKING_STORE,
170 &mDispatch.getBackingStore);
171 initDispatch(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride);
172 initDispatch(GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES,
173 &mDispatch.getNumFlexPlanes);
174 initDispatch(GRALLOC1_FUNCTION_LOCK, &mDispatch.lock);
175 initDispatch(GRALLOC1_FUNCTION_LOCK_FLEX, &mDispatch.lockFlex);
176 initDispatch(GRALLOC1_FUNCTION_UNLOCK, &mDispatch.unlock);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800177}
178
Chia-I Wu158d5302016-10-04 06:00:12 +0800179gralloc1_rect_t GrallocMapperHal::asGralloc1Rect(const IMapper::Rect& rect)
Chia-I Wu0f215c52016-10-11 11:48:21 +0800180{
Chia-I Wu158d5302016-10-04 06:00:12 +0800181 return gralloc1_rect_t{rect.left, rect.top, rect.width, rect.height};
Chia-I Wu0f215c52016-10-11 11:48:21 +0800182}
183
Chia-I Wu158d5302016-10-04 06:00:12 +0800184bool GrallocMapperHal::dupFence(const hidl_handle& fenceHandle, int* outFd)
Chia-I Wu0f215c52016-10-11 11:48:21 +0800185{
Chia-I Wu158d5302016-10-04 06:00:12 +0800186 auto handle = fenceHandle.getNativeHandle();
187 if (!handle || handle->numFds == 0) {
188 *outFd = -1;
189 return true;
190 }
191
192 if (handle->numFds > 1) {
193 ALOGE("invalid fence handle with %d fds", handle->numFds);
194 return false;
195 }
196
197 *outFd = dup(handle->data[0]);
198 return (*outFd >= 0);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800199}
200
Chia-I Wu158d5302016-10-04 06:00:12 +0800201Return<Error> GrallocMapperHal::retain(const hidl_handle& bufferHandle)
Chia-I Wu0f215c52016-10-11 11:48:21 +0800202{
Chia-I Wu158d5302016-10-04 06:00:12 +0800203 int32_t err = mDispatch.retain(mDevice, bufferHandle);
204 return static_cast<Error>(err);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800205}
206
Chia-I Wu158d5302016-10-04 06:00:12 +0800207Return<Error> GrallocMapperHal::release(const hidl_handle& bufferHandle)
Craig Donner0b00adf2016-10-20 17:12:58 -0700208{
Chia-I Wu158d5302016-10-04 06:00:12 +0800209 int32_t err = mDispatch.release(mDevice, bufferHandle);
210 return static_cast<Error>(err);
211}
212
213Return<void> GrallocMapperHal::getDimensions(const hidl_handle& bufferHandle,
214 getDimensions_cb hidl_cb)
215{
216 uint32_t width = 0;
217 uint32_t height = 0;
218 int32_t err = mDispatch.getDimensions(mDevice, bufferHandle,
219 &width, &height);
220
221 hidl_cb(static_cast<Error>(err), width, height);
222 return Void();
223}
224
225Return<void> GrallocMapperHal::getFormat(const hidl_handle& bufferHandle,
226 getFormat_cb hidl_cb)
227{
228 int32_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
229 int32_t err = mDispatch.getFormat(mDevice, bufferHandle, &format);
230
231 hidl_cb(static_cast<Error>(err), static_cast<PixelFormat>(format));
232 return Void();
233}
234
235Return<void> GrallocMapperHal::getLayerCount(const hidl_handle& bufferHandle,
236 getLayerCount_cb hidl_cb)
237{
238 int32_t err = GRALLOC1_ERROR_NONE;
239 uint32_t count = 1;
240 if (mCapabilities.layeredBuffers) {
241 err = mDispatch.getLayerCount(mDevice, bufferHandle, &count);
242 }
243
244 hidl_cb(static_cast<Error>(err), count);
245 return Void();
246}
247
248Return<void> GrallocMapperHal::getProducerUsageMask(
249 const hidl_handle& bufferHandle, getProducerUsageMask_cb hidl_cb)
250{
251 uint64_t mask = 0x0;
252 int32_t err = mDispatch.getProducerUsage(mDevice, bufferHandle, &mask);
253
254 hidl_cb(static_cast<Error>(err), mask);
255 return Void();
256}
257
258Return<void> GrallocMapperHal::getConsumerUsageMask(
259 const hidl_handle& bufferHandle, getConsumerUsageMask_cb hidl_cb)
260{
261 uint64_t mask = 0x0;
262 int32_t err = mDispatch.getConsumerUsage(mDevice, bufferHandle, &mask);
263
264 hidl_cb(static_cast<Error>(err), mask);
265 return Void();
266}
267
268Return<void> GrallocMapperHal::getBackingStore(
269 const hidl_handle& bufferHandle, getBackingStore_cb hidl_cb)
270{
271 uint64_t store = 0;
272 int32_t err = mDispatch.getBackingStore(mDevice, bufferHandle, &store);
273
274 hidl_cb(static_cast<Error>(err), store);
275 return Void();
276}
277
278Return<void> GrallocMapperHal::getStride(const hidl_handle& bufferHandle,
279 getStride_cb hidl_cb)
280{
281 uint32_t stride = 0;
282 int32_t err = mDispatch.getStride(mDevice, bufferHandle, &stride);
283
284 hidl_cb(static_cast<Error>(err), stride);
285 return Void();
286}
287
288Return<void> GrallocMapperHal::lock(const hidl_handle& bufferHandle,
289 uint64_t producerUsageMask, uint64_t consumerUsageMask,
290 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
291 lock_cb hidl_cb)
292{
293 gralloc1_rect_t rect = asGralloc1Rect(accessRegion);
294
295 int fence = -1;
296 if (!dupFence(acquireFence, &fence)) {
297 hidl_cb(Error::NO_RESOURCES, nullptr);
298 return Void();
299 }
300
301 void* data = nullptr;
302 int32_t err = mDispatch.lock(mDevice, bufferHandle, producerUsageMask,
303 consumerUsageMask, &rect, &data, fence);
304 if (err != GRALLOC1_ERROR_NONE) {
305 close(fence);
306 }
307
308 hidl_cb(static_cast<Error>(err), data);
309 return Void();
310}
311
312Return<void> GrallocMapperHal::lockFlex(const hidl_handle& bufferHandle,
313 uint64_t producerUsageMask, uint64_t consumerUsageMask,
314 const IMapper::Rect& accessRegion, const hidl_handle& acquireFence,
315 lockFlex_cb hidl_cb)
316{
317 FlexLayout layout_reply{};
318
319 uint32_t planeCount = 0;
320 int32_t err = mDispatch.getNumFlexPlanes(mDevice, bufferHandle,
321 &planeCount);
322 if (err != GRALLOC1_ERROR_NONE) {
323 hidl_cb(static_cast<Error>(err), layout_reply);
324 return Void();
325 }
326
327 gralloc1_rect_t rect = asGralloc1Rect(accessRegion);
328
329 int fence = -1;
330 if (!dupFence(acquireFence, &fence)) {
331 hidl_cb(Error::NO_RESOURCES, layout_reply);
332 return Void();
333 }
334
335 std::vector<android_flex_plane_t> planes(planeCount);
336 android_flex_layout_t layout{};
337 layout.num_planes = planes.size();
338 layout.planes = planes.data();
339
340 err = mDispatch.lockFlex(mDevice, bufferHandle, producerUsageMask,
341 consumerUsageMask, &rect, &layout, fence);
342 if (err == GRALLOC1_ERROR_NONE) {
343 layout_reply.format = static_cast<FlexFormat>(layout.format);
344
345 planes.resize(layout.num_planes);
346 layout_reply.planes.setToExternal(
347 reinterpret_cast<FlexPlane*>(planes.data()), planes.size());
Craig Donner0b00adf2016-10-20 17:12:58 -0700348 } else {
Chia-I Wu158d5302016-10-04 06:00:12 +0800349 close(fence);
Craig Donner0b00adf2016-10-20 17:12:58 -0700350 }
Chia-I Wu158d5302016-10-04 06:00:12 +0800351
352 hidl_cb(static_cast<Error>(err), layout_reply);
353 return Void();
Craig Donner0b00adf2016-10-20 17:12:58 -0700354}
355
Chia-I Wu158d5302016-10-04 06:00:12 +0800356Return<void> GrallocMapperHal::unlock(const hidl_handle& bufferHandle,
357 unlock_cb hidl_cb)
Chia-I Wu0f215c52016-10-11 11:48:21 +0800358{
Chia-I Wu158d5302016-10-04 06:00:12 +0800359 int32_t fence = -1;
360 int32_t err = mDispatch.unlock(mDevice, bufferHandle, &fence);
361
362 NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0);
363 hidl_handle fenceHandle;
364 if (err == GRALLOC1_ERROR_NONE && fence >= 0) {
365 auto nativeHandle = native_handle_init(fenceStorage, 1, 0);
366 nativeHandle->data[0] = fence;
367
368 fenceHandle = nativeHandle;
369 }
370
371 hidl_cb(static_cast<Error>(err), fenceHandle);
372 return Void();
Chia-I Wu0f215c52016-10-11 11:48:21 +0800373}
374
Chia-I Wu158d5302016-10-04 06:00:12 +0800375} // anonymous namespace
Chia-I Wu0f215c52016-10-11 11:48:21 +0800376
Chia-I Wu158d5302016-10-04 06:00:12 +0800377IMapper* HIDL_FETCH_IMapper(const char* /* name */) {
378 const hw_module_t* module = nullptr;
379 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
380 if (err) {
381 ALOGE("failed to get gralloc module");
382 return nullptr;
Chia-I Wu0f215c52016-10-11 11:48:21 +0800383 }
384
Chia-I Wu158d5302016-10-04 06:00:12 +0800385 uint8_t major = (module->module_api_version >> 8) & 0xff;
386 if (major != 1) {
387 ALOGE("unknown gralloc module major version %d", major);
388 return nullptr;
Chia-I Wu0f215c52016-10-11 11:48:21 +0800389 }
390
Chia-I Wu158d5302016-10-04 06:00:12 +0800391 return new GrallocMapperHal(module);
Chia-I Wu0f215c52016-10-11 11:48:21 +0800392}
393
394} // namespace implementation
395} // namespace V2_0
396} // namespace mapper
397} // namespace graphics
398} // namespace hardware
399} // namespace android