blob: 7a54187ef5377167edb7c0757bb9152ec733b5a6 [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
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
34#include "gr_utils.h"
35#include "gr_allocator.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
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070052#ifndef ION_SECURE
53#define ION_SECURE ION_FLAG_SECURE
54#endif
55
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053056#ifdef MASTER_SIDE_CP
57#define CP_HEAP_ID ION_SECURE_HEAP_ID
58#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
59#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
60#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070061#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
62#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053063#else // SLAVE_SIDE_CP
64#define CP_HEAP_ID ION_CP_MM_HEAP_ID
65#define SD_HEAP_ID CP_HEAP_ID
66#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
67#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070068#define ION_SC_FLAGS ION_SECURE
69#define ION_SC_PREVIEW_FLAGS ION_SECURE
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053070#endif
71
Naseer Ahmede69031e2016-11-22 20:05:16 -050072using std::vector;
73using std::shared_ptr;
74
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053075namespace gralloc1 {
76
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070077static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
78 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
79 descriptor.GetProducerUsage(), descriptor.GetConsumerUsage());
80}
81
82Allocator::Allocator() : ion_allocator_(NULL) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083}
84
85bool Allocator::Init() {
86 ion_allocator_ = new IonAlloc();
87 if (!ion_allocator_->Init()) {
88 return false;
89 }
90
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053091 return true;
92}
93
94Allocator::~Allocator() {
95 if (ion_allocator_) {
96 delete ion_allocator_;
97 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053098}
99
100int Allocator::AllocateMem(AllocData *alloc_data, gralloc1_producer_usage_t prod_usage,
101 gralloc1_consumer_usage_t cons_usage) {
102 int ret;
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400103 alloc_data->uncached = UseUncached(prod_usage, cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530104
105 // After this point we should have the right heap set, there is no fallback
106 GetIonHeapInfo(prod_usage, cons_usage, &alloc_data->heap_id, &alloc_data->alloc_type,
107 &alloc_data->flags);
108
109 ret = ion_allocator_->AllocBuffer(alloc_data);
110 if (ret >= 0) {
111 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
112 } else {
113 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
114 alloc_data->heap_id, alloc_data->flags);
115 }
116
117 return ret;
118}
119
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530120int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
121 if (ion_allocator_) {
122 return ion_allocator_->MapBuffer(base, size, offset, fd);
123 }
124
125 return -EINVAL;
126}
127
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400128int Allocator::ImportBuffer(int fd) {
129 if (ion_allocator_) {
130 return ion_allocator_->ImportBuffer(fd);
131 }
132 return -EINVAL;
133}
134
Naseer Ahmede69031e2016-11-22 20:05:16 -0500135int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
136 int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500138 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530139 }
140
141 return -EINVAL;
142}
143
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700144int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
145 int fd) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530146 if (ion_allocator_) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700147 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530148 }
149
150 return -EINVAL;
151}
152
Naseer Ahmede69031e2016-11-22 20:05:16 -0500153bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
154 const vector<shared_ptr<BufferDescriptor>>& descriptors,
155 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156 unsigned int cur_heap_id = 0, prev_heap_id = 0;
157 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
158 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
159 bool cur_uncached = false, prev_uncached = false;
160 unsigned int alignedw, alignedh;
161 unsigned int max_size = 0;
162
163 *max_index = -1;
164 for (uint32_t i = 0; i < num_descriptors; i++) {
165 // Check Cached vs non-cached and all the ION flags
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400166 cur_uncached = UseUncached(descriptors[i]->GetProducerUsage(),
167 descriptors[i]->GetConsumerUsage());
Naseer Ahmede69031e2016-11-22 20:05:16 -0500168 GetIonHeapInfo(descriptors[i]->GetProducerUsage(), descriptors[i]->GetConsumerUsage(),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530169 &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
170
171 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
172 cur_ion_flags != prev_ion_flags)) {
173 return false;
174 }
175
176 // For same format type, find the descriptor with bigger size
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700177 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
178 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179 if (max_size < size) {
180 *max_index = INT(i);
181 max_size = size;
182 }
183
184 prev_heap_id = cur_heap_id;
185 prev_uncached = cur_uncached;
186 prev_ion_flags = cur_ion_flags;
187 prev_alloc_type = cur_alloc_type;
188 }
189
190 return true;
191}
192
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530193int Allocator::GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
194 gralloc1_consumer_usage_t cons_usage, int format) {
195 int gr_format = format;
196
197 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
198 // the usage bits, gralloc assigns a format.
199 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
200 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
201 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) {
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800202 // Use of 10BIT_TP and 10BIT bits is supposed to be mutually exclusive.
203 // Each bit maps to only one format. Here we will check one of the bits
204 // and ignore the other.
Rohit Kulkarni79f279c2017-06-28 14:25:25 -0700205 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_10BIT_TP) {
206 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800207 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_10BIT) {
208 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC;
Rohit Kulkarni79f279c2017-06-28 14:25:25 -0700209 } else {
210 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
211 }
Rohit Kulkarnia50206a2017-11-13 15:55:24 -0800212 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_10BIT) {
213 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530214 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
Yahui Wang55563ca2018-01-15 19:50:05 +0800215 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_VIDEO_NV21_ENCODER) {
216 gr_format = HAL_PIXEL_FORMAT_NV21_ENCODEABLE; // NV21
217 } else {
218 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
219 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530220 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
Naseer Ahmed1901fa32017-03-22 20:34:45 -0400221 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
222 // Assumed ZSL if both producer and consumer camera flags set
223 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
224 } else {
225 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
226 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530227 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
228 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
229 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
230 } else {
231 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
232 }
233 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
234 // XXX: If we still haven't set a format, default to RGBA8888
235 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
236 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
237 // If no other usage flags are detected, default the
238 // flexible YUV format to NV21_ZSL
239 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
240 }
241 }
242
243 return gr_format;
244}
245
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530246/* The default policy is to return cached buffers unless the client explicity
247 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
248 * read or written in software. */
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400249bool Allocator::UseUncached(gralloc1_producer_usage_t prod_usage,
250 gralloc1_consumer_usage_t cons_usage) {
251 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
252 (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530253 return true;
254 }
255
256 // CPU read rarely
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400257 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
258 !(prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530259 return true;
260 }
261
262 // CPU write rarely
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400263 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
264 !(prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
265 return true;
266 }
267
268 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_SENSOR_DIRECT_DATA) ||
269 (cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_DATA_BUFFER)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530270 return true;
271 }
272
273 return false;
274}
275
276void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage,
277 gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id,
278 unsigned int *alloc_type, unsigned int *ion_flags) {
279 unsigned int heap_id = 0;
280 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400281 uint32_t flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530282 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
283 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
284 heap_id = ION_HEAP(SD_HEAP_ID);
285 /*
286 * There is currently no flag in ION for Secure Display
287 * VM. Please add it to the define once available.
288 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400289 flags |= UINT(ION_SD_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700290 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
291 heap_id = ION_HEAP(SD_HEAP_ID);
292 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400293 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700294 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400295 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700296 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530297 } else {
298 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400299 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530300 }
301 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) {
302 // MM Heap is exclusively a secure heap.
303 // If it is used for non secure cases, fallback to IOMMU heap
304 ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!");
305 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
306 }
307
Naseer Ahmed98de6d82017-05-02 21:10:29 -0400308 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP ||
309 prod_usage & GRALLOC1_PRODUCER_USAGE_SENSOR_DIRECT_DATA) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530310 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
311 }
312
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400313 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530314 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
315 }
316
317 // if no ion heap flags are set, default to system heap
318 if (!heap_id) {
319 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
320 }
321
322 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500323 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530324 *ion_heap_id = heap_id;
325
326 return;
327}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530328} // namespace gralloc1