blob: fc338322309377ed1497536cb3c48bad086ad072 [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:
Naseer Ahmed92998622017-03-20 12:39:17 -0400226 case HAL_PIXEL_FORMAT_RAW12:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530227 size = ALIGN(alignedw * alignedh, SIZE_4K);
228 break;
Naseer Ahmedbe5c2ef2017-03-07 10:09:11 -0500229 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800230 size = alignedw * alignedh * 1;
231 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530232
233 // adreno formats
234 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
235 size = ALIGN(alignedw * alignedh, SIZE_4K);
236 size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
237 break;
238 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
239 // The chroma plane is subsampled,
240 // but the pitch in bytes is unchanged
241 // The GPU needs 4K alignment, but the video decoder needs 8K
242 size = ALIGN(alignedw * alignedh, SIZE_8K);
243 size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K);
244 break;
245 case HAL_PIXEL_FORMAT_YV12:
246 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
247 ALOGE("w or h is odd for the YV12 format");
248 return 0;
249 }
250 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
251 size = ALIGN(size, (unsigned int)SIZE_4K);
252 break;
253 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
254 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
255 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
256 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800257 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
258 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
259 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530260 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
261 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
262 case HAL_PIXEL_FORMAT_YCbCr_422_I:
263 case HAL_PIXEL_FORMAT_YCrCb_422_I:
264 if (width & 1) {
265 ALOGE("width is odd for the YUV422_SP format");
266 return 0;
267 }
268 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
269 break;
270 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
271 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
272 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
273 break;
274 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
275 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
276 break;
277 case HAL_PIXEL_FORMAT_BLOB:
278 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
279 if (height != 1) {
280 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
281 return 0;
282 }
283 size = (unsigned int)width;
284 break;
285 case HAL_PIXEL_FORMAT_NV21_ZSL:
286 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K);
287 break;
288 default:
289 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
290 return 0;
291 }
292
293 return size;
294}
295
296void Allocator::GetBufferSizeAndDimensions(int width, int height, int format, unsigned int *size,
297 unsigned int *alignedw, unsigned int *alignedh) {
298 BufferDescriptor descriptor = BufferDescriptor(width, height, format);
299 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
300
301 *size = GetSize(descriptor, *alignedw, *alignedh);
302}
303
304void Allocator::GetBufferSizeAndDimensions(const BufferDescriptor &descriptor, unsigned int *size,
305 unsigned int *alignedw, unsigned int *alignedh) {
306 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
307
308 *size = GetSize(descriptor, *alignedw, *alignedh);
309}
310
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530311void Allocator::GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
312 int color_format, struct android_ycbcr *ycbcr) {
313 // UBWC buffer has these 4 planes in the following sequence:
314 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
315 unsigned int y_meta_stride, y_meta_height, y_meta_size;
316 unsigned int y_stride, y_height, y_size;
317 unsigned int c_meta_stride, c_meta_height, c_meta_size;
318 unsigned int alignment = 4096;
319
320 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
321 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
322 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
323
324 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
325 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
326 y_size = ALIGN((y_stride * y_height), alignment);
327
328 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
329 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
330 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
331
332 ycbcr->y = reinterpret_cast<void *>(base + y_meta_size);
333 ycbcr->cb = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size);
334 ycbcr->cr = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size + 1);
335 ycbcr->ystride = y_stride;
336 ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width));
337}
338
339void Allocator::GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp,
340 struct android_ycbcr *ycbcr) {
341 unsigned int ystride, cstride;
342
343 ystride = cstride = UINT(width) * bpp;
344 ycbcr->y = reinterpret_cast<void *>(base);
345 ycbcr->cb = reinterpret_cast<void *>(base + ystride * UINT(height));
346 ycbcr->cr = reinterpret_cast<void *>(base + ystride * UINT(height) + 1);
347 ycbcr->ystride = ystride;
348 ycbcr->cstride = cstride;
349 ycbcr->chroma_step = 2 * bpp;
350}
351
352int Allocator::GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr) {
353 int err = 0;
354 uint32_t width = UINT(hnd->width);
355 uint32_t height = UINT(hnd->height);
356 int format = hnd->format;
357 gralloc1_producer_usage_t prod_usage = hnd->GetProducerUsage();
358 gralloc1_consumer_usage_t cons_usage = hnd->GetConsumerUsage();
359 unsigned int ystride, cstride;
360
361 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
362 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
363
364 // Check if UBWC buffer has been rendered in linear format.
365 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
366 format = INT(metadata->linearFormat);
367 }
368
369 // Check metadata if the geometry has been updated.
370 if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
371 int usage = 0;
372
373 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
374 usage = GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC;
375 }
376
377 BufferDescriptor descriptor =
378 BufferDescriptor(metadata->bufferDim.sliceWidth, metadata->bufferDim.sliceHeight, format,
379 prod_usage, cons_usage);
380 GetAlignedWidthAndHeight(descriptor, &width, &height);
381 }
382
383 // Get the chroma offsets from the handle width/height. We take advantage
384 // of the fact the width _is_ the stride
385 switch (format) {
386 // Semiplanar
387 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
388 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
389 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
390 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
391 // Same as YCbCr_420_SP_VENUS
392 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
393 break;
394
395 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
396 GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
397 break;
398
399 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
400 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
401 ycbcr->chroma_step = 2;
402 break;
403
404 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
405 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
406 ycbcr->chroma_step = 3;
407 break;
408
409 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
410 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
411 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
412 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
413 case HAL_PIXEL_FORMAT_NV21_ZSL:
414 case HAL_PIXEL_FORMAT_RAW16:
415 case HAL_PIXEL_FORMAT_RAW10:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800416 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530417 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
418 std::swap(ycbcr->cb, ycbcr->cr);
419 break;
420
421 // Planar
422 case HAL_PIXEL_FORMAT_YV12:
423 ystride = width;
424 cstride = ALIGN(width / 2, 16);
425 ycbcr->y = reinterpret_cast<void *>(hnd->base);
426 ycbcr->cr = reinterpret_cast<void *>(hnd->base + ystride * height);
427 ycbcr->cb = reinterpret_cast<void *>(hnd->base + ystride * height + cstride * height / 2);
428 ycbcr->ystride = ystride;
429 ycbcr->cstride = cstride;
430 ycbcr->chroma_step = 1;
431 break;
432
433 // Unsupported formats
434 case HAL_PIXEL_FORMAT_YCbCr_422_I:
435 case HAL_PIXEL_FORMAT_YCrCb_422_I:
436 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
437 default:
438 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
439 err = -EINVAL;
440 }
441
442 return err;
443}
444
445int Allocator::GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
446 gralloc1_consumer_usage_t cons_usage, int format) {
447 int gr_format = format;
448
449 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
450 // the usage bits, gralloc assigns a format.
451 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
452 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
453 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) {
454 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
455 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
456 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530457 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
458 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
459 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
460 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
461 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
462 } else {
463 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
464 }
465 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
466 // XXX: If we still haven't set a format, default to RGBA8888
467 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
468 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
469 // If no other usage flags are detected, default the
470 // flexible YUV format to NV21_ZSL
471 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
472 }
473 }
474
475 return gr_format;
476}
477
478// Explicitly defined UBWC formats
479bool Allocator::IsUBwcFormat(int format) {
480 switch (format) {
481 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Arun Kumar K.R54885f02017-03-23 11:56:59 -0700482 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530483 return true;
484 default:
485 return false;
486 }
487}
488
489bool Allocator::IsUBwcSupported(int format) {
490 // Existing HAL formats with UBWC support
491 switch (format) {
492 case HAL_PIXEL_FORMAT_BGR_565:
493 case HAL_PIXEL_FORMAT_RGBA_8888:
494 case HAL_PIXEL_FORMAT_RGBX_8888:
495 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
496 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
497 return true;
498 default:
499 break;
500 }
501
502 return false;
503}
504
505/* The default policy is to return cached buffers unless the client explicity
506 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
507 * read or written in software. */
508// TODO(user) : As of now relying only on producer usage
509bool Allocator::UseUncached(gralloc1_producer_usage_t usage) {
510 if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
511 (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
512 return true;
513 }
514
515 // CPU read rarely
516 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
517 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
518 return true;
519 }
520
521 // CPU write rarely
522 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
523 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
524 return true;
525 }
526
527 return false;
528}
529
530void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage,
531 gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id,
532 unsigned int *alloc_type, unsigned int *ion_flags) {
533 unsigned int heap_id = 0;
534 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400535 uint32_t flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530536 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
537 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
538 heap_id = ION_HEAP(SD_HEAP_ID);
539 /*
540 * There is currently no flag in ION for Secure Display
541 * VM. Please add it to the define once available.
542 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400543 flags |= UINT(ION_SD_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700544 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
545 heap_id = ION_HEAP(SD_HEAP_ID);
546 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400547 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700548 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400549 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700550 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530551 } else {
552 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400553 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530554 }
555 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) {
556 // MM Heap is exclusively a secure heap.
557 // If it is used for non secure cases, fallback to IOMMU heap
558 ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!");
559 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
560 }
561
562 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP) {
563 heap_id |= ION_HEAP(ION_CAMERA_HEAP_ID);
564 }
565
566 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP) {
567 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
568 }
569
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400570 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530571 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
572 }
573
574 // if no ion heap flags are set, default to system heap
575 if (!heap_id) {
576 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
577 }
578
579 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500580 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530581 *ion_heap_id = heap_id;
582
583 return;
584}
585
586bool Allocator::IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage,
587 gralloc1_consumer_usage_t cons_usage) {
588 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
589 if (IsUBwcFormat(format)) {
590 return true;
591 }
592
593 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
594 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
595 // usage flag and MDP supports the format.
596 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
597 bool enable = true;
598 // Query GPU for UBWC only if buffer is intended to be used by GPU.
599 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) ||
600 (prod_usage & GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET)) {
601 enable = adreno_helper_->IsUBWCSupportedByGPU(format);
602 }
603
604 // Allow UBWC, only if CPU usage flags are not set
605 if (enable && !(CpuCanAccess(prod_usage, cons_usage))) {
606 return true;
607 }
608 }
609
610 return false;
611}
612
613void Allocator::GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
614 unsigned int *aligned_h) {
615 switch (format) {
616 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
617 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
618 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
619 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
620 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
621 break;
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700622 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
623 // The macro returns the stride which is 4/3 times the width, hence * 3/4
624 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
625 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
626 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530627 default:
628 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
629 *aligned_w = 0;
630 *aligned_h = 0;
631 break;
632 }
633}
634
635void Allocator::GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
636 *block_width = 0;
637 *block_height = 0;
638
639 switch (bpp) {
640 case 2:
641 case 4:
642 *block_width = 16;
643 *block_height = 4;
644 break;
645 case 8:
646 *block_width = 8;
647 *block_height = 4;
648 break;
649 case 16:
650 *block_width = 4;
651 *block_height = 4;
652 break;
653 default:
654 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
655 break;
656 }
657}
658
659unsigned int Allocator::GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
660 unsigned int size = 0;
661 int meta_width, meta_height;
662 int block_width, block_height;
663
664 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
665 if (!block_width || !block_height) {
666 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
667 return size;
668 }
669
670 // Align meta buffer height to 16 blocks
671 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
672
673 // Align meta buffer width to 64 blocks
674 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
675
676 // Align meta buffer size to 4K
677 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
678
679 return size;
680}
681
682unsigned int Allocator::GetUBwcSize(int width, int height, int format, unsigned int alignedw,
683 unsigned int alignedh) {
684 unsigned int size = 0;
685 uint32_t bpp = 0;
686 switch (format) {
687 case HAL_PIXEL_FORMAT_BGR_565:
688 case HAL_PIXEL_FORMAT_RGBA_8888:
689 case HAL_PIXEL_FORMAT_RGBX_8888:
690 case HAL_PIXEL_FORMAT_RGBA_1010102:
691 case HAL_PIXEL_FORMAT_RGBX_1010102:
692 bpp = GetBppForUncompressedRGB(format);
693 size = alignedw * alignedh * bpp;
694 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
695 break;
696 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
697 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
698 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
699 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
700 break;
701 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
702 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
703 break;
704 default:
705 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
706 break;
707 }
708
709 return size;
710}
711
712int Allocator::GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
713 int err = 0;
714
715 // This api is for RGB* formats
716 if (!gralloc1::IsUncompressedRGBFormat(hnd->format)) {
717 return -EINVAL;
718 }
719
720 // linear buffer, nothing to do further
721 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
722 *rgb_data = reinterpret_cast<void *>(hnd->base);
723 return err;
724 }
725
726 unsigned int meta_size = 0;
727 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
728 switch (hnd->format) {
729 case HAL_PIXEL_FORMAT_BGR_565:
730 case HAL_PIXEL_FORMAT_RGBA_8888:
731 case HAL_PIXEL_FORMAT_RGBX_8888:
732 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
733 break;
734 default:
735 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
736 err = -EINVAL;
737 break;
738 }
739 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
740
741 return err;
742}
743
744void Allocator::GetAlignedWidthAndHeight(const BufferDescriptor &descriptor, unsigned int *alignedw,
745 unsigned int *alignedh) {
746 int width = descriptor.GetWidth();
747 int height = descriptor.GetHeight();
748 int format = descriptor.GetFormat();
749 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
750 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
751
752 // Currently surface padding is only computed for RGB* surfaces.
753 bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700754 int tile = ubwc_enabled;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530755
756 if (IsUncompressedRGBFormat(format)) {
757 adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh);
758 return;
759 }
760
761 if (ubwc_enabled) {
762 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
763 return;
764 }
765
766 if (IsCompressedRGBFormat(format)) {
767 adreno_helper_->AlignCompressedRGB(width, height, format, alignedw, alignedh);
768 return;
769 }
770
771 int aligned_w = width;
772 int aligned_h = height;
773 unsigned int alignment = 32;
774
775 // Below should be only YUV family
776 switch (format) {
777 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
778 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
779 alignment = adreno_helper_->GetGpuPixelAlignment();
780 aligned_w = ALIGN(width, alignment);
781 break;
782 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
783 aligned_w = ALIGN(width, alignment);
784 break;
785 case HAL_PIXEL_FORMAT_RAW16:
786 aligned_w = ALIGN(width, 16);
787 break;
Naseer Ahmed92998622017-03-20 12:39:17 -0400788 case HAL_PIXEL_FORMAT_RAW12:
789 aligned_w = ALIGN(width * 12 / 8, 8);
790 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530791 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmeddfd5e332017-03-20 12:36:43 -0400792 aligned_w = ALIGN(width * 10 / 8, 8);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530793 break;
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800794 case HAL_PIXEL_FORMAT_RAW8:
795 aligned_w = ALIGN(width, 8);
796 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530797 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
798 aligned_w = ALIGN(width, 128);
799 break;
800 case HAL_PIXEL_FORMAT_YV12:
801 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
802 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
803 case HAL_PIXEL_FORMAT_YCbCr_422_I:
804 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800805 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530806 aligned_w = ALIGN(width, 16);
807 break;
808 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
809 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
810 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
811 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
812 break;
813 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
814 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
815 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
816 break;
817 case HAL_PIXEL_FORMAT_BLOB:
818 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
819 break;
820 case HAL_PIXEL_FORMAT_NV21_ZSL:
821 aligned_w = ALIGN(width, 64);
822 aligned_h = ALIGN(height, 64);
823 break;
824 default:
825 break;
826 }
827
828 *alignedw = (unsigned int)aligned_w;
829 *alignedh = (unsigned int)aligned_h;
830}
831
832} // namespace gralloc1