blob: a8073195fccf146da97abf0674d3c83b414645cd [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Tharaga Balachandran576571c2020-01-23 18:41:10 -05002 * Copyright (c) 2011-2020, 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
Xiaowen Wu139436c2019-11-22 17:06:30 -050069#ifdef HYPERVISOR
70#define CP_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
71#else
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070072#define CP_HEAP_ID ION_SECURE_HEAP_ID
Xiaowen Wu139436c2019-11-22 17:06:30 -050073#endif
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070074#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
75#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
76#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
77#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
78#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079#endif
80
Naseer Ahmede69031e2016-11-22 20:05:16 -050081using std::shared_ptr;
Naseer Ahmede36f2242017-12-01 15:33:56 -050082using std::vector;
Naseer Ahmede69031e2016-11-22 20:05:16 -050083
Naseer Ahmede36f2242017-12-01 15:33:56 -050084namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053085
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070086static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
87 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmede36f2242017-12-01 15:33:56 -050088 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070089}
90
Naseer Ahmede36f2242017-12-01 15:33:56 -050091Allocator::Allocator() : ion_allocator_(nullptr) {}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053092
93bool Allocator::Init() {
94 ion_allocator_ = new IonAlloc();
Ananda Kishore3c510ff2018-06-17 17:06:52 +053095
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096 if (!ion_allocator_->Init()) {
97 return false;
98 }
99
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530100 return true;
101}
102
103Allocator::~Allocator() {
104 if (ion_allocator_) {
105 delete ion_allocator_;
106 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530107}
108
Tharaga Balachandran576571c2020-01-23 18:41:10 -0500109void Allocator::SetProperties(gralloc::GrallocProperties props) {
110 use_system_heap_for_sensors_ = props.use_system_heap_for_sensors;
111}
112
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700113int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530114 int ret;
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700115 alloc_data->uncached = UseUncached(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530116
117 // After this point we should have the right heap set, there is no fallback
Naseer Ahmede36f2242017-12-01 15:33:56 -0500118 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530119
120 ret = ion_allocator_->AllocBuffer(alloc_data);
121 if (ret >= 0) {
122 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
123 } else {
124 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
125 alloc_data->heap_id, alloc_data->flags);
126 }
127
128 return ret;
129}
130
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530131int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
132 if (ion_allocator_) {
133 return ion_allocator_->MapBuffer(base, size, offset, fd);
134 }
135
136 return -EINVAL;
137}
138
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400139int Allocator::ImportBuffer(int fd) {
140 if (ion_allocator_) {
141 return ion_allocator_->ImportBuffer(fd);
142 }
143 return -EINVAL;
144}
145
Naseer Ahmede36f2242017-12-01 15:33:56 -0500146int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530147 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500148 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530149 }
150
151 return -EINVAL;
152}
153
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700154int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
155 int fd) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156 if (ion_allocator_) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700157 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530158 }
159
160 return -EINVAL;
161}
162
Naseer Ahmede69031e2016-11-22 20:05:16 -0500163bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500164 const vector<shared_ptr<BufferDescriptor>> &descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500165 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530166 unsigned int cur_heap_id = 0, prev_heap_id = 0;
167 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
168 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
169 bool cur_uncached = false, prev_uncached = false;
170 unsigned int alignedw, alignedh;
171 unsigned int max_size = 0;
172
173 *max_index = -1;
174 for (uint32_t i = 0; i < num_descriptors; i++) {
175 // Check Cached vs non-cached and all the ION flags
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700176 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
Naseer Ahmede36f2242017-12-01 15:33:56 -0500177 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530178
179 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
180 cur_ion_flags != prev_ion_flags)) {
181 return false;
182 }
183
184 // For same format type, find the descriptor with bigger size
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700185 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
186 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530187 if (max_size < size) {
188 *max_index = INT(i);
189 max_size = size;
190 }
191
192 prev_heap_id = cur_heap_id;
193 prev_uncached = cur_uncached;
194 prev_ion_flags = cur_ion_flags;
195 prev_alloc_type = cur_alloc_type;
196 }
197
198 return true;
199}
200
Naseer Ahmede36f2242017-12-01 15:33:56 -0500201void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
202 unsigned int *ion_flags) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530203 unsigned int heap_id = 0;
204 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400205 uint32_t flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500206 if (usage & GRALLOC_USAGE_PROTECTED) {
207 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530208 heap_id = ION_HEAP(SD_HEAP_ID);
209 /*
210 * There is currently no flag in ION for Secure Display
211 * VM. Please add it to the define once available.
212 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400213 flags |= UINT(ION_SD_FLAGS);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500214 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700215 heap_id = ION_HEAP(SD_HEAP_ID);
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700216 if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
217 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
218 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500219 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400220 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700221 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400222 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700223 }
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700224 } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
225 heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID);
226 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530227 } else {
228 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400229 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530230 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 }
232
Naseer Ahmede36f2242017-12-01 15:33:56 -0500233 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
Ananda Kishore3c510ff2018-06-17 17:06:52 +0530234 if (use_system_heap_for_sensors_) {
235 ALOGI("gralloc::sns_direct_data with system_heap");
236 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
237 } else {
238 ALOGI("gralloc::sns_direct_data with adsp_heap");
239 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
240 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530241 }
242
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400243 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530244 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
245 }
246
247 // if no ion heap flags are set, default to system heap
248 if (!heap_id) {
249 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
250 }
251
252 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500253 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530254 *ion_heap_id = heap_id;
255
256 return;
257}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500258} // namespace gralloc