Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef __GR_DEVICE_IMPL_H__ |
| 31 | #define __GR_DEVICE_IMPL_H__ |
| 32 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 33 | #include <hardware/gralloc1.h> |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 34 | #include <hardware/hardware.h> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 35 | #include "gr_buf_mgr.h" |
| 36 | |
| 37 | struct private_module_t { |
| 38 | hw_module_t base; |
| 39 | }; |
| 40 | |
| 41 | #define GRALLOC_IMPL(exp) reinterpret_cast<GrallocImpl const *>(exp) |
| 42 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 43 | namespace gralloc { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 44 | |
| 45 | class GrallocImpl : public gralloc1_device_t { |
| 46 | public: |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 47 | static int CloseDevice(hw_device_t *device); |
| 48 | static void GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, |
| 49 | int32_t * /*gralloc1_capability_t*/ out_capabilities); |
| 50 | static gralloc1_function_pointer_t GetFunction( |
| 51 | struct gralloc1_device *device, int32_t /*gralloc1_function_descriptor_t*/ descriptor); |
| 52 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 53 | static GrallocImpl *GetInstance(const struct hw_module_t *module) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 54 | static GrallocImpl *instance = new GrallocImpl(module); |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 55 | if (instance->IsInitialized()) { |
| 56 | return instance; |
| 57 | } else { |
| 58 | return nullptr; |
| 59 | } |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 62 | private: |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 63 | static inline gralloc1_error_t Dump(gralloc1_device_t *device, uint32_t *out_size, |
| 64 | char *out_buffer); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 65 | static inline gralloc1_error_t CheckDeviceAndHandle(gralloc1_device_t *device, |
| 66 | buffer_handle_t buffer); |
| 67 | static gralloc1_error_t CreateBufferDescriptor(gralloc1_device_t *device, |
| 68 | gralloc1_buffer_descriptor_t *out_descriptor); |
| 69 | static gralloc1_error_t DestroyBufferDescriptor(gralloc1_device_t *device, |
| 70 | gralloc1_buffer_descriptor_t descriptor); |
| 71 | static gralloc1_error_t SetConsumerUsage(gralloc1_device_t *device, |
| 72 | gralloc1_buffer_descriptor_t descriptor, |
| 73 | gralloc1_consumer_usage_t usage); |
| 74 | static gralloc1_error_t SetBufferDimensions(gralloc1_device_t *device, |
| 75 | gralloc1_buffer_descriptor_t descriptor, |
| 76 | uint32_t width, uint32_t height); |
| 77 | static gralloc1_error_t SetColorFormat(gralloc1_device_t *device, |
| 78 | gralloc1_buffer_descriptor_t descriptor, int32_t format); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 79 | static gralloc1_error_t SetLayerCount(gralloc1_device_t *device, |
| 80 | gralloc1_buffer_descriptor_t descriptor, |
| 81 | uint32_t layer_count); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 82 | static gralloc1_error_t SetProducerUsage(gralloc1_device_t *device, |
| 83 | gralloc1_buffer_descriptor_t descriptor, |
| 84 | gralloc1_producer_usage_t usage); |
| 85 | static gralloc1_error_t GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, |
| 86 | gralloc1_backing_store_t *out_store); |
| 87 | static gralloc1_error_t GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, |
| 88 | gralloc1_consumer_usage_t *out_usage); |
| 89 | static gralloc1_error_t GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, |
| 90 | uint32_t *out_width, uint32_t *out_height); |
| 91 | static gralloc1_error_t GetColorFormat(gralloc1_device_t *device, buffer_handle_t descriptor, |
| 92 | int32_t *outFormat); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 93 | static gralloc1_error_t GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer, |
| 94 | uint32_t *out_layer_count); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 95 | static gralloc1_error_t GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, |
| 96 | gralloc1_producer_usage_t *out_usage); |
| 97 | static gralloc1_error_t GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, |
| 98 | uint32_t *out_stride); |
| 99 | static gralloc1_error_t AllocateBuffers(gralloc1_device_t *device, uint32_t num_dptors, |
| 100 | const gralloc1_buffer_descriptor_t *descriptors, |
| 101 | buffer_handle_t *out_buffers); |
| 102 | static gralloc1_error_t RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer); |
| 103 | static gralloc1_error_t ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 104 | static gralloc1_error_t GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 105 | uint32_t *out_num_planes); |
| 106 | static gralloc1_error_t LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, |
| 107 | gralloc1_producer_usage_t prod_usage, |
| 108 | gralloc1_consumer_usage_t cons_usage, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 109 | const gralloc1_rect_t *region, void **out_data, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 110 | int32_t acquire_fence); |
| 111 | static gralloc1_error_t LockFlex(gralloc1_device_t *device, buffer_handle_t buffer, |
| 112 | gralloc1_producer_usage_t prod_usage, |
| 113 | gralloc1_consumer_usage_t cons_usage, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 114 | const gralloc1_rect_t *region, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 115 | struct android_flex_layout *out_flex_layout, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 116 | int32_t acquire_fence); |
| 117 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 118 | static gralloc1_error_t UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, |
| 119 | int32_t *release_fence); |
| 120 | static gralloc1_error_t Gralloc1Perform(gralloc1_device_t *device, int operation, ...); |
| 121 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 122 | gralloc1_error_t CreateBufferDescriptorLocked(gralloc1_buffer_descriptor_t *descriptor_id); |
| 123 | gralloc1_error_t DestroyBufferDescriptorLocked(gralloc1_buffer_descriptor_t descriptor_id); |
| 124 | gralloc1_error_t AllocateBuffer(const gralloc1_buffer_descriptor_t *descriptor_ids, |
| 125 | buffer_handle_t *out_buffers); |
| 126 | gralloc1_error_t GetFlexLayout(const private_handle_t *hnd, struct android_flex_layout *layout); |
| 127 | |
| 128 | template <typename... Args> |
| 129 | gralloc1_error_t CallBufferDescriptorFunction(gralloc1_buffer_descriptor_t descriptor_id, |
| 130 | void (BufferDescriptor::*member)(Args...), |
| 131 | Args... args) { |
| 132 | std::lock_guard<std::mutex> lock(descriptor_lock_); |
| 133 | const auto map_descriptor = descriptors_map_.find(descriptor_id); |
| 134 | if (map_descriptor == descriptors_map_.end()) { |
| 135 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 136 | } |
| 137 | const auto descriptor = map_descriptor->second; |
| 138 | (descriptor.get()->*member)(std::forward<Args>(args)...); |
| 139 | return GRALLOC1_ERROR_NONE; |
| 140 | } |
| 141 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 142 | explicit GrallocImpl(const hw_module_t *module); |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 143 | ~GrallocImpl(); |
| 144 | bool Init(); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 145 | bool IsInitialized() const { return initialized_; } |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 146 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 147 | BufferManager *buf_mgr_ = NULL; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 148 | bool initialized_ = false; |
| 149 | std::mutex descriptor_lock_; |
| 150 | std::unordered_map<gralloc1_buffer_descriptor_t, std::shared_ptr<gralloc::BufferDescriptor>> |
| 151 | descriptors_map_ = {}; |
| 152 | static std::atomic<uint64_t> next_descriptor_id_; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 153 | }; |
| 154 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 155 | } // namespace gralloc |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 156 | |
| 157 | #endif // __GR_DEVICE_IMPL_H__ |