Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-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 | |
Naseer Ahmed | fcad05e | 2018-03-06 20:41:14 -0500 | [diff] [blame] | 30 | #include <log/log.h> |
Ananda Kishore | 3c510ff | 2018-06-17 17:06:52 +0530 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 32 | #include <algorithm> |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 33 | #include <vector> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 34 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 35 | #include "gr_allocator.h" |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 36 | #include "gr_utils.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 37 | #include "gralloc_priv.h" |
| 38 | |
| 39 | #include "qd_utils.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 40 | |
| 41 | #ifndef ION_FLAG_CP_PIXEL |
| 42 | #define ION_FLAG_CP_PIXEL 0 |
| 43 | #endif |
| 44 | |
| 45 | #ifndef ION_FLAG_ALLOW_NON_CONTIG |
| 46 | #define ION_FLAG_ALLOW_NON_CONTIG 0 |
| 47 | #endif |
| 48 | |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 49 | #ifndef ION_FLAG_CP_CAMERA_PREVIEW |
| 50 | #define ION_FLAG_CP_CAMERA_PREVIEW 0 |
| 51 | #endif |
| 52 | |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 53 | #ifndef ION_SECURE |
| 54 | #define ION_SECURE ION_FLAG_SECURE |
| 55 | #endif |
| 56 | |
Ramkumar Radhakrishnan | 2b4de27 | 2018-08-30 17:26:56 -0700 | [diff] [blame^] | 57 | #ifndef ION_FLAG_CP_CDSP |
| 58 | #define ION_FLAG_CP_CDSP 0 |
| 59 | #endif |
| 60 | |
Prabhakar Reddy Krishnappa | 348a0cf | 2018-05-31 11:38:40 -0700 | [diff] [blame] | 61 | #ifdef SLAVE_SIDE_CP |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 62 | #define CP_HEAP_ID ION_CP_MM_HEAP_ID |
| 63 | #define SD_HEAP_ID CP_HEAP_ID |
| 64 | #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG) |
| 65 | #define ION_SD_FLAGS ION_SECURE |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 66 | #define ION_SC_FLAGS ION_SECURE |
| 67 | #define ION_SC_PREVIEW_FLAGS ION_SECURE |
Prabhakar Reddy Krishnappa | 348a0cf | 2018-05-31 11:38:40 -0700 | [diff] [blame] | 68 | #else // MASTER_SIDE_CP |
| 69 | #define CP_HEAP_ID ION_SECURE_HEAP_ID |
| 70 | #define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID |
| 71 | #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL) |
| 72 | #define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY) |
| 73 | #define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA) |
| 74 | #define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW) |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 75 | #endif |
| 76 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 77 | using std::shared_ptr; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 78 | using std::vector; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 79 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 80 | namespace gralloc { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 81 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 82 | static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) { |
| 83 | return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(), |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 84 | descriptor.GetUsage()); |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 87 | Allocator::Allocator() : ion_allocator_(nullptr) {} |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 88 | |
| 89 | bool Allocator::Init() { |
| 90 | ion_allocator_ = new IonAlloc(); |
Ananda Kishore | 3c510ff | 2018-06-17 17:06:52 +0530 | [diff] [blame] | 91 | char property[PROPERTY_VALUE_MAX]; |
| 92 | property_get(USE_SYSTEM_HEAP_FOR_SENSORS, property, "1"); |
| 93 | if (!(strncmp(property, "0", PROPERTY_VALUE_MAX))) { |
| 94 | use_system_heap_for_sensors_ = false; |
| 95 | } |
| 96 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 97 | if (!ion_allocator_->Init()) { |
| 98 | return false; |
| 99 | } |
| 100 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
| 104 | Allocator::~Allocator() { |
| 105 | if (ion_allocator_) { |
| 106 | delete ion_allocator_; |
| 107 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 108 | } |
| 109 | |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 110 | int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 111 | int ret; |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 112 | alloc_data->uncached = UseUncached(format, usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 113 | |
| 114 | // After this point we should have the right heap set, there is no fallback |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 115 | GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 116 | |
| 117 | ret = ion_allocator_->AllocBuffer(alloc_data); |
| 118 | if (ret >= 0) { |
| 119 | alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION; |
| 120 | } else { |
| 121 | ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__, |
| 122 | alloc_data->heap_id, alloc_data->flags); |
| 123 | } |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 128 | int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) { |
| 129 | if (ion_allocator_) { |
| 130 | return ion_allocator_->MapBuffer(base, size, offset, fd); |
| 131 | } |
| 132 | |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 136 | int Allocator::ImportBuffer(int fd) { |
| 137 | if (ion_allocator_) { |
| 138 | return ion_allocator_->ImportBuffer(fd); |
| 139 | } |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 143 | int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 144 | if (ion_allocator_) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 145 | return ion_allocator_->FreeBuffer(base, size, offset, fd, handle); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 151 | int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op, |
| 152 | int fd) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 153 | if (ion_allocator_) { |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 154 | return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 160 | bool Allocator::CheckForBufferSharing(uint32_t num_descriptors, |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 161 | const vector<shared_ptr<BufferDescriptor>> &descriptors, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 162 | ssize_t *max_index) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 163 | unsigned int cur_heap_id = 0, prev_heap_id = 0; |
| 164 | unsigned int cur_alloc_type = 0, prev_alloc_type = 0; |
| 165 | unsigned int cur_ion_flags = 0, prev_ion_flags = 0; |
| 166 | bool cur_uncached = false, prev_uncached = false; |
| 167 | unsigned int alignedw, alignedh; |
| 168 | unsigned int max_size = 0; |
| 169 | |
| 170 | *max_index = -1; |
| 171 | for (uint32_t i = 0; i < num_descriptors; i++) { |
| 172 | // Check Cached vs non-cached and all the ION flags |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 173 | cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage()); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 174 | GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 175 | |
| 176 | if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type || |
| 177 | cur_ion_flags != prev_ion_flags)) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // For same format type, find the descriptor with bigger size |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 182 | GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh); |
| 183 | unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 184 | if (max_size < size) { |
| 185 | *max_index = INT(i); |
| 186 | max_size = size; |
| 187 | } |
| 188 | |
| 189 | prev_heap_id = cur_heap_id; |
| 190 | prev_uncached = cur_uncached; |
| 191 | prev_ion_flags = cur_ion_flags; |
| 192 | prev_alloc_type = cur_alloc_type; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 198 | void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type, |
| 199 | unsigned int *ion_flags) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 200 | unsigned int heap_id = 0; |
| 201 | unsigned int type = 0; |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 202 | uint32_t flags = 0; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 203 | if (usage & GRALLOC_USAGE_PROTECTED) { |
| 204 | if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 205 | heap_id = ION_HEAP(SD_HEAP_ID); |
| 206 | /* |
| 207 | * There is currently no flag in ION for Secure Display |
| 208 | * VM. Please add it to the define once available. |
| 209 | */ |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 210 | flags |= UINT(ION_SD_FLAGS); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 211 | } else if (usage & BufferUsage::CAMERA_OUTPUT) { |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 212 | heap_id = ION_HEAP(SD_HEAP_ID); |
Ramkumar Radhakrishnan | 2b4de27 | 2018-08-30 17:26:56 -0700 | [diff] [blame^] | 213 | if (usage & GRALLOC_USAGE_PRIVATE_CDSP) { |
| 214 | flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP); |
| 215 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 216 | if (usage & BufferUsage::COMPOSER_OVERLAY) { |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 217 | flags |= UINT(ION_SC_PREVIEW_FLAGS); |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 218 | } else { |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 219 | flags |= UINT(ION_SC_FLAGS); |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 220 | } |
Ramkumar Radhakrishnan | 2b4de27 | 2018-08-30 17:26:56 -0700 | [diff] [blame^] | 221 | } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) { |
| 222 | heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID); |
| 223 | flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 224 | } else { |
| 225 | heap_id = ION_HEAP(CP_HEAP_ID); |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 226 | flags |= UINT(ION_CP_FLAGS); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 227 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 228 | } |
| 229 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 230 | if (usage & BufferUsage::SENSOR_DIRECT_DATA) { |
Ananda Kishore | 3c510ff | 2018-06-17 17:06:52 +0530 | [diff] [blame] | 231 | if (use_system_heap_for_sensors_) { |
| 232 | ALOGI("gralloc::sns_direct_data with system_heap"); |
| 233 | heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 234 | } else { |
| 235 | ALOGI("gralloc::sns_direct_data with adsp_heap"); |
| 236 | heap_id |= ION_HEAP(ION_ADSP_HEAP_ID); |
| 237 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 238 | } |
| 239 | |
Naseer Ahmed | 7dc06d1 | 2017-03-14 11:13:27 -0400 | [diff] [blame] | 240 | if (flags & UINT(ION_SECURE)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 241 | type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; |
| 242 | } |
| 243 | |
| 244 | // if no ion heap flags are set, default to system heap |
| 245 | if (!heap_id) { |
| 246 | heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 247 | } |
| 248 | |
| 249 | *alloc_type = type; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 250 | *ion_flags = flags; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 251 | *ion_heap_id = heap_id; |
| 252 | |
| 253 | return; |
| 254 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 255 | } // namespace gralloc |