blob: eeca5c73ee8e571a1519bce3214ac30bf087fd44 [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
18#include <android/hardware/graphics/mapper/2.0/IMapper.h>
19#include <hardware/gralloc1.h>
20#include <log/log.h>
21
22namespace android {
23namespace hardware {
24namespace graphics {
25namespace mapper {
26namespace V2_0 {
27namespace implementation {
28
29class GrallocDevice : public Device {
30public:
31 GrallocDevice();
32 ~GrallocDevice();
33
34 // IMapper interface
35 Error retain(const native_handle_t* bufferHandle);
36 Error release(const native_handle_t* bufferHandle);
37 Error getDimensions(const native_handle_t* bufferHandle,
38 uint32_t* outWidth, uint32_t* outHeight);
39 Error getFormat(const native_handle_t* bufferHandle,
40 PixelFormat* outFormat);
41 Error getProducerUsageMask(const native_handle_t* bufferHandle,
42 uint64_t* outUsageMask);
43 Error getConsumerUsageMask(const native_handle_t* bufferHandle,
44 uint64_t* outUsageMask);
45 Error getBackingStore(const native_handle_t* bufferHandle,
46 BackingStore* outStore);
47 Error getStride(const native_handle_t* bufferHandle, uint32_t* outStride);
48 Error getNumFlexPlanes(const native_handle_t* bufferHandle,
49 uint32_t* outNumPlanes);
50 Error lock(const native_handle_t* bufferHandle,
51 uint64_t producerUsageMask, uint64_t consumerUsageMask,
52 const Rect* accessRegion, int32_t acquireFence, void** outData);
53 Error lockFlex(const native_handle_t* bufferHandle,
54 uint64_t producerUsageMask, uint64_t consumerUsageMask,
55 const Rect* accessRegion, int32_t acquireFence,
56 FlexLayout* outFlexLayout);
57 Error unlock(const native_handle_t* bufferHandle,
58 int32_t* outReleaseFence);
59
60private:
61 void initDispatch();
62
63 gralloc1_device_t* mDevice;
64
65 struct {
66 GRALLOC1_PFN_RETAIN retain;
67 GRALLOC1_PFN_RELEASE release;
68 GRALLOC1_PFN_GET_DIMENSIONS getDimensions;
69 GRALLOC1_PFN_GET_FORMAT getFormat;
70 GRALLOC1_PFN_GET_PRODUCER_USAGE getProducerUsage;
71 GRALLOC1_PFN_GET_CONSUMER_USAGE getConsumerUsage;
72 GRALLOC1_PFN_GET_BACKING_STORE getBackingStore;
73 GRALLOC1_PFN_GET_STRIDE getStride;
74 GRALLOC1_PFN_GET_NUM_FLEX_PLANES getNumFlexPlanes;
75 GRALLOC1_PFN_LOCK lock;
76 GRALLOC1_PFN_LOCK_FLEX lockFlex;
77 GRALLOC1_PFN_UNLOCK unlock;
78 } mDispatch;
79};
80
81GrallocDevice::GrallocDevice()
82{
83 const hw_module_t* module;
84 int status = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
85 if (status) {
86 LOG_ALWAYS_FATAL("failed to get gralloc module");
87 }
88
89 uint8_t major = (module->module_api_version >> 8) & 0xff;
90 if (major != 1) {
91 LOG_ALWAYS_FATAL("unknown gralloc module major version %d", major);
92 }
93
94 status = gralloc1_open(module, &mDevice);
95 if (status) {
96 LOG_ALWAYS_FATAL("failed to open gralloc1 device");
97 }
98
99 initDispatch();
100}
101
102GrallocDevice::~GrallocDevice()
103{
104 gralloc1_close(mDevice);
105}
106
107void GrallocDevice::initDispatch()
108{
109#define CHECK_FUNC(func, desc) do { \
110 mDispatch.func = reinterpret_cast<decltype(mDispatch.func)>( \
111 mDevice->getFunction(mDevice, desc)); \
112 if (!mDispatch.func) { \
113 LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc); \
114 } \
115} while (0)
116
117 CHECK_FUNC(retain, GRALLOC1_FUNCTION_RETAIN);
118 CHECK_FUNC(release, GRALLOC1_FUNCTION_RELEASE);
119 CHECK_FUNC(getDimensions, GRALLOC1_FUNCTION_GET_DIMENSIONS);
120 CHECK_FUNC(getFormat, GRALLOC1_FUNCTION_GET_FORMAT);
121 CHECK_FUNC(getProducerUsage, GRALLOC1_FUNCTION_GET_PRODUCER_USAGE);
122 CHECK_FUNC(getConsumerUsage, GRALLOC1_FUNCTION_GET_CONSUMER_USAGE);
123 CHECK_FUNC(getBackingStore, GRALLOC1_FUNCTION_GET_BACKING_STORE);
124 CHECK_FUNC(getStride, GRALLOC1_FUNCTION_GET_STRIDE);
125 CHECK_FUNC(getNumFlexPlanes, GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES);
126 CHECK_FUNC(lock, GRALLOC1_FUNCTION_LOCK);
127 CHECK_FUNC(lockFlex, GRALLOC1_FUNCTION_LOCK_FLEX);
128 CHECK_FUNC(unlock, GRALLOC1_FUNCTION_UNLOCK);
129
130#undef CHECK_FUNC
131}
132
133Error GrallocDevice::retain(const native_handle_t* bufferHandle)
134{
135 int32_t error = mDispatch.retain(mDevice, bufferHandle);
136 return static_cast<Error>(error);
137}
138
139Error GrallocDevice::release(const native_handle_t* bufferHandle)
140{
141 int32_t error = mDispatch.release(mDevice, bufferHandle);
142 return static_cast<Error>(error);
143}
144
145Error GrallocDevice::getDimensions(const native_handle_t* bufferHandle,
146 uint32_t* outWidth, uint32_t* outHeight)
147{
148 int32_t error = mDispatch.getDimensions(mDevice, bufferHandle,
149 outWidth, outHeight);
150 return static_cast<Error>(error);
151}
152
153Error GrallocDevice::getFormat(const native_handle_t* bufferHandle,
154 PixelFormat* outFormat)
155{
156 int32_t error = mDispatch.getFormat(mDevice, bufferHandle,
157 reinterpret_cast<int32_t*>(outFormat));
158 return static_cast<Error>(error);
159}
160
161Error GrallocDevice::getProducerUsageMask(const native_handle_t* bufferHandle,
162 uint64_t* outUsageMask)
163{
164 int32_t error = mDispatch.getProducerUsage(mDevice, bufferHandle,
165 outUsageMask);
166 return static_cast<Error>(error);
167}
168
169Error GrallocDevice::getConsumerUsageMask(const native_handle_t* bufferHandle,
170 uint64_t* outUsageMask)
171{
172 int32_t error = mDispatch.getConsumerUsage(mDevice, bufferHandle,
173 outUsageMask);
174 return static_cast<Error>(error);
175}
176
177Error GrallocDevice::getBackingStore(const native_handle_t* bufferHandle,
178 BackingStore* outStore)
179{
180 int32_t error = mDispatch.getBackingStore(mDevice, bufferHandle,
181 outStore);
182 return static_cast<Error>(error);
183}
184
185Error GrallocDevice::getStride(const native_handle_t* bufferHandle,
186 uint32_t* outStride)
187{
188 int32_t error = mDispatch.getStride(mDevice, bufferHandle, outStride);
189 return static_cast<Error>(error);
190}
191
192Error GrallocDevice::getNumFlexPlanes(const native_handle_t* bufferHandle,
193 uint32_t* outNumPlanes)
194{
195 int32_t error = mDispatch.getNumFlexPlanes(mDevice, bufferHandle,
196 outNumPlanes);
197 return static_cast<Error>(error);
198}
199
200Error GrallocDevice::lock(const native_handle_t* bufferHandle,
201 uint64_t producerUsageMask, uint64_t consumerUsageMask,
202 const Rect* accessRegion, int32_t acquireFence,
203 void** outData)
204{
205 int32_t error = mDispatch.lock(mDevice, bufferHandle,
206 producerUsageMask, consumerUsageMask,
207 reinterpret_cast<const gralloc1_rect_t*>(accessRegion),
208 outData, acquireFence);
209 return static_cast<Error>(error);
210}
211
212Error GrallocDevice::lockFlex(const native_handle_t* bufferHandle,
213 uint64_t producerUsageMask, uint64_t consumerUsageMask,
214 const Rect* accessRegion, int32_t acquireFence,
215 FlexLayout* outFlexLayout)
216{
217 int32_t error = mDispatch.lockFlex(mDevice, bufferHandle,
218 producerUsageMask, consumerUsageMask,
219 reinterpret_cast<const gralloc1_rect_t*>(accessRegion),
220 reinterpret_cast<android_flex_layout_t*>(outFlexLayout),
221 acquireFence);
222 return static_cast<Error>(error);
223}
224
225Error GrallocDevice::unlock(const native_handle_t* bufferHandle,
226 int32_t* outReleaseFence)
227{
228 int32_t error = mDispatch.unlock(mDevice, bufferHandle, outReleaseFence);
229 return static_cast<Error>(error);
230}
231
232class GrallocMapper : public IMapper {
233public:
234 GrallocMapper() : IMapper{
235 .createDevice = createDevice,
236 .destroyDevice = destroyDevice,
237 .retain = retain,
238 .release = release,
239 .getDimensions = getDimensions,
240 .getFormat = getFormat,
241 .getProducerUsageMask = getProducerUsageMask,
242 .getConsumerUsageMask = getConsumerUsageMask,
243 .getBackingStore = getBackingStore,
244 .getStride = getStride,
245 .getNumFlexPlanes = getNumFlexPlanes,
246 .lock = lock,
247 .lockFlex = lockFlex,
248 .unlock = unlock,
249 } {}
250
251 const IMapper* getInterface() const
252 {
253 return static_cast<const IMapper*>(this);
254 }
255
256private:
257 static GrallocDevice* cast(Device* device)
258 {
259 return reinterpret_cast<GrallocDevice*>(device);
260 }
261
262 static Error createDevice(Device** outDevice)
263 {
264 *outDevice = new GrallocDevice;
265 return Error::NONE;
266 }
267
268 static Error destroyDevice(Device* device)
269 {
270 delete cast(device);
271 return Error::NONE;
272 }
273
274 static Error retain(Device* device,
275 const native_handle_t* bufferHandle)
276 {
277 return cast(device)->retain(bufferHandle);
278 }
279
280 static Error release(Device* device,
281 const native_handle_t* bufferHandle)
282 {
283 return cast(device)->release(bufferHandle);
284 }
285
286 static Error getDimensions(Device* device,
287 const native_handle_t* bufferHandle,
288 uint32_t* outWidth, uint32_t* outHeight)
289 {
290 return cast(device)->getDimensions(bufferHandle, outWidth, outHeight);
291 }
292
293 static Error getFormat(Device* device,
294 const native_handle_t* bufferHandle, PixelFormat* outFormat)
295 {
296 return cast(device)->getFormat(bufferHandle, outFormat);
297 }
298
299 static Error getProducerUsageMask(Device* device,
300 const native_handle_t* bufferHandle, uint64_t* outUsageMask)
301 {
302 return cast(device)->getProducerUsageMask(bufferHandle, outUsageMask);
303 }
304
305 static Error getConsumerUsageMask(Device* device,
306 const native_handle_t* bufferHandle, uint64_t* outUsageMask)
307 {
308 return cast(device)->getConsumerUsageMask(bufferHandle, outUsageMask);
309 }
310
311 static Error getBackingStore(Device* device,
312 const native_handle_t* bufferHandle, BackingStore* outStore)
313 {
314 return cast(device)->getBackingStore(bufferHandle, outStore);
315 }
316
317 static Error getStride(Device* device,
318 const native_handle_t* bufferHandle, uint32_t* outStride)
319 {
320 return cast(device)->getStride(bufferHandle, outStride);
321 }
322
323 static Error getNumFlexPlanes(Device* device,
324 const native_handle_t* bufferHandle, uint32_t* outNumPlanes)
325 {
326 return cast(device)->getNumFlexPlanes(bufferHandle, outNumPlanes);
327 }
328
329 static Error lock(Device* device,
330 const native_handle_t* bufferHandle,
331 uint64_t producerUsageMask, uint64_t consumerUsageMask,
332 const Device::Rect* accessRegion, int32_t acquireFence,
333 void** outData)
334 {
335 return cast(device)->lock(bufferHandle,
336 producerUsageMask, consumerUsageMask,
337 accessRegion, acquireFence, outData);
338 }
339
340 static Error lockFlex(Device* device,
341 const native_handle_t* bufferHandle,
342 uint64_t producerUsageMask, uint64_t consumerUsageMask,
343 const Device::Rect* accessRegion, int32_t acquireFence,
344 FlexLayout* outFlexLayout)
345 {
346 return cast(device)->lockFlex(bufferHandle,
347 producerUsageMask, consumerUsageMask,
348 accessRegion, acquireFence, outFlexLayout);
349 }
350
351 static Error unlock(Device* device,
352 const native_handle_t* bufferHandle, int32_t* outReleaseFence)
353 {
354 return cast(device)->unlock(bufferHandle, outReleaseFence);
355 }
356};
357
358extern "C" const void* HALLIB_FETCH_Interface(const char* name)
359{
360 if (strcmp(name, "android.hardware.graphics.mapper@2.0::IMapper") == 0) {
361 static GrallocMapper sGrallocMapper;
362 return sGrallocMapper.getInterface();
363 }
364
365 return nullptr;
366}
367
368} // namespace implementation
369} // namespace V2_0
370} // namespace mapper
371} // namespace graphics
372} // namespace hardware
373} // namespace android