| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 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_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 |  | 
 | 26 | namespace 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 |  | 
 | 35 | namespace android { | 
 | 36 |  | 
 | 37 | class Fence; | 
 | 38 | class GraphicBuffer; | 
 | 39 |  | 
 | 40 | namespace Gralloc1 { | 
 | 41 |  | 
 | 42 | class Device; | 
 | 43 |  | 
 | 44 | class Descriptor { | 
 | 45 | public: | 
 | 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 Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 52 |         mLayerCount(0), | 
| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 53 |         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 Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 62 |     gralloc1_error_t setLayerCount(uint32_t layerCount); | 
| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 63 |     gralloc1_error_t setProducerUsage(gralloc1_producer_usage_t usage); | 
 | 64 |     gralloc1_error_t setConsumerUsage(gralloc1_consumer_usage_t usage); | 
 | 65 |  | 
 | 66 | private: | 
 | 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 Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 73 |     uint32_t mLayerCount; | 
| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 74 |     gralloc1_producer_usage_t mProducerUsage; | 
 | 75 |     gralloc1_consumer_usage_t mConsumerUsage; | 
 | 76 |  | 
 | 77 | }; // Descriptor | 
 | 78 |  | 
 | 79 | class Device { | 
 | 80 |     friend class Gralloc1::Descriptor; | 
 | 81 |  | 
 | 82 | public: | 
 | 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 |  | 
 | 91 |     gralloc1_error_t getStride(buffer_handle_t buffer, uint32_t* outStride); | 
 | 92 |  | 
 | 93 |     gralloc1_error_t allocate( | 
 | 94 |             const std::vector<std::shared_ptr<const Descriptor>>& descriptors, | 
 | 95 |             std::vector<buffer_handle_t>* outBuffers); | 
 | 96 |     gralloc1_error_t allocate( | 
 | 97 |             const std::shared_ptr<const Descriptor>& descriptor, | 
 | 98 |             gralloc1_backing_store_t id, buffer_handle_t* outBuffer); | 
 | 99 |  | 
 | 100 |     gralloc1_error_t retain(buffer_handle_t buffer); | 
 | 101 |     gralloc1_error_t retain(const GraphicBuffer* buffer); | 
 | 102 |  | 
 | 103 |     gralloc1_error_t release(buffer_handle_t buffer); | 
 | 104 |  | 
 | 105 |     gralloc1_error_t getNumFlexPlanes(buffer_handle_t buffer, | 
 | 106 |             uint32_t* outNumPlanes); | 
 | 107 |  | 
 | 108 |     gralloc1_error_t lock(buffer_handle_t buffer, | 
 | 109 |             gralloc1_producer_usage_t producerUsage, | 
 | 110 |             gralloc1_consumer_usage_t consumerUsage, | 
 | 111 |             const gralloc1_rect_t* accessRegion, void** outData, | 
 | 112 |             const sp<Fence>& acquireFence); | 
 | 113 |     gralloc1_error_t lockFlex(buffer_handle_t buffer, | 
 | 114 |             gralloc1_producer_usage_t producerUsage, | 
 | 115 |             gralloc1_consumer_usage_t consumerUsage, | 
 | 116 |             const gralloc1_rect_t* accessRegion, | 
 | 117 |             struct android_flex_layout* outData, const sp<Fence>& acquireFence); | 
 | 118 |     gralloc1_error_t lockYCbCr(buffer_handle_t buffer, | 
 | 119 |             gralloc1_producer_usage_t producerUsage, | 
 | 120 |             gralloc1_consumer_usage_t consumerUsage, | 
 | 121 |             const gralloc1_rect_t* accessRegion, struct android_ycbcr* outData, | 
 | 122 |             const sp<Fence>& acquireFence); | 
 | 123 |  | 
 | 124 |     gralloc1_error_t unlock(buffer_handle_t buffer, sp<Fence>* outFence); | 
 | 125 |  | 
 | 126 | private: | 
 | 127 |     std::unordered_set<gralloc1_capability_t> loadCapabilities(); | 
 | 128 |  | 
 | 129 |     bool loadFunctions(); | 
 | 130 |  | 
 | 131 |     template <typename LockType, typename OutType> | 
 | 132 |     gralloc1_error_t lockHelper(LockType pfn, buffer_handle_t buffer, | 
 | 133 |             gralloc1_producer_usage_t producerUsage, | 
 | 134 |             gralloc1_consumer_usage_t consumerUsage, | 
 | 135 |             const gralloc1_rect_t* accessRegion, OutType* outData, | 
 | 136 |             const sp<Fence>& acquireFence) { | 
 | 137 |         int32_t intError = pfn(mDevice, buffer, | 
 | 138 |                 static_cast<uint64_t>(producerUsage), | 
 | 139 |                 static_cast<uint64_t>(consumerUsage), accessRegion, outData, | 
 | 140 |                 acquireFence->dup()); | 
 | 141 |         return static_cast<gralloc1_error_t>(intError); | 
 | 142 |     } | 
 | 143 |  | 
 | 144 |     gralloc1_device_t* const mDevice; | 
 | 145 |  | 
 | 146 |     const std::unordered_set<gralloc1_capability_t> mCapabilities; | 
 | 147 |  | 
 | 148 |     template <typename PFN, gralloc1_function_descriptor_t descriptor> | 
 | 149 |     struct FunctionLoader { | 
 | 150 |         FunctionLoader() : pfn(nullptr) {} | 
 | 151 |  | 
 | 152 |         bool load(gralloc1_device_t* device, bool errorIfNull) { | 
 | 153 |             gralloc1_function_pointer_t rawPointer = | 
 | 154 |                     device->getFunction(device, descriptor); | 
 | 155 |             pfn = reinterpret_cast<PFN>(rawPointer); | 
 | 156 |             if (errorIfNull && !rawPointer) { | 
 | 157 |                 ALOG(LOG_ERROR, GRALLOC1_LOG_TAG, | 
 | 158 |                         "Failed to load function pointer %d", descriptor); | 
 | 159 |             } | 
 | 160 |             return rawPointer != nullptr; | 
 | 161 |         } | 
 | 162 |  | 
 | 163 |         template <typename ...Args> | 
 | 164 |         typename std::result_of<PFN(Args...)>::type operator()(Args... args) { | 
 | 165 |             return pfn(args...); | 
 | 166 |         } | 
 | 167 |  | 
 | 168 |         PFN pfn; | 
 | 169 |     }; | 
 | 170 |  | 
 | 171 |     // Function pointers | 
 | 172 |     struct Functions { | 
 | 173 |         FunctionLoader<GRALLOC1_PFN_DUMP, GRALLOC1_FUNCTION_DUMP> dump; | 
 | 174 |         FunctionLoader<GRALLOC1_PFN_CREATE_DESCRIPTOR, | 
 | 175 |                 GRALLOC1_FUNCTION_CREATE_DESCRIPTOR> createDescriptor; | 
 | 176 |         FunctionLoader<GRALLOC1_PFN_DESTROY_DESCRIPTOR, | 
 | 177 |                 GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR> destroyDescriptor; | 
 | 178 |         FunctionLoader<GRALLOC1_PFN_SET_CONSUMER_USAGE, | 
 | 179 |                 GRALLOC1_FUNCTION_SET_CONSUMER_USAGE> setConsumerUsage; | 
 | 180 |         FunctionLoader<GRALLOC1_PFN_SET_DIMENSIONS, | 
 | 181 |                 GRALLOC1_FUNCTION_SET_DIMENSIONS> setDimensions; | 
 | 182 |         FunctionLoader<GRALLOC1_PFN_SET_FORMAT, | 
 | 183 |                 GRALLOC1_FUNCTION_SET_FORMAT> setFormat; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 184 |         FunctionLoader<GRALLOC1_PFN_SET_LAYER_COUNT, | 
 | 185 |                 GRALLOC1_FUNCTION_SET_LAYER_COUNT> setLayerCount; | 
| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 186 |         FunctionLoader<GRALLOC1_PFN_SET_PRODUCER_USAGE, | 
 | 187 |                 GRALLOC1_FUNCTION_SET_PRODUCER_USAGE> setProducerUsage; | 
 | 188 |         FunctionLoader<GRALLOC1_PFN_GET_BACKING_STORE, | 
 | 189 |                 GRALLOC1_FUNCTION_GET_BACKING_STORE> getBackingStore; | 
 | 190 |         FunctionLoader<GRALLOC1_PFN_GET_CONSUMER_USAGE, | 
 | 191 |                 GRALLOC1_FUNCTION_GET_CONSUMER_USAGE> getConsumerUsage; | 
 | 192 |         FunctionLoader<GRALLOC1_PFN_GET_DIMENSIONS, | 
 | 193 |                 GRALLOC1_FUNCTION_GET_DIMENSIONS> getDimensions; | 
 | 194 |         FunctionLoader<GRALLOC1_PFN_GET_FORMAT, | 
 | 195 |                 GRALLOC1_FUNCTION_GET_FORMAT> getFormat; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 196 |         FunctionLoader<GRALLOC1_PFN_GET_LAYER_COUNT, | 
 | 197 |                 GRALLOC1_FUNCTION_GET_LAYER_COUNT> getLayerCount; | 
| Dan Stoza | 41b1261 | 2016-05-20 12:14:37 -0700 | [diff] [blame] | 198 |         FunctionLoader<GRALLOC1_PFN_GET_PRODUCER_USAGE, | 
 | 199 |                 GRALLOC1_FUNCTION_GET_PRODUCER_USAGE> getProducerUsage; | 
 | 200 |         FunctionLoader<GRALLOC1_PFN_GET_STRIDE, | 
 | 201 |                 GRALLOC1_FUNCTION_GET_STRIDE> getStride; | 
 | 202 |         FunctionLoader<GRALLOC1_PFN_ALLOCATE, | 
 | 203 |                 GRALLOC1_FUNCTION_ALLOCATE> allocate; | 
 | 204 |         FunctionLoader<GRALLOC1_PFN_RETAIN, | 
 | 205 |                 GRALLOC1_FUNCTION_RETAIN> retain; | 
 | 206 |         FunctionLoader<GRALLOC1_PFN_RELEASE, | 
 | 207 |                 GRALLOC1_FUNCTION_RELEASE> release; | 
 | 208 |         FunctionLoader<GRALLOC1_PFN_GET_NUM_FLEX_PLANES, | 
 | 209 |                 GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES> getNumFlexPlanes; | 
 | 210 |         FunctionLoader<GRALLOC1_PFN_LOCK, | 
 | 211 |                 GRALLOC1_FUNCTION_LOCK> lock; | 
 | 212 |         FunctionLoader<GRALLOC1_PFN_LOCK_FLEX, | 
 | 213 |                 GRALLOC1_FUNCTION_LOCK_FLEX> lockFlex; | 
 | 214 |         FunctionLoader<GRALLOC1_PFN_LOCK_YCBCR, | 
 | 215 |                 GRALLOC1_FUNCTION_LOCK_YCBCR> lockYCbCr; | 
 | 216 |         FunctionLoader<GRALLOC1_PFN_UNLOCK, | 
 | 217 |                 GRALLOC1_FUNCTION_UNLOCK> unlock; | 
 | 218 |  | 
 | 219 |         // Adapter-only functions | 
 | 220 |         FunctionLoader<GRALLOC1_PFN_RETAIN_GRAPHIC_BUFFER, | 
 | 221 |                 GRALLOC1_FUNCTION_RETAIN_GRAPHIC_BUFFER> retainGraphicBuffer; | 
 | 222 |         FunctionLoader<GRALLOC1_PFN_ALLOCATE_WITH_ID, | 
 | 223 |                 GRALLOC1_FUNCTION_ALLOCATE_WITH_ID> allocateWithId; | 
 | 224 |     } mFunctions; | 
 | 225 |  | 
 | 226 | }; // class android::Gralloc1::Device | 
 | 227 |  | 
 | 228 | class Loader | 
 | 229 | { | 
 | 230 | public: | 
 | 231 |     Loader(); | 
 | 232 |     ~Loader(); | 
 | 233 |  | 
 | 234 |     std::unique_ptr<Device> getDevice(); | 
 | 235 |  | 
 | 236 | private: | 
 | 237 |     static std::unique_ptr<Gralloc1On0Adapter> mAdapter; | 
 | 238 |     std::unique_ptr<Device> mDevice; | 
 | 239 | }; | 
 | 240 |  | 
 | 241 | } // namespace android::Gralloc1 | 
 | 242 |  | 
 | 243 | } // namespace android | 
 | 244 |  | 
 | 245 | #endif |