blob: fc53168c14a132f1db4af2e33fe824477fd16055 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Rohit Kulkarni3ac98392017-10-20 12:04:34 -07002 * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303
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 Ahmedfcad05e2018-03-06 20:41:14 -050030#include <log/log.h>
Ananda Kishore3c510ff2018-06-17 17:06:52 +053031#include <cutils/properties.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053032#include <algorithm>
Naseer Ahmede69031e2016-11-22 20:05:16 -050033#include <vector>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053035#include "gr_allocator.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050036#include "gr_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053037#include "gralloc_priv.h"
38
39#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053040
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 Chauhandfe55a22016-09-12 15:48:29 -070049#ifndef ION_FLAG_CP_CAMERA_PREVIEW
50#define ION_FLAG_CP_CAMERA_PREVIEW 0
51#endif
52
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070053#ifndef ION_SECURE
54#define ION_SECURE ION_FLAG_SECURE
55#endif
56
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -070057#ifndef ION_FLAG_CP_CDSP
58#define ION_FLAG_CP_CDSP 0
59#endif
60
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070061#ifdef SLAVE_SIDE_CP
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053062#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 Chauhandfe55a22016-09-12 15:48:29 -070066#define ION_SC_FLAGS ION_SECURE
67#define ION_SC_PREVIEW_FLAGS ION_SECURE
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070068#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 Kandula96e92342016-03-24 21:03:35 +053075#endif
76
Naseer Ahmede69031e2016-11-22 20:05:16 -050077using std::shared_ptr;
Naseer Ahmede36f2242017-12-01 15:33:56 -050078using std::vector;
Naseer Ahmede69031e2016-11-22 20:05:16 -050079
Naseer Ahmede36f2242017-12-01 15:33:56 -050080namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053081
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070082static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
83 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmede36f2242017-12-01 15:33:56 -050084 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070085}
86
Naseer Ahmede36f2242017-12-01 15:33:56 -050087Allocator::Allocator() : ion_allocator_(nullptr) {}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053088
89bool Allocator::Init() {
90 ion_allocator_ = new IonAlloc();
Ananda Kishore3c510ff2018-06-17 17:06:52 +053091 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 Kandula96e92342016-03-24 21:03:35 +053097 if (!ion_allocator_->Init()) {
98 return false;
99 }
100
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530101 return true;
102}
103
104Allocator::~Allocator() {
105 if (ion_allocator_) {
106 delete ion_allocator_;
107 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530108}
109
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700110int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530111 int ret;
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700112 alloc_data->uncached = UseUncached(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530113
114 // After this point we should have the right heap set, there is no fallback
Naseer Ahmede36f2242017-12-01 15:33:56 -0500115 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530116
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 Kandula96e92342016-03-24 21:03:35 +0530128int 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 Ahmed3a9d53a2017-03-15 19:21:40 -0400136int Allocator::ImportBuffer(int fd) {
137 if (ion_allocator_) {
138 return ion_allocator_->ImportBuffer(fd);
139 }
140 return -EINVAL;
141}
142
Naseer Ahmede36f2242017-12-01 15:33:56 -0500143int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530144 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500145 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530146 }
147
148 return -EINVAL;
149}
150
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700151int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
152 int fd) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530153 if (ion_allocator_) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700154 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530155 }
156
157 return -EINVAL;
158}
159
Naseer Ahmede69031e2016-11-22 20:05:16 -0500160bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500161 const vector<shared_ptr<BufferDescriptor>> &descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500162 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530163 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 Kulkarnib20abe72018-03-13 16:55:10 -0700173 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
Naseer Ahmede36f2242017-12-01 15:33:56 -0500174 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530175
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 Shah14c8e5b2017-04-07 10:37:23 -0700182 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
183 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530184 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 Ahmede36f2242017-12-01 15:33:56 -0500198void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
199 unsigned int *ion_flags) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530200 unsigned int heap_id = 0;
201 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400202 uint32_t flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500203 if (usage & GRALLOC_USAGE_PROTECTED) {
204 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530205 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 Ahmed7dc06d12017-03-14 11:13:27 -0400210 flags |= UINT(ION_SD_FLAGS);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500211 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700212 heap_id = ION_HEAP(SD_HEAP_ID);
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700213 if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
214 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
215 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500216 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400217 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700218 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400219 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700220 }
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700221 } 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 Kandula96e92342016-03-24 21:03:35 +0530224 } else {
225 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400226 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530227 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530228 }
229
Naseer Ahmede36f2242017-12-01 15:33:56 -0500230 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
Ananda Kishore3c510ff2018-06-17 17:06:52 +0530231 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 Kandula96e92342016-03-24 21:03:35 +0530238 }
239
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400240 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530241 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 Ahmede69031e2016-11-22 20:05:16 -0500250 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530251 *ion_heap_id = heap_id;
252
253 return;
254}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500255} // namespace gralloc