blob: 640e29c7205e7a230724b0fa9ce09066d6036a82 [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)),
Craig Donner6ebc46a2016-10-21 15:23:44 -070052 mLayerCount(0),
Dan Stoza41b12612016-05-20 12:14:37 -070053 mProducerUsage(GRALLOC1_PRODUCER_USAGE_NONE),
54 mConsumerUsage(GRALLOC1_CONSUMER_USAGE_NONE) {}
55
56 ~Descriptor();
57
58 gralloc1_buffer_descriptor_t getDeviceId() const { return mDeviceId; }
59
60 gralloc1_error_t setDimensions(uint32_t width, uint32_t height);
61 gralloc1_error_t setFormat(android_pixel_format_t format);
Craig Donner6ebc46a2016-10-21 15:23:44 -070062 gralloc1_error_t setLayerCount(uint32_t layerCount);
Dan Stoza41b12612016-05-20 12:14:37 -070063 gralloc1_error_t setProducerUsage(gralloc1_producer_usage_t usage);
64 gralloc1_error_t setConsumerUsage(gralloc1_consumer_usage_t usage);
65
66private:
67 Device& mShimDevice;
68 const gralloc1_buffer_descriptor_t mDeviceId;
69
70 uint32_t mWidth;
71 uint32_t mHeight;
72 android_pixel_format_t mFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -070073 uint32_t mLayerCount;
Dan Stoza41b12612016-05-20 12:14:37 -070074 gralloc1_producer_usage_t mProducerUsage;
75 gralloc1_consumer_usage_t mConsumerUsage;
76
77}; // Descriptor
78
79class Device {
80 friend class Gralloc1::Descriptor;
81
82public:
83 Device(gralloc1_device_t* device);
84
85 bool hasCapability(gralloc1_capability_t capability) const;
86
87 std::string dump();
88
89 std::shared_ptr<Descriptor> createDescriptor();
90
Dan Stoza41b12612016-05-20 12:14:37 -070091 gralloc1_error_t allocate(
92 const std::vector<std::shared_ptr<const Descriptor>>& descriptors,
93 std::vector<buffer_handle_t>* outBuffers);
94 gralloc1_error_t allocate(
95 const std::shared_ptr<const Descriptor>& descriptor,
96 gralloc1_backing_store_t id, buffer_handle_t* outBuffer);
97
98 gralloc1_error_t retain(buffer_handle_t buffer);
99 gralloc1_error_t retain(const GraphicBuffer* buffer);
100
101 gralloc1_error_t release(buffer_handle_t buffer);
102
Craig Donner58a1ef22017-02-02 12:40:05 -0800103 gralloc1_error_t getDimensions(buffer_handle_t buffer,
104 uint32_t* outWidth, uint32_t* outHeight);
105 gralloc1_error_t getFormat(buffer_handle_t buffer,
106 int32_t* outFormat);
107 gralloc1_error_t getLayerCount(buffer_handle_t buffer,
108 uint32_t* outLayerCount);
109 gralloc1_error_t getProducerUsage(buffer_handle_t buffer,
110 uint64_t* outProducerUsage);
111 gralloc1_error_t getConsumerUsage(buffer_handle_t buffer,
112 uint64_t* outConsumerUsage);
113 gralloc1_error_t getBackingStore(buffer_handle_t buffer,
114 uint64_t* outBackingStore);
115 gralloc1_error_t getStride(buffer_handle_t buffer, uint32_t* outStride);
Dan Stoza41b12612016-05-20 12:14:37 -0700116 gralloc1_error_t getNumFlexPlanes(buffer_handle_t buffer,
117 uint32_t* outNumPlanes);
118
119 gralloc1_error_t lock(buffer_handle_t buffer,
120 gralloc1_producer_usage_t producerUsage,
121 gralloc1_consumer_usage_t consumerUsage,
122 const gralloc1_rect_t* accessRegion, void** outData,
123 const sp<Fence>& acquireFence);
124 gralloc1_error_t lockFlex(buffer_handle_t buffer,
125 gralloc1_producer_usage_t producerUsage,
126 gralloc1_consumer_usage_t consumerUsage,
127 const gralloc1_rect_t* accessRegion,
128 struct android_flex_layout* outData, const sp<Fence>& acquireFence);
129 gralloc1_error_t lockYCbCr(buffer_handle_t buffer,
130 gralloc1_producer_usage_t producerUsage,
131 gralloc1_consumer_usage_t consumerUsage,
132 const gralloc1_rect_t* accessRegion, struct android_ycbcr* outData,
133 const sp<Fence>& acquireFence);
134
135 gralloc1_error_t unlock(buffer_handle_t buffer, sp<Fence>* outFence);
136
137private:
138 std::unordered_set<gralloc1_capability_t> loadCapabilities();
139
140 bool loadFunctions();
141
142 template <typename LockType, typename OutType>
143 gralloc1_error_t lockHelper(LockType pfn, buffer_handle_t buffer,
144 gralloc1_producer_usage_t producerUsage,
145 gralloc1_consumer_usage_t consumerUsage,
146 const gralloc1_rect_t* accessRegion, OutType* outData,
147 const sp<Fence>& acquireFence) {
148 int32_t intError = pfn(mDevice, buffer,
149 static_cast<uint64_t>(producerUsage),
150 static_cast<uint64_t>(consumerUsage), accessRegion, outData,
151 acquireFence->dup());
152 return static_cast<gralloc1_error_t>(intError);
153 }
154
155 gralloc1_device_t* const mDevice;
156
157 const std::unordered_set<gralloc1_capability_t> mCapabilities;
158
159 template <typename PFN, gralloc1_function_descriptor_t descriptor>
160 struct FunctionLoader {
161 FunctionLoader() : pfn(nullptr) {}
162
163 bool load(gralloc1_device_t* device, bool errorIfNull) {
164 gralloc1_function_pointer_t rawPointer =
165 device->getFunction(device, descriptor);
166 pfn = reinterpret_cast<PFN>(rawPointer);
167 if (errorIfNull && !rawPointer) {
168 ALOG(LOG_ERROR, GRALLOC1_LOG_TAG,
169 "Failed to load function pointer %d", descriptor);
170 }
171 return rawPointer != nullptr;
172 }
173
174 template <typename ...Args>
175 typename std::result_of<PFN(Args...)>::type operator()(Args... args) {
176 return pfn(args...);
177 }
178
179 PFN pfn;
180 };
181
182 // Function pointers
183 struct Functions {
184 FunctionLoader<GRALLOC1_PFN_DUMP, GRALLOC1_FUNCTION_DUMP> dump;
185 FunctionLoader<GRALLOC1_PFN_CREATE_DESCRIPTOR,
186 GRALLOC1_FUNCTION_CREATE_DESCRIPTOR> createDescriptor;
187 FunctionLoader<GRALLOC1_PFN_DESTROY_DESCRIPTOR,
188 GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR> destroyDescriptor;
189 FunctionLoader<GRALLOC1_PFN_SET_CONSUMER_USAGE,
190 GRALLOC1_FUNCTION_SET_CONSUMER_USAGE> setConsumerUsage;
191 FunctionLoader<GRALLOC1_PFN_SET_DIMENSIONS,
192 GRALLOC1_FUNCTION_SET_DIMENSIONS> setDimensions;
193 FunctionLoader<GRALLOC1_PFN_SET_FORMAT,
194 GRALLOC1_FUNCTION_SET_FORMAT> setFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700195 FunctionLoader<GRALLOC1_PFN_SET_LAYER_COUNT,
196 GRALLOC1_FUNCTION_SET_LAYER_COUNT> setLayerCount;
Dan Stoza41b12612016-05-20 12:14:37 -0700197 FunctionLoader<GRALLOC1_PFN_SET_PRODUCER_USAGE,
198 GRALLOC1_FUNCTION_SET_PRODUCER_USAGE> setProducerUsage;
199 FunctionLoader<GRALLOC1_PFN_GET_BACKING_STORE,
200 GRALLOC1_FUNCTION_GET_BACKING_STORE> getBackingStore;
201 FunctionLoader<GRALLOC1_PFN_GET_CONSUMER_USAGE,
202 GRALLOC1_FUNCTION_GET_CONSUMER_USAGE> getConsumerUsage;
203 FunctionLoader<GRALLOC1_PFN_GET_DIMENSIONS,
204 GRALLOC1_FUNCTION_GET_DIMENSIONS> getDimensions;
205 FunctionLoader<GRALLOC1_PFN_GET_FORMAT,
206 GRALLOC1_FUNCTION_GET_FORMAT> getFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700207 FunctionLoader<GRALLOC1_PFN_GET_LAYER_COUNT,
208 GRALLOC1_FUNCTION_GET_LAYER_COUNT> getLayerCount;
Dan Stoza41b12612016-05-20 12:14:37 -0700209 FunctionLoader<GRALLOC1_PFN_GET_PRODUCER_USAGE,
210 GRALLOC1_FUNCTION_GET_PRODUCER_USAGE> getProducerUsage;
211 FunctionLoader<GRALLOC1_PFN_GET_STRIDE,
212 GRALLOC1_FUNCTION_GET_STRIDE> getStride;
213 FunctionLoader<GRALLOC1_PFN_ALLOCATE,
214 GRALLOC1_FUNCTION_ALLOCATE> allocate;
215 FunctionLoader<GRALLOC1_PFN_RETAIN,
216 GRALLOC1_FUNCTION_RETAIN> retain;
217 FunctionLoader<GRALLOC1_PFN_RELEASE,
218 GRALLOC1_FUNCTION_RELEASE> release;
219 FunctionLoader<GRALLOC1_PFN_GET_NUM_FLEX_PLANES,
220 GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES> getNumFlexPlanes;
221 FunctionLoader<GRALLOC1_PFN_LOCK,
222 GRALLOC1_FUNCTION_LOCK> lock;
223 FunctionLoader<GRALLOC1_PFN_LOCK_FLEX,
224 GRALLOC1_FUNCTION_LOCK_FLEX> lockFlex;
225 FunctionLoader<GRALLOC1_PFN_LOCK_YCBCR,
226 GRALLOC1_FUNCTION_LOCK_YCBCR> lockYCbCr;
227 FunctionLoader<GRALLOC1_PFN_UNLOCK,
228 GRALLOC1_FUNCTION_UNLOCK> unlock;
229
230 // Adapter-only functions
231 FunctionLoader<GRALLOC1_PFN_RETAIN_GRAPHIC_BUFFER,
232 GRALLOC1_FUNCTION_RETAIN_GRAPHIC_BUFFER> retainGraphicBuffer;
233 FunctionLoader<GRALLOC1_PFN_ALLOCATE_WITH_ID,
234 GRALLOC1_FUNCTION_ALLOCATE_WITH_ID> allocateWithId;
235 } mFunctions;
236
237}; // class android::Gralloc1::Device
238
239class Loader
240{
241public:
242 Loader();
243 ~Loader();
244
245 std::unique_ptr<Device> getDevice();
246
247private:
248 static std::unique_ptr<Gralloc1On0Adapter> mAdapter;
249 std::unique_ptr<Device> mDevice;
250};
251
252} // namespace android::Gralloc1
253
254} // namespace android
255
256#endif