blob: 642115727cfc5e6cb37d501bb30890b70826afb3 [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
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 char property[PROPERTY_VALUE_MAX];
96 property_get(USE_SYSTEM_HEAP_FOR_SENSORS, property, "1");
97 if (!(strncmp(property, "0", PROPERTY_VALUE_MAX))) {
98 use_system_heap_for_sensors_ = false;
99 }
100
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530101 if (!ion_allocator_->Init()) {
102 return false;
103 }
104
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530105 return true;
106}
107
108Allocator::~Allocator() {
109 if (ion_allocator_) {
110 delete ion_allocator_;
111 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530112}
113
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700114int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530115 int ret;
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700116 alloc_data->uncached = UseUncached(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530117
118 // After this point we should have the right heap set, there is no fallback
Naseer Ahmede36f2242017-12-01 15:33:56 -0500119 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530120
121 ret = ion_allocator_->AllocBuffer(alloc_data);
122 if (ret >= 0) {
123 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
124 } else {
125 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
126 alloc_data->heap_id, alloc_data->flags);
127 }
128
129 return ret;
130}
131
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530132int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
133 if (ion_allocator_) {
134 return ion_allocator_->MapBuffer(base, size, offset, fd);
135 }
136
137 return -EINVAL;
138}
139
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400140int Allocator::ImportBuffer(int fd) {
141 if (ion_allocator_) {
142 return ion_allocator_->ImportBuffer(fd);
143 }
144 return -EINVAL;
145}
146
Naseer Ahmede36f2242017-12-01 15:33:56 -0500147int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530148 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500149 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530150 }
151
152 return -EINVAL;
153}
154
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700155int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
156 int fd) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530157 if (ion_allocator_) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700158 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530159 }
160
161 return -EINVAL;
162}
163
Naseer Ahmede69031e2016-11-22 20:05:16 -0500164bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500165 const vector<shared_ptr<BufferDescriptor>> &descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500166 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530167 unsigned int cur_heap_id = 0, prev_heap_id = 0;
168 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
169 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
170 bool cur_uncached = false, prev_uncached = false;
171 unsigned int alignedw, alignedh;
172 unsigned int max_size = 0;
173
174 *max_index = -1;
175 for (uint32_t i = 0; i < num_descriptors; i++) {
176 // Check Cached vs non-cached and all the ION flags
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700177 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
Naseer Ahmede36f2242017-12-01 15:33:56 -0500178 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179
180 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
181 cur_ion_flags != prev_ion_flags)) {
182 return false;
183 }
184
185 // For same format type, find the descriptor with bigger size
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700186 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
187 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530188 if (max_size < size) {
189 *max_index = INT(i);
190 max_size = size;
191 }
192
193 prev_heap_id = cur_heap_id;
194 prev_uncached = cur_uncached;
195 prev_ion_flags = cur_ion_flags;
196 prev_alloc_type = cur_alloc_type;
197 }
198
199 return true;
200}
201
Naseer Ahmede36f2242017-12-01 15:33:56 -0500202void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
203 unsigned int *ion_flags) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530204 unsigned int heap_id = 0;
205 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400206 uint32_t flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500207 if (usage & GRALLOC_USAGE_PROTECTED) {
208 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530209 heap_id = ION_HEAP(SD_HEAP_ID);
210 /*
211 * There is currently no flag in ION for Secure Display
212 * VM. Please add it to the define once available.
213 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400214 flags |= UINT(ION_SD_FLAGS);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500215 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700216 heap_id = ION_HEAP(SD_HEAP_ID);
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700217 if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
218 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
219 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500220 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400221 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700222 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400223 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700224 }
Ramkumar Radhakrishnan2b4de272018-08-30 17:26:56 -0700225 } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
226 heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID);
227 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530228 } else {
229 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400230 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530232 }
233
Naseer Ahmede36f2242017-12-01 15:33:56 -0500234 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
Ananda Kishore3c510ff2018-06-17 17:06:52 +0530235 if (use_system_heap_for_sensors_) {
236 ALOGI("gralloc::sns_direct_data with system_heap");
237 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
238 } else {
239 ALOGI("gralloc::sns_direct_data with adsp_heap");
240 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
241 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530242 }
243
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400244 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530245 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
246 }
247
248 // if no ion heap flags are set, default to system heap
249 if (!heap_id) {
250 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
251 }
252
253 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500254 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530255 *ion_heap_id = heap_id;
256
257 return;
258}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500259} // namespace gralloc