1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H
18 #define ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H
19 
20 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
21 #include <system/window.h>
22 
23 #include <mutex>
24 #include <unordered_set>
25 
26 namespace android {
27 namespace hardware {
28 namespace graphics {
29 namespace mapper {
30 namespace V2_0 {
31 namespace implementation {
32 
33 class GrallocMapper : public IMapper {
34    public:
35     // IMapper interface
36     Return<void> createDescriptor(const BufferDescriptorInfo& descriptorInfo,
37                                   createDescriptor_cb hidl_cb) override;
38     Return<void> importBuffer(const hidl_handle& rawHandle,
39                               importBuffer_cb hidl_cb) override;
40     Return<Error> freeBuffer(void* buffer) override;
41     Return<void> lock(void* buffer, uint64_t cpuUsage,
42                       const IMapper::Rect& accessRegion,
43                       const hidl_handle& acquireFence,
44                       lock_cb hidl_cb) override;
45     Return<void> lockYCbCr(void* buffer, uint64_t cpuUsage,
46                            const IMapper::Rect& accessRegion,
47                            const hidl_handle& acquireFence,
48                            lockYCbCr_cb hidl_cb) override;
49     Return<void> unlock(void* buffer, unlock_cb hidl_cb) override;
50 
51    protected:
52     static void waitFenceFd(int fenceFd, const char* logname);
53 
54     struct {
55         bool highUsageBits;
56         bool layeredBuffers;
57         bool unregisterImplyDelete;
58     } mCapabilities = {};
59 
60    private:
61     virtual bool validateDescriptorInfo(
62         const BufferDescriptorInfo& descriptorInfo) const;
63 
64     // Register a buffer.  The handle is already cloned by the caller.
65     virtual Error registerBuffer(buffer_handle_t bufferHandle) = 0;
66 
67     // Unregister a buffer.  The handle is closed and deleted by the
68     // callee if and only if mCapabilities.unregisterImplyDelete is set.
69     virtual void unregisterBuffer(buffer_handle_t bufferHandle) = 0;
70 
71     // Lock a buffer.  The fence is owned by the caller.
72     virtual Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage,
73                              const IMapper::Rect& accessRegion, int fenceFd,
74                              void** outData) = 0;
75     virtual Error lockBuffer(buffer_handle_t bufferHandle, uint64_t cpuUsage,
76                              const IMapper::Rect& accessRegion, int fenceFd,
77                              YCbCrLayout* outLayout) = 0;
78 
79     // Unlock a buffer.  The returned fence is owned by the caller.
80     virtual Error unlockBuffer(buffer_handle_t bufferHandle,
81                                int* outFenceFd) = 0;
82 
83     static bool getFenceFd(const hidl_handle& fenceHandle, int* outFenceFd);
84     static hidl_handle getFenceHandle(int fenceFd, char* handleStorage);
85 };
86 
87 extern "C" IMapper* HIDL_FETCH_IMapper(const char* name);
88 
89 } // namespace implementation
90 } // namespace V2_0
91 } // namespace mapper
92 } // namespace graphics
93 } // namespace hardware
94 } // namespace android
95 
96 #endif // ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_GRALLOC_MAPPER_H
97