1 /*
2  * Copyright (C) 2018 ARM Limited. All rights reserved.
3  *
4  * Copyright 2016 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_x_GRALLOC_MAPPER_H
19 #define ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_x_GRALLOC_MAPPER_H
20 
21 #if HIDL_MAPPER_VERSION_SCALED == 200
22 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
23 #elif HIDL_MAPPER_VERSION_SCALED == 210
24 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
25 #endif
26 
27 #include "mali_gralloc_module.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace graphics {
32 namespace mapper {
33 namespace HIDL_IMAPPER_NAMESPACE {
34 namespace implementation {
35 
36 using V2_0::Error;
37 using V2_0::BufferDescriptor;
38 using V2_0::YCbCrLayout;
39 
40 class GrallocMapper : public IMapper
41 {
42 public:
43 	GrallocMapper();
44 	~GrallocMapper();
45 
46 	/* Override the public IMapper 2.0 interface */
47 
48 	/* Creates a buffer descriptor */
49 	Return<void> createDescriptor(const V2_0::IMapper::BufferDescriptorInfo& descriptorInfo,
50 	                              createDescriptor_cb hidl_cb) override;
51 
52 	/* Imports a raw buffer handle to create an imported buffer handle */
53 	Return<void> importBuffer(const hidl_handle& rawHandle,
54 	                          importBuffer_cb hidl_cb) override;
55 
56 	/* Frees a buffer handle */
57 	Return<Error> freeBuffer(void* buffer) override;
58 
59 	/* Locks the given buffer for the specified CPU usage */
60 	Return<void> lock(void* buffer, uint64_t cpuUsage,
61 	                  const IMapper::Rect& accessRegion,
62 	                  const hidl_handle& acquireFence,
63 	                  lock_cb hidl_cb) override;
64 
65 	/*
66 	 * Locks the given buffer for the specified CPU usage and exports CPU
67 	 * accessible data in YCbCr structure
68 	 */
69 	Return<void> lockYCbCr(void* buffer, uint64_t cpuUsage,
70 	                       const IMapper::Rect& accessRegion,
71 	                       const hidl_handle& acquireFence,
72 	                       lockYCbCr_cb hidl_cb) override;
73 
74 	/* Unlocks a buffer to indicate all CPU accesses to the buffer have completed */
75 	Return<void> unlock(void* buffer, unlock_cb hidl_cb) override;
76 
77 #if HIDL_MAPPER_VERSION_SCALED >= 210
78 	/* Override the public IMapper 2.1 specific interface */
79 
80 	/* Validates the buffer against specified descriptor attributes */
81 	Return<Error> validateBufferSize(void* buffer,
82 	                                 const V2_1::IMapper::BufferDescriptorInfo& descriptorInfo,
83 	                                 uint32_t stride) override;
84 
85 	/* Get the transport size of a buffer */
86 	Return<void> getTransportSize(void* buffer, getTransportSize_cb _hidl_cb) override;
87 
88 	/* Creates a descriptor using IMapper 2.1 version of descriptor attributes */
89 	Return<void> createDescriptor_2_1(const V2_1::IMapper::BufferDescriptorInfo& descriptorInfo,
90 	                                  createDescriptor_2_1_cb _hidl_cb) override;
91 #endif
92 
93 private:
94 	/* Validates incoming IMapper descriptor attributes */
95 	bool validateDescriptorInfo(void *descriptor_attr) const;
96 
97 	/* Register a buffer.  The handle is already cloned by the caller */
98 	Error registerBuffer(buffer_handle_t bufferHandle) const;
99 
100 	/* Unmap a buffer.  The handle is already cloned by the caller */
101 	Error unregisterBuffer(buffer_handle_t bufferHandle) const;
102 
103 	/* Lock a buffer.  The fence is owned by the caller */
104 	Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage,
105 	                 const IMapper::Rect& accessRegion, int fenceFd,
106 	                 void** outData) const;
107 
108 	/* Lock a buffer, with YCbCr data exported.  The fence is owned by the caller */
109 	Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage,
110 	                 const IMapper::Rect& accessRegion, int fenceFd,
111 	                 YCbCrLayout* outLayout) const;
112 
113 	/* Unlock a buffer.  The returned fence is owned by the caller */
114 	Error unlockBuffer(buffer_handle_t bufferHandle,
115 	                   int* outFenceFd) const;
116 
117 	/* Retrieves the file descriptor referring to a sync fence object */
118 	bool getFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) const;
119 
120 	/* Populates the HIDL fence handle for the given fence object */
121 	hidl_handle getFenceHandle(int fenceFd, char* handleStorage) const;
122 
123 	/* To hold state information for the instance of mapper */
124 	struct private_module_t privateModule;
125 };
126 
127 extern "C" IMapper* HIDL_FETCH_IMapper(const char* name);
128 
129 } // namespace implementation
130 } // namespace HIDL_IMAPPER_NAMESPACE
131 } // namespace mapper
132 } // namespace graphics
133 } // namespace hardware
134 } // namespace android
135 
136 #endif // ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_x_GRALLOC_MAPPER_H
137