blob: cf8c173eee8c3d1941e651a3eb9a6eb877d42512 [file] [log] [blame]
Dan Stoza41b12612016-05-20 12:14:37 -07001/*
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_UI_GRALLOC1_H
18#define ANDROID_UI_GRALLOC1_H
19
20#define GRALLOC1_LOG_TAG "Gralloc1"
21
22#include <ui/Gralloc1On0Adapter.h>
23
24#include <unordered_set>
25
26namespace std {
27 template <>
28 struct hash<gralloc1_capability_t> {
29 size_t operator()(gralloc1_capability_t capability) const {
30 return std::hash<int32_t>()(static_cast<int32_t>(capability));
31 }
32 };
33}
34
35namespace android {
36
37class Fence;
38class GraphicBuffer;
39
40namespace Gralloc1 {
41
42class Device;
43
44class Descriptor {
45public:
46 Descriptor(Device& device, gralloc1_buffer_descriptor_t deviceId)
47 : mShimDevice(device),
48 mDeviceId(deviceId),
49 mWidth(0),
50 mHeight(0),
51 mFormat(static_cast<android_pixel_format_t>(0)),
52 mProducerUsage(GRALLOC1_PRODUCER_USAGE_NONE),
53 mConsumerUsage(GRALLOC1_CONSUMER_USAGE_NONE) {}
54
55 ~Descriptor();
56
57 gralloc1_buffer_descriptor_t getDeviceId() const { return mDeviceId; }
58
59 gralloc1_error_t setDimensions(uint32_t width, uint32_t height);
60 gralloc1_error_t setFormat(android_pixel_format_t format);
61 gralloc1_error_t setProducerUsage(gralloc1_producer_usage_t usage);
62 gralloc1_error_t setConsumerUsage(gralloc1_consumer_usage_t usage);
63
64private:
65 Device& mShimDevice;
66 const gralloc1_buffer_descriptor_t mDeviceId;
67
68 uint32_t mWidth;
69 uint32_t mHeight;
70 android_pixel_format_t mFormat;
71 gralloc1_producer_usage_t mProducerUsage;
72 gralloc1_consumer_usage_t mConsumerUsage;
73
74}; // Descriptor
75
76class Device {
77 friend class Gralloc1::Descriptor;
78
79public:
80 Device(gralloc1_device_t* device);
81
82 bool hasCapability(gralloc1_capability_t capability) const;
83
84 std::string dump();
85
86 std::shared_ptr<Descriptor> createDescriptor();
87
88 gralloc1_error_t getStride(buffer_handle_t buffer, uint32_t* outStride);
89
90 gralloc1_error_t allocate(
91 const std::vector<std::shared_ptr<const Descriptor>>& descriptors,
92 std::vector<buffer_handle_t>* outBuffers);
93 gralloc1_error_t allocate(
94 const std::shared_ptr<const Descriptor>& descriptor,
95 gralloc1_backing_store_t id, buffer_handle_t* outBuffer);
96
97 gralloc1_error_t retain(buffer_handle_t buffer);
98 gralloc1_error_t retain(const GraphicBuffer* buffer);
99
100 gralloc1_error_t release(buffer_handle_t buffer);
101
102 gralloc1_error_t getNumFlexPlanes(buffer_handle_t buffer,
103 uint32_t* outNumPlanes);
104
105 gralloc1_error_t lock(buffer_handle_t buffer,
106 gralloc1_producer_usage_t producerUsage,
107 gralloc1_consumer_usage_t consumerUsage,
108 const gralloc1_rect_t* accessRegion, void** outData,
109 const sp<Fence>& acquireFence);
110 gralloc1_error_t lockFlex(buffer_handle_t buffer,
111 gralloc1_producer_usage_t producerUsage,
112 gralloc1_consumer_usage_t consumerUsage,
113 const gralloc1_rect_t* accessRegion,
114 struct android_flex_layout* outData, const sp<Fence>& acquireFence);
115 gralloc1_error_t lockYCbCr(buffer_handle_t buffer,
116 gralloc1_producer_usage_t producerUsage,
117 gralloc1_consumer_usage_t consumerUsage,
118 const gralloc1_rect_t* accessRegion, struct android_ycbcr* outData,
119 const sp<Fence>& acquireFence);
120
121 gralloc1_error_t unlock(buffer_handle_t buffer, sp<Fence>* outFence);
122
123private:
124 std::unordered_set<gralloc1_capability_t> loadCapabilities();
125
126 bool loadFunctions();
127
128 template <typename LockType, typename OutType>
129 gralloc1_error_t lockHelper(LockType pfn, buffer_handle_t buffer,
130 gralloc1_producer_usage_t producerUsage,
131 gralloc1_consumer_usage_t consumerUsage,
132 const gralloc1_rect_t* accessRegion, OutType* outData,
133 const sp<Fence>& acquireFence) {
134 int32_t intError = pfn(mDevice, buffer,
135 static_cast<uint64_t>(producerUsage),
136 static_cast<uint64_t>(consumerUsage), accessRegion, outData,
137 acquireFence->dup());
138 return static_cast<gralloc1_error_t>(intError);
139 }
140
141 gralloc1_device_t* const mDevice;
142
143 const std::unordered_set<gralloc1_capability_t> mCapabilities;
144
145 template <typename PFN, gralloc1_function_descriptor_t descriptor>
146 struct FunctionLoader {
147 FunctionLoader() : pfn(nullptr) {}
148
149 bool load(gralloc1_device_t* device, bool errorIfNull) {
150 gralloc1_function_pointer_t rawPointer =
151 device->getFunction(device, descriptor);
152 pfn = reinterpret_cast<PFN>(rawPointer);
153 if (errorIfNull && !rawPointer) {
154 ALOG(LOG_ERROR, GRALLOC1_LOG_TAG,
155 "Failed to load function pointer %d", descriptor);
156 }
157 return rawPointer != nullptr;
158 }
159
160 template <typename ...Args>
161 typename std::result_of<PFN(Args...)>::type operator()(Args... args) {
162 return pfn(args...);
163 }
164
165 PFN pfn;
166 };
167
168 // Function pointers
169 struct Functions {
170 FunctionLoader<GRALLOC1_PFN_DUMP, GRALLOC1_FUNCTION_DUMP> dump;
171 FunctionLoader<GRALLOC1_PFN_CREATE_DESCRIPTOR,
172 GRALLOC1_FUNCTION_CREATE_DESCRIPTOR> createDescriptor;
173 FunctionLoader<GRALLOC1_PFN_DESTROY_DESCRIPTOR,
174 GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR> destroyDescriptor;
175 FunctionLoader<GRALLOC1_PFN_SET_CONSUMER_USAGE,
176 GRALLOC1_FUNCTION_SET_CONSUMER_USAGE> setConsumerUsage;
177 FunctionLoader<GRALLOC1_PFN_SET_DIMENSIONS,
178 GRALLOC1_FUNCTION_SET_DIMENSIONS> setDimensions;
179 FunctionLoader<GRALLOC1_PFN_SET_FORMAT,
180 GRALLOC1_FUNCTION_SET_FORMAT> setFormat;
181 FunctionLoader<GRALLOC1_PFN_SET_PRODUCER_USAGE,
182 GRALLOC1_FUNCTION_SET_PRODUCER_USAGE> setProducerUsage;
183 FunctionLoader<GRALLOC1_PFN_GET_BACKING_STORE,
184 GRALLOC1_FUNCTION_GET_BACKING_STORE> getBackingStore;
185 FunctionLoader<GRALLOC1_PFN_GET_CONSUMER_USAGE,
186 GRALLOC1_FUNCTION_GET_CONSUMER_USAGE> getConsumerUsage;
187 FunctionLoader<GRALLOC1_PFN_GET_DIMENSIONS,
188 GRALLOC1_FUNCTION_GET_DIMENSIONS> getDimensions;
189 FunctionLoader<GRALLOC1_PFN_GET_FORMAT,
190 GRALLOC1_FUNCTION_GET_FORMAT> getFormat;
191 FunctionLoader<GRALLOC1_PFN_GET_PRODUCER_USAGE,
192 GRALLOC1_FUNCTION_GET_PRODUCER_USAGE> getProducerUsage;
193 FunctionLoader<GRALLOC1_PFN_GET_STRIDE,
194 GRALLOC1_FUNCTION_GET_STRIDE> getStride;
195 FunctionLoader<GRALLOC1_PFN_ALLOCATE,
196 GRALLOC1_FUNCTION_ALLOCATE> allocate;
197 FunctionLoader<GRALLOC1_PFN_RETAIN,
198 GRALLOC1_FUNCTION_RETAIN> retain;
199 FunctionLoader<GRALLOC1_PFN_RELEASE,
200 GRALLOC1_FUNCTION_RELEASE> release;
201 FunctionLoader<GRALLOC1_PFN_GET_NUM_FLEX_PLANES,
202 GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES> getNumFlexPlanes;
203 FunctionLoader<GRALLOC1_PFN_LOCK,
204 GRALLOC1_FUNCTION_LOCK> lock;
205 FunctionLoader<GRALLOC1_PFN_LOCK_FLEX,
206 GRALLOC1_FUNCTION_LOCK_FLEX> lockFlex;
207 FunctionLoader<GRALLOC1_PFN_LOCK_YCBCR,
208 GRALLOC1_FUNCTION_LOCK_YCBCR> lockYCbCr;
209 FunctionLoader<GRALLOC1_PFN_UNLOCK,
210 GRALLOC1_FUNCTION_UNLOCK> unlock;
211
212 // Adapter-only functions
213 FunctionLoader<GRALLOC1_PFN_RETAIN_GRAPHIC_BUFFER,
214 GRALLOC1_FUNCTION_RETAIN_GRAPHIC_BUFFER> retainGraphicBuffer;
215 FunctionLoader<GRALLOC1_PFN_ALLOCATE_WITH_ID,
216 GRALLOC1_FUNCTION_ALLOCATE_WITH_ID> allocateWithId;
217 } mFunctions;
218
219}; // class android::Gralloc1::Device
220
221class Loader
222{
223public:
224 Loader();
225 ~Loader();
226
227 std::unique_ptr<Device> getDevice();
228
229private:
230 static std::unique_ptr<Gralloc1On0Adapter> mAdapter;
231 std::unique_ptr<Device> mDevice;
232};
233
234} // namespace android::Gralloc1
235
236} // namespace android
237
238#endif