blob: 4c16b6267d80d98bc801788449e833a44559d488 [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
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070057#ifdef SLAVE_SIDE_CP
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053058#define CP_HEAP_ID ION_CP_MM_HEAP_ID
59#define SD_HEAP_ID CP_HEAP_ID
60#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
61#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070062#define ION_SC_FLAGS ION_SECURE
63#define ION_SC_PREVIEW_FLAGS ION_SECURE
Prabhakar Reddy Krishnappa348a0cf2018-05-31 11:38:40 -070064#else // MASTER_SIDE_CP
65#define CP_HEAP_ID ION_SECURE_HEAP_ID
66#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
67#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
68#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
69#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
70#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053071#endif
72
Naseer Ahmede69031e2016-11-22 20:05:16 -050073using std::shared_ptr;
Naseer Ahmede36f2242017-12-01 15:33:56 -050074using std::vector;
Naseer Ahmede69031e2016-11-22 20:05:16 -050075
Naseer Ahmede36f2242017-12-01 15:33:56 -050076namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053077
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070078static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
79 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmede36f2242017-12-01 15:33:56 -050080 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070081}
82
Naseer Ahmede36f2242017-12-01 15:33:56 -050083Allocator::Allocator() : ion_allocator_(nullptr) {}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053084
85bool Allocator::Init() {
86 ion_allocator_ = new IonAlloc();
Ananda Kishore3c510ff2018-06-17 17:06:52 +053087 char property[PROPERTY_VALUE_MAX];
88 property_get(USE_SYSTEM_HEAP_FOR_SENSORS, property, "1");
89 if (!(strncmp(property, "0", PROPERTY_VALUE_MAX))) {
90 use_system_heap_for_sensors_ = false;
91 }
92
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093 if (!ion_allocator_->Init()) {
94 return false;
95 }
96
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053097 return true;
98}
99
100Allocator::~Allocator() {
101 if (ion_allocator_) {
102 delete ion_allocator_;
103 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530104}
105
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700106int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530107 int ret;
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700108 alloc_data->uncached = UseUncached(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530109
110 // After this point we should have the right heap set, there is no fallback
Naseer Ahmede36f2242017-12-01 15:33:56 -0500111 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530112
113 ret = ion_allocator_->AllocBuffer(alloc_data);
114 if (ret >= 0) {
115 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
116 } else {
117 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
118 alloc_data->heap_id, alloc_data->flags);
119 }
120
121 return ret;
122}
123
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530124int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
125 if (ion_allocator_) {
126 return ion_allocator_->MapBuffer(base, size, offset, fd);
127 }
128
129 return -EINVAL;
130}
131
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400132int Allocator::ImportBuffer(int fd) {
133 if (ion_allocator_) {
134 return ion_allocator_->ImportBuffer(fd);
135 }
136 return -EINVAL;
137}
138
Naseer Ahmede36f2242017-12-01 15:33:56 -0500139int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530140 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500141 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530142 }
143
144 return -EINVAL;
145}
146
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700147int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
148 int fd) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530149 if (ion_allocator_) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700150 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151 }
152
153 return -EINVAL;
154}
155
Naseer Ahmede69031e2016-11-22 20:05:16 -0500156bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500157 const vector<shared_ptr<BufferDescriptor>> &descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500158 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530159 unsigned int cur_heap_id = 0, prev_heap_id = 0;
160 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
161 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
162 bool cur_uncached = false, prev_uncached = false;
163 unsigned int alignedw, alignedh;
164 unsigned int max_size = 0;
165
166 *max_index = -1;
167 for (uint32_t i = 0; i < num_descriptors; i++) {
168 // Check Cached vs non-cached and all the ION flags
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700169 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
Naseer Ahmede36f2242017-12-01 15:33:56 -0500170 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530171
172 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
173 cur_ion_flags != prev_ion_flags)) {
174 return false;
175 }
176
177 // For same format type, find the descriptor with bigger size
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700178 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
179 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530180 if (max_size < size) {
181 *max_index = INT(i);
182 max_size = size;
183 }
184
185 prev_heap_id = cur_heap_id;
186 prev_uncached = cur_uncached;
187 prev_ion_flags = cur_ion_flags;
188 prev_alloc_type = cur_alloc_type;
189 }
190
191 return true;
192}
193
Naseer Ahmede36f2242017-12-01 15:33:56 -0500194void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
195 unsigned int *ion_flags) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530196 unsigned int heap_id = 0;
197 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400198 uint32_t flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500199 if (usage & GRALLOC_USAGE_PROTECTED) {
200 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530201 heap_id = ION_HEAP(SD_HEAP_ID);
202 /*
203 * There is currently no flag in ION for Secure Display
204 * VM. Please add it to the define once available.
205 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400206 flags |= UINT(ION_SD_FLAGS);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500207 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700208 heap_id = ION_HEAP(SD_HEAP_ID);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500209 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400210 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700211 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400212 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700213 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530214 } else {
215 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400216 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530217 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530218 }
219
Naseer Ahmede36f2242017-12-01 15:33:56 -0500220 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
Ananda Kishore3c510ff2018-06-17 17:06:52 +0530221 if (use_system_heap_for_sensors_) {
222 ALOGI("gralloc::sns_direct_data with system_heap");
223 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
224 } else {
225 ALOGI("gralloc::sns_direct_data with adsp_heap");
226 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
227 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530228 }
229
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400230 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
232 }
233
234 // if no ion heap flags are set, default to system heap
235 if (!heap_id) {
236 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
237 }
238
239 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500240 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530241 *ion_heap_id = heap_id;
242
243 return;
244}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500245} // namespace gralloc