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_SLSI_GRALLOC2WRAPPER_H
18 #define ANDROID_SLSI_GRALLOC2WRAPPER_H
19 
20 #include <string>
21 
22 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
23 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
24 #include <utils/StrongPointer.h>
25 
26 namespace android {
27 
28 namespace GrallocWrapper {
29 
30 using hardware::graphics::allocator::V2_0::IAllocator;
31 using hardware::graphics::common::V1_0::BufferUsage;
32 using hardware::graphics::common::V1_0::PixelFormat;
33 using hardware::graphics::mapper::V2_0::BufferDescriptor;
34 using hardware::graphics::mapper::V2_0::Error;
35 using hardware::graphics::mapper::V2_0::IMapper;
36 using hardware::graphics::mapper::V2_0::YCbCrLayout;
37 
38 // A wrapper to IMapper
39 class Mapper {
40 public:
41     Mapper();
42 
43     Error createDescriptor(
44             const IMapper::BufferDescriptorInfo& descriptorInfo,
45             BufferDescriptor* outDescriptor) const;
46 
47     // Import a buffer that is from another HAL, another process, or is
48     // cloned.
49     //
50     // The returned handle must be freed with freeBuffer.
51     Error importBuffer(const hardware::hidl_handle& rawHandle,
52             buffer_handle_t* outBufferHandle) const;
53 
54     void freeBuffer(buffer_handle_t bufferHandle) const;
55 
56     // The ownership of acquireFence is always transferred to the callee, even
57     // on errors.
58     Error lock(buffer_handle_t bufferHandle, uint64_t usage,
59             const IMapper::Rect& accessRegion,
60             int acquireFence, void** outData) const;
61 
62     // The ownership of acquireFence is always transferred to the callee, even
63     // on errors.
64     Error lock(buffer_handle_t bufferHandle, uint64_t usage,
65             const IMapper::Rect& accessRegion,
66             int acquireFence, YCbCrLayout* outLayout) const;
67 
68     // unlock returns a fence sync object (or -1) and the fence sync object is
69     // owned by the caller
70     int unlock(buffer_handle_t bufferHandle) const;
71 
72 private:
73     sp<IMapper> mMapper;
74 };
75 
76 // A wrapper to IAllocator
77 class Allocator {
78 public:
79     // An allocator relies on a mapper, and that mapper must be alive at all
80     // time.
81     Allocator(const Mapper& mapper);
82 
83     std::string dumpDebugInfo() const;
84 
85     /*
86      * The returned buffers are already imported and must not be imported
87      * again.  outBufferHandles must point to a space that can contain at
88      * least "count" buffer_handle_t.
89      */
90     Error allocate(BufferDescriptor descriptor, uint32_t count,
91             uint32_t* outStride, buffer_handle_t* outBufferHandles) const;
92 
allocate(BufferDescriptor descriptor,uint32_t * outStride,buffer_handle_t * outBufferHandle)93     Error allocate(BufferDescriptor descriptor,
94             uint32_t* outStride, buffer_handle_t* outBufferHandle) const
95     {
96         return allocate(descriptor, 1, outStride, outBufferHandle);
97     }
98 
allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t count,uint32_t * outStride,buffer_handle_t * outBufferHandles)99     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count,
100             uint32_t* outStride, buffer_handle_t* outBufferHandles) const
101     {
102         BufferDescriptor descriptor;
103         Error error = mMapper.createDescriptor(descriptorInfo, &descriptor);
104         if (error == Error::NONE) {
105             error = allocate(descriptor, count, outStride, outBufferHandles);
106         }
107         return error;
108     }
109 
allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t * outStride,buffer_handle_t * outBufferHandle)110     Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
111             uint32_t* outStride, buffer_handle_t* outBufferHandle) const
112     {
113         return allocate(descriptorInfo, 1, outStride, outBufferHandle);
114     }
115 
116 private:
117     const Mapper& mMapper;
118     sp<IAllocator> mAllocator;
119 };
120 
121 } // namespace GrallocWrapper
122 
123 } // namespace android
124 
125 #endif
126