blob: c044827061e697b9f7f667c5281fef3de576482f [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Saurabh Shahc5b2b702016-10-24 17:16:01 -07002 * Copyright (c) 2011-2017, 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"
36#include "gr_adreno_info.h"
37#include "gralloc_priv.h"
38
39#include "qd_utils.h"
40#include "qdMetaData.h"
41
42#define ASTC_BLOCK_SIZE 16
43
44#ifndef ION_FLAG_CP_PIXEL
45#define ION_FLAG_CP_PIXEL 0
46#endif
47
48#ifndef ION_FLAG_ALLOW_NON_CONTIG
49#define ION_FLAG_ALLOW_NON_CONTIG 0
50#endif
51
Sushil Chauhandfe55a22016-09-12 15:48:29 -070052#ifndef ION_FLAG_CP_CAMERA_PREVIEW
53#define ION_FLAG_CP_CAMERA_PREVIEW 0
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
77Allocator::Allocator() : ion_allocator_(NULL), adreno_helper_(NULL) {
78}
79
80bool Allocator::Init() {
81 ion_allocator_ = new IonAlloc();
82 if (!ion_allocator_->Init()) {
83 return false;
84 }
85
86 adreno_helper_ = new AdrenoMemInfo();
87 if (!adreno_helper_->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 }
98
99 if (adreno_helper_) {
100 delete adreno_helper_;
101 }
102}
103
104int Allocator::AllocateMem(AllocData *alloc_data, gralloc1_producer_usage_t prod_usage,
105 gralloc1_consumer_usage_t cons_usage) {
106 int ret;
107 alloc_data->uncached = UseUncached(prod_usage);
108
109 // After this point we should have the right heap set, there is no fallback
110 GetIonHeapInfo(prod_usage, cons_usage, &alloc_data->heap_id, &alloc_data->alloc_type,
111 &alloc_data->flags);
112
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 Ahmede69031e2016-11-22 20:05:16 -0500139int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
140 int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530141 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500142 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530143 }
144
145 return -EINVAL;
146}
147
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400148int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530149 if (ion_allocator_) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400150 return ion_allocator_->CleanBuffer(base, size, offset, handle, op);
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,
157 const vector<shared_ptr<BufferDescriptor>>& descriptors,
158 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
Naseer Ahmede69031e2016-11-22 20:05:16 -0500169 cur_uncached = UseUncached(descriptors[i]->GetProducerUsage());
170 GetIonHeapInfo(descriptors[i]->GetProducerUsage(), descriptors[i]->GetConsumerUsage(),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530171 &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
172
173 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
174 cur_ion_flags != prev_ion_flags)) {
175 return false;
176 }
177
178 // For same format type, find the descriptor with bigger size
Naseer Ahmede69031e2016-11-22 20:05:16 -0500179 GetAlignedWidthAndHeight(*descriptors[i], &alignedw, &alignedh);
180 unsigned int size = GetSize(*descriptors[i], alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530181 if (max_size < size) {
182 *max_index = INT(i);
183 max_size = size;
184 }
185
186 prev_heap_id = cur_heap_id;
187 prev_uncached = cur_uncached;
188 prev_ion_flags = cur_ion_flags;
189 prev_alloc_type = cur_alloc_type;
190 }
191
192 return true;
193}
194
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530195// helper function
196unsigned int Allocator::GetSize(const BufferDescriptor &descriptor, unsigned int alignedw,
197 unsigned int alignedh) {
198 unsigned int size = 0;
199 int format = descriptor.GetFormat();
200 int width = descriptor.GetWidth();
201 int height = descriptor.GetHeight();
202 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
203 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
204
205 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
206 return GetUBwcSize(width, height, format, alignedw, alignedh);
207 }
208
209 if (IsUncompressedRGBFormat(format)) {
210 uint32_t bpp = GetBppForUncompressedRGB(format);
211 size = alignedw * alignedh * bpp;
212 return size;
213 }
214
215 if (IsCompressedRGBFormat(format)) {
216 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
217 return size;
218 }
219
220 // Below switch should be for only YUV/custom formats
221 switch (format) {
222 case HAL_PIXEL_FORMAT_RAW16:
223 size = alignedw * alignedh * 2;
224 break;
225 case HAL_PIXEL_FORMAT_RAW10:
226 size = ALIGN(alignedw * alignedh, SIZE_4K);
227 break;
Naseer Ahmedbe5c2ef2017-03-07 10:09:11 -0500228 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800229 size = alignedw * alignedh * 1;
230 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231
232 // adreno formats
233 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
234 size = ALIGN(alignedw * alignedh, SIZE_4K);
235 size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
236 break;
237 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
238 // The chroma plane is subsampled,
239 // but the pitch in bytes is unchanged
240 // The GPU needs 4K alignment, but the video decoder needs 8K
241 size = ALIGN(alignedw * alignedh, SIZE_8K);
242 size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K);
243 break;
244 case HAL_PIXEL_FORMAT_YV12:
245 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
246 ALOGE("w or h is odd for the YV12 format");
247 return 0;
248 }
249 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
250 size = ALIGN(size, (unsigned int)SIZE_4K);
251 break;
252 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
253 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
254 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
255 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800256 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
257 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
258 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530259 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
260 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
261 case HAL_PIXEL_FORMAT_YCbCr_422_I:
262 case HAL_PIXEL_FORMAT_YCrCb_422_I:
263 if (width & 1) {
264 ALOGE("width is odd for the YUV422_SP format");
265 return 0;
266 }
267 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
268 break;
269 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
270 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
271 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
272 break;
273 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
274 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
275 break;
276 case HAL_PIXEL_FORMAT_BLOB:
277 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
278 if (height != 1) {
279 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
280 return 0;
281 }
282 size = (unsigned int)width;
283 break;
284 case HAL_PIXEL_FORMAT_NV21_ZSL:
285 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K);
286 break;
287 default:
288 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
289 return 0;
290 }
291
292 return size;
293}
294
295void Allocator::GetBufferSizeAndDimensions(int width, int height, int format, unsigned int *size,
296 unsigned int *alignedw, unsigned int *alignedh) {
297 BufferDescriptor descriptor = BufferDescriptor(width, height, format);
298 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
299
300 *size = GetSize(descriptor, *alignedw, *alignedh);
301}
302
303void Allocator::GetBufferSizeAndDimensions(const BufferDescriptor &descriptor, unsigned int *size,
304 unsigned int *alignedw, unsigned int *alignedh) {
305 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
306
307 *size = GetSize(descriptor, *alignedw, *alignedh);
308}
309
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530310void Allocator::GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
311 int color_format, struct android_ycbcr *ycbcr) {
312 // UBWC buffer has these 4 planes in the following sequence:
313 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
314 unsigned int y_meta_stride, y_meta_height, y_meta_size;
315 unsigned int y_stride, y_height, y_size;
316 unsigned int c_meta_stride, c_meta_height, c_meta_size;
317 unsigned int alignment = 4096;
318
319 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
320 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
321 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
322
323 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
324 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
325 y_size = ALIGN((y_stride * y_height), alignment);
326
327 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
328 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
329 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
330
331 ycbcr->y = reinterpret_cast<void *>(base + y_meta_size);
332 ycbcr->cb = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size);
333 ycbcr->cr = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size + 1);
334 ycbcr->ystride = y_stride;
335 ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width));
336}
337
338void Allocator::GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp,
339 struct android_ycbcr *ycbcr) {
340 unsigned int ystride, cstride;
341
342 ystride = cstride = UINT(width) * bpp;
343 ycbcr->y = reinterpret_cast<void *>(base);
344 ycbcr->cb = reinterpret_cast<void *>(base + ystride * UINT(height));
345 ycbcr->cr = reinterpret_cast<void *>(base + ystride * UINT(height) + 1);
346 ycbcr->ystride = ystride;
347 ycbcr->cstride = cstride;
348 ycbcr->chroma_step = 2 * bpp;
349}
350
351int Allocator::GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr) {
352 int err = 0;
353 uint32_t width = UINT(hnd->width);
354 uint32_t height = UINT(hnd->height);
355 int format = hnd->format;
356 gralloc1_producer_usage_t prod_usage = hnd->GetProducerUsage();
357 gralloc1_consumer_usage_t cons_usage = hnd->GetConsumerUsage();
358 unsigned int ystride, cstride;
359
360 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
361 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
362
363 // Check if UBWC buffer has been rendered in linear format.
364 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
365 format = INT(metadata->linearFormat);
366 }
367
368 // Check metadata if the geometry has been updated.
369 if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
370 int usage = 0;
371
372 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
373 usage = GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC;
374 }
375
376 BufferDescriptor descriptor =
377 BufferDescriptor(metadata->bufferDim.sliceWidth, metadata->bufferDim.sliceHeight, format,
378 prod_usage, cons_usage);
379 GetAlignedWidthAndHeight(descriptor, &width, &height);
380 }
381
382 // Get the chroma offsets from the handle width/height. We take advantage
383 // of the fact the width _is_ the stride
384 switch (format) {
385 // Semiplanar
386 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
387 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
388 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
389 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
390 // Same as YCbCr_420_SP_VENUS
391 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
392 break;
393
394 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
395 GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
396 break;
397
398 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
399 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
400 ycbcr->chroma_step = 2;
401 break;
402
403 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
404 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
405 ycbcr->chroma_step = 3;
406 break;
407
408 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
409 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
410 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
411 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
412 case HAL_PIXEL_FORMAT_NV21_ZSL:
413 case HAL_PIXEL_FORMAT_RAW16:
414 case HAL_PIXEL_FORMAT_RAW10:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800415 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530416 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
417 std::swap(ycbcr->cb, ycbcr->cr);
418 break;
419
420 // Planar
421 case HAL_PIXEL_FORMAT_YV12:
422 ystride = width;
423 cstride = ALIGN(width / 2, 16);
424 ycbcr->y = reinterpret_cast<void *>(hnd->base);
425 ycbcr->cr = reinterpret_cast<void *>(hnd->base + ystride * height);
426 ycbcr->cb = reinterpret_cast<void *>(hnd->base + ystride * height + cstride * height / 2);
427 ycbcr->ystride = ystride;
428 ycbcr->cstride = cstride;
429 ycbcr->chroma_step = 1;
430 break;
431
432 // Unsupported formats
433 case HAL_PIXEL_FORMAT_YCbCr_422_I:
434 case HAL_PIXEL_FORMAT_YCrCb_422_I:
435 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
436 default:
437 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
438 err = -EINVAL;
439 }
440
441 return err;
442}
443
444int Allocator::GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
445 gralloc1_consumer_usage_t cons_usage, int format) {
446 int gr_format = format;
447
448 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
449 // the usage bits, gralloc assigns a format.
450 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
451 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
452 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) {
453 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
454 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
455 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530456 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
457 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
458 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
459 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
460 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
461 } else {
462 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
463 }
464 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
465 // XXX: If we still haven't set a format, default to RGBA8888
466 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
467 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
468 // If no other usage flags are detected, default the
469 // flexible YUV format to NV21_ZSL
470 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
471 }
472 }
473
474 return gr_format;
475}
476
477// Explicitly defined UBWC formats
478bool Allocator::IsUBwcFormat(int format) {
479 switch (format) {
480 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
481 return true;
482 default:
483 return false;
484 }
485}
486
487bool Allocator::IsUBwcSupported(int format) {
488 // Existing HAL formats with UBWC support
489 switch (format) {
490 case HAL_PIXEL_FORMAT_BGR_565:
491 case HAL_PIXEL_FORMAT_RGBA_8888:
492 case HAL_PIXEL_FORMAT_RGBX_8888:
493 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
494 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
495 return true;
496 default:
497 break;
498 }
499
500 return false;
501}
502
503/* The default policy is to return cached buffers unless the client explicity
504 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
505 * read or written in software. */
506// TODO(user) : As of now relying only on producer usage
507bool Allocator::UseUncached(gralloc1_producer_usage_t usage) {
508 if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
509 (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
510 return true;
511 }
512
513 // CPU read rarely
514 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
515 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
516 return true;
517 }
518
519 // CPU write rarely
520 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
521 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
522 return true;
523 }
524
525 return false;
526}
527
528void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage,
529 gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id,
530 unsigned int *alloc_type, unsigned int *ion_flags) {
531 unsigned int heap_id = 0;
532 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400533 uint32_t flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530534 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
535 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
536 heap_id = ION_HEAP(SD_HEAP_ID);
537 /*
538 * There is currently no flag in ION for Secure Display
539 * VM. Please add it to the define once available.
540 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400541 flags |= UINT(ION_SD_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700542 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
543 heap_id = ION_HEAP(SD_HEAP_ID);
544 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400545 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700546 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400547 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700548 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530549 } else {
550 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400551 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530552 }
553 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) {
554 // MM Heap is exclusively a secure heap.
555 // If it is used for non secure cases, fallback to IOMMU heap
556 ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!");
557 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
558 }
559
560 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP) {
561 heap_id |= ION_HEAP(ION_CAMERA_HEAP_ID);
562 }
563
564 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP) {
565 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
566 }
567
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400568 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530569 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
570 }
571
572 // if no ion heap flags are set, default to system heap
573 if (!heap_id) {
574 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
575 }
576
577 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500578 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530579 *ion_heap_id = heap_id;
580
581 return;
582}
583
584bool Allocator::IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage,
585 gralloc1_consumer_usage_t cons_usage) {
586 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
587 if (IsUBwcFormat(format)) {
588 return true;
589 }
590
591 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
592 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
593 // usage flag and MDP supports the format.
594 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
595 bool enable = true;
596 // Query GPU for UBWC only if buffer is intended to be used by GPU.
597 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) ||
598 (prod_usage & GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET)) {
599 enable = adreno_helper_->IsUBWCSupportedByGPU(format);
600 }
601
602 // Allow UBWC, only if CPU usage flags are not set
603 if (enable && !(CpuCanAccess(prod_usage, cons_usage))) {
604 return true;
605 }
606 }
607
608 return false;
609}
610
611void Allocator::GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
612 unsigned int *aligned_h) {
613 switch (format) {
614 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
615 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
616 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
617 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
618 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
619 break;
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700620 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
621 // The macro returns the stride which is 4/3 times the width, hence * 3/4
622 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
623 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
624 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530625 default:
626 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
627 *aligned_w = 0;
628 *aligned_h = 0;
629 break;
630 }
631}
632
633void Allocator::GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
634 *block_width = 0;
635 *block_height = 0;
636
637 switch (bpp) {
638 case 2:
639 case 4:
640 *block_width = 16;
641 *block_height = 4;
642 break;
643 case 8:
644 *block_width = 8;
645 *block_height = 4;
646 break;
647 case 16:
648 *block_width = 4;
649 *block_height = 4;
650 break;
651 default:
652 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
653 break;
654 }
655}
656
657unsigned int Allocator::GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
658 unsigned int size = 0;
659 int meta_width, meta_height;
660 int block_width, block_height;
661
662 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
663 if (!block_width || !block_height) {
664 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
665 return size;
666 }
667
668 // Align meta buffer height to 16 blocks
669 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
670
671 // Align meta buffer width to 64 blocks
672 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
673
674 // Align meta buffer size to 4K
675 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
676
677 return size;
678}
679
680unsigned int Allocator::GetUBwcSize(int width, int height, int format, unsigned int alignedw,
681 unsigned int alignedh) {
682 unsigned int size = 0;
683 uint32_t bpp = 0;
684 switch (format) {
685 case HAL_PIXEL_FORMAT_BGR_565:
686 case HAL_PIXEL_FORMAT_RGBA_8888:
687 case HAL_PIXEL_FORMAT_RGBX_8888:
688 case HAL_PIXEL_FORMAT_RGBA_1010102:
689 case HAL_PIXEL_FORMAT_RGBX_1010102:
690 bpp = GetBppForUncompressedRGB(format);
691 size = alignedw * alignedh * bpp;
692 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
693 break;
694 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
695 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
696 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
697 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
698 break;
699 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
700 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
701 break;
702 default:
703 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
704 break;
705 }
706
707 return size;
708}
709
710int Allocator::GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
711 int err = 0;
712
713 // This api is for RGB* formats
714 if (!gralloc1::IsUncompressedRGBFormat(hnd->format)) {
715 return -EINVAL;
716 }
717
718 // linear buffer, nothing to do further
719 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
720 *rgb_data = reinterpret_cast<void *>(hnd->base);
721 return err;
722 }
723
724 unsigned int meta_size = 0;
725 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
726 switch (hnd->format) {
727 case HAL_PIXEL_FORMAT_BGR_565:
728 case HAL_PIXEL_FORMAT_RGBA_8888:
729 case HAL_PIXEL_FORMAT_RGBX_8888:
730 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
731 break;
732 default:
733 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
734 err = -EINVAL;
735 break;
736 }
737 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
738
739 return err;
740}
741
742void Allocator::GetAlignedWidthAndHeight(const BufferDescriptor &descriptor, unsigned int *alignedw,
743 unsigned int *alignedh) {
744 int width = descriptor.GetWidth();
745 int height = descriptor.GetHeight();
746 int format = descriptor.GetFormat();
747 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
748 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
749
750 // Currently surface padding is only computed for RGB* surfaces.
751 bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700752 int tile = ubwc_enabled;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530753
754 if (IsUncompressedRGBFormat(format)) {
755 adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh);
756 return;
757 }
758
759 if (ubwc_enabled) {
760 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
761 return;
762 }
763
764 if (IsCompressedRGBFormat(format)) {
765 adreno_helper_->AlignCompressedRGB(width, height, format, alignedw, alignedh);
766 return;
767 }
768
769 int aligned_w = width;
770 int aligned_h = height;
771 unsigned int alignment = 32;
772
773 // Below should be only YUV family
774 switch (format) {
775 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
776 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
777 alignment = adreno_helper_->GetGpuPixelAlignment();
778 aligned_w = ALIGN(width, alignment);
779 break;
780 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
781 aligned_w = ALIGN(width, alignment);
782 break;
783 case HAL_PIXEL_FORMAT_RAW16:
784 aligned_w = ALIGN(width, 16);
785 break;
786 case HAL_PIXEL_FORMAT_RAW10:
787 aligned_w = ALIGN(width * 10 / 8, 16);
788 break;
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800789 case HAL_PIXEL_FORMAT_RAW8:
790 aligned_w = ALIGN(width, 8);
791 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530792 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
793 aligned_w = ALIGN(width, 128);
794 break;
795 case HAL_PIXEL_FORMAT_YV12:
796 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
797 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
798 case HAL_PIXEL_FORMAT_YCbCr_422_I:
799 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800800 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530801 aligned_w = ALIGN(width, 16);
802 break;
803 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
804 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
805 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
806 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
807 break;
808 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
809 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
810 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
811 break;
812 case HAL_PIXEL_FORMAT_BLOB:
813 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
814 break;
815 case HAL_PIXEL_FORMAT_NV21_ZSL:
816 aligned_w = ALIGN(width, 64);
817 aligned_h = ALIGN(height, 64);
818 break;
819 default:
820 break;
821 }
822
823 *alignedw = (unsigned int)aligned_w;
824 *alignedh = (unsigned int)aligned_h;
825}
826
827} // namespace gralloc1