blob: 00a328cea7c33556f4f4ff2885dfe9b313b483ca [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmeda422f352017-12-01 15:33:56 -05002 * 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
30#include <cutils/log.h>
31#include <algorithm>
Naseer Ahmede69031e2016-11-22 20:05:16 -050032#include <vector>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053033
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034#include "gr_allocator.h"
Naseer Ahmeda422f352017-12-01 15:33:56 -050035#include "gr_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053036#include "gralloc_priv.h"
37
38#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053039
40#ifndef ION_FLAG_CP_PIXEL
41#define ION_FLAG_CP_PIXEL 0
42#endif
43
44#ifndef ION_FLAG_ALLOW_NON_CONTIG
45#define ION_FLAG_ALLOW_NON_CONTIG 0
46#endif
47
Sushil Chauhandfe55a22016-09-12 15:48:29 -070048#ifndef ION_FLAG_CP_CAMERA_PREVIEW
49#define ION_FLAG_CP_CAMERA_PREVIEW 0
50#endif
51
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053052#ifdef MASTER_SIDE_CP
53#define CP_HEAP_ID ION_SECURE_HEAP_ID
54#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
55#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
56#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070057#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
58#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053059#else // SLAVE_SIDE_CP
60#define CP_HEAP_ID ION_CP_MM_HEAP_ID
61#define SD_HEAP_ID CP_HEAP_ID
62#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
63#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070064#define ION_SC_FLAGS ION_SECURE
65#define ION_SC_PREVIEW_FLAGS ION_SECURE
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053066#endif
67
Naseer Ahmede69031e2016-11-22 20:05:16 -050068using std::shared_ptr;
Naseer Ahmeda422f352017-12-01 15:33:56 -050069using std::vector;
Naseer Ahmede69031e2016-11-22 20:05:16 -050070
Naseer Ahmeda422f352017-12-01 15:33:56 -050071namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053072
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070073static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
74 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmeda422f352017-12-01 15:33:56 -050075 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070076}
77
Naseer Ahmeda422f352017-12-01 15:33:56 -050078Allocator::Allocator() : ion_allocator_(nullptr) {}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079
80bool Allocator::Init() {
81 ion_allocator_ = new IonAlloc();
82 if (!ion_allocator_->Init()) {
83 return false;
84 }
85
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053086 return true;
87}
88
89Allocator::~Allocator() {
90 if (ion_allocator_) {
91 delete ion_allocator_;
92 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093}
94
Naseer Ahmeda422f352017-12-01 15:33:56 -050095int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096 int ret;
Naseer Ahmeda422f352017-12-01 15:33:56 -050097 alloc_data->uncached = UseUncached(usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053098
99 // After this point we should have the right heap set, there is no fallback
Naseer Ahmeda422f352017-12-01 15:33:56 -0500100 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530101
102 ret = ion_allocator_->AllocBuffer(alloc_data);
103 if (ret >= 0) {
104 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
105 } else {
106 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
107 alloc_data->heap_id, alloc_data->flags);
108 }
109
110 return ret;
111}
112
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530113int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
114 if (ion_allocator_) {
115 return ion_allocator_->MapBuffer(base, size, offset, fd);
116 }
117
118 return -EINVAL;
119}
120
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400121int Allocator::ImportBuffer(int fd) {
122 if (ion_allocator_) {
123 return ion_allocator_->ImportBuffer(fd);
124 }
125 return -EINVAL;
126}
127
Naseer Ahmeda422f352017-12-01 15:33:56 -0500128int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530129 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500130 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530131 }
132
133 return -EINVAL;
134}
135
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400136int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 if (ion_allocator_) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400138 return ion_allocator_->CleanBuffer(base, size, offset, handle, op);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530139 }
140
141 return -EINVAL;
142}
143
Naseer Ahmede69031e2016-11-22 20:05:16 -0500144bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
Naseer Ahmeda422f352017-12-01 15:33:56 -0500145 const vector<shared_ptr<BufferDescriptor>> &descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500146 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530147 unsigned int cur_heap_id = 0, prev_heap_id = 0;
148 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
149 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
150 bool cur_uncached = false, prev_uncached = false;
151 unsigned int alignedw, alignedh;
152 unsigned int max_size = 0;
153
154 *max_index = -1;
155 for (uint32_t i = 0; i < num_descriptors; i++) {
156 // Check Cached vs non-cached and all the ION flags
Naseer Ahmeda422f352017-12-01 15:33:56 -0500157 cur_uncached = UseUncached(descriptors[i]->GetUsage());
158 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530159
160 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
161 cur_ion_flags != prev_ion_flags)) {
162 return false;
163 }
164
165 // For same format type, find the descriptor with bigger size
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700166 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
167 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530168 if (max_size < size) {
169 *max_index = INT(i);
170 max_size = size;
171 }
172
173 prev_heap_id = cur_heap_id;
174 prev_uncached = cur_uncached;
175 prev_ion_flags = cur_ion_flags;
176 prev_alloc_type = cur_alloc_type;
177 }
178
179 return true;
180}
181
Naseer Ahmeda422f352017-12-01 15:33:56 -0500182int Allocator::GetImplDefinedFormat(uint64_t usage, int format) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530183 int gr_format = format;
184
185 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
186 // the usage bits, gralloc assigns a format.
187 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
188 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500189 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) {
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800190 // Use of 10BIT_TP and 10BIT bits is supposed to be mutually exclusive.
191 // Each bit maps to only one format. Here we will check one of the bits
192 // and ignore the other.
Naseer Ahmeda422f352017-12-01 15:33:56 -0500193 if (usage & GRALLOC_USAGE_PRIVATE_10BIT_TP) {
Rohit Kulkarni79f279c2017-06-28 14:25:25 -0700194 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500195 } else if (usage & GRALLOC_USAGE_PRIVATE_10BIT) {
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800196 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC;
Rohit Kulkarni79f279c2017-06-28 14:25:25 -0700197 } else {
198 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
199 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500200 } else if (usage & GRALLOC_USAGE_PRIVATE_10BIT) {
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800201 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500202 } else if (usage & BufferUsage::VIDEO_ENCODER) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530203 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
Naseer Ahmeda422f352017-12-01 15:33:56 -0500204 } else if (usage & BufferUsage::CAMERA_INPUT) {
205 if (usage & BufferUsage::CAMERA_OUTPUT) {
Naseer Ahmed1901fa32017-03-22 20:34:45 -0400206 // Assumed ZSL if both producer and consumer camera flags set
207 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
208 } else {
209 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
210 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500211 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530212 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
213 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
214 } else {
215 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
216 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500217 } else if (usage & BufferUsage::COMPOSER_OVERLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530218 // XXX: If we still haven't set a format, default to RGBA8888
219 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
220 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
221 // If no other usage flags are detected, default the
222 // flexible YUV format to NV21_ZSL
223 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
224 }
225 }
226
227 return gr_format;
228}
229
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530230/* The default policy is to return cached buffers unless the client explicity
231 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
232 * read or written in software. */
Naseer Ahmeda422f352017-12-01 15:33:56 -0500233bool Allocator::UseUncached(uint64_t usage) {
234 if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530235 return true;
236 }
237
238 // CPU read rarely
Naseer Ahmeda422f352017-12-01 15:33:56 -0500239 if ((usage & BufferUsage::CPU_READ_RARELY) && !(usage & BufferUsage::CPU_READ_OFTEN)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530240 return true;
241 }
242
243 // CPU write rarely
Naseer Ahmeda422f352017-12-01 15:33:56 -0500244 if ((usage & BufferUsage::CPU_WRITE_RARELY) && !(usage & BufferUsage::CPU_WRITE_OFTEN)) {
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400245 return true;
246 }
247
Naseer Ahmeda422f352017-12-01 15:33:56 -0500248 if ((usage & BufferUsage::SENSOR_DIRECT_DATA) || (usage & BufferUsage::GPU_DATA_BUFFER)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530249 return true;
250 }
251
252 return false;
253}
254
Naseer Ahmeda422f352017-12-01 15:33:56 -0500255void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
256 unsigned int *ion_flags) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530257 unsigned int heap_id = 0;
258 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400259 uint32_t flags = 0;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500260 if (usage & GRALLOC_USAGE_PROTECTED) {
261 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530262 heap_id = ION_HEAP(SD_HEAP_ID);
263 /*
264 * There is currently no flag in ION for Secure Display
265 * VM. Please add it to the define once available.
266 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400267 flags |= UINT(ION_SD_FLAGS);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500268 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700269 heap_id = ION_HEAP(SD_HEAP_ID);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500270 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400271 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700272 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400273 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700274 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530275 } else {
276 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400277 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530278 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530279 }
280
Naseer Ahmeda422f352017-12-01 15:33:56 -0500281 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530282 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
283 }
284
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400285 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530286 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
287 }
288
289 // if no ion heap flags are set, default to system heap
290 if (!heap_id) {
291 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
292 }
293
294 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500295 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530296 *ion_heap_id = heap_id;
297
298 return;
299}
Naseer Ahmeda422f352017-12-01 15:33:56 -0500300} // namespace gralloc