blob: 577819cdc7ff4fa690497827dd38cd0466ef96b7 [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:
482 return true;
483 default:
484 return false;
485 }
486}
487
488bool Allocator::IsUBwcSupported(int format) {
489 // Existing HAL formats with UBWC support
490 switch (format) {
491 case HAL_PIXEL_FORMAT_BGR_565:
492 case HAL_PIXEL_FORMAT_RGBA_8888:
493 case HAL_PIXEL_FORMAT_RGBX_8888:
494 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
495 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
496 return true;
497 default:
498 break;
499 }
500
501 return false;
502}
503
504/* The default policy is to return cached buffers unless the client explicity
505 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
506 * read or written in software. */
507// TODO(user) : As of now relying only on producer usage
508bool Allocator::UseUncached(gralloc1_producer_usage_t usage) {
509 if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
510 (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
511 return true;
512 }
513
514 // CPU read rarely
515 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
516 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
517 return true;
518 }
519
520 // CPU write rarely
521 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
522 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
523 return true;
524 }
525
526 return false;
527}
528
529void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage,
530 gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id,
531 unsigned int *alloc_type, unsigned int *ion_flags) {
532 unsigned int heap_id = 0;
533 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400534 uint32_t flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530535 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
536 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
537 heap_id = ION_HEAP(SD_HEAP_ID);
538 /*
539 * There is currently no flag in ION for Secure Display
540 * VM. Please add it to the define once available.
541 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400542 flags |= UINT(ION_SD_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700543 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
544 heap_id = ION_HEAP(SD_HEAP_ID);
545 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400546 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700547 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400548 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700549 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530550 } else {
551 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400552 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530553 }
554 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) {
555 // MM Heap is exclusively a secure heap.
556 // If it is used for non secure cases, fallback to IOMMU heap
557 ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!");
558 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
559 }
560
561 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP) {
562 heap_id |= ION_HEAP(ION_CAMERA_HEAP_ID);
563 }
564
565 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP) {
566 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
567 }
568
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400569 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530570 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
571 }
572
573 // if no ion heap flags are set, default to system heap
574 if (!heap_id) {
575 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
576 }
577
578 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500579 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530580 *ion_heap_id = heap_id;
581
582 return;
583}
584
585bool Allocator::IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage,
586 gralloc1_consumer_usage_t cons_usage) {
587 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
588 if (IsUBwcFormat(format)) {
589 return true;
590 }
591
592 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
593 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
594 // usage flag and MDP supports the format.
595 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
596 bool enable = true;
597 // Query GPU for UBWC only if buffer is intended to be used by GPU.
598 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) ||
599 (prod_usage & GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET)) {
600 enable = adreno_helper_->IsUBWCSupportedByGPU(format);
601 }
602
603 // Allow UBWC, only if CPU usage flags are not set
604 if (enable && !(CpuCanAccess(prod_usage, cons_usage))) {
605 return true;
606 }
607 }
608
609 return false;
610}
611
612void Allocator::GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
613 unsigned int *aligned_h) {
614 switch (format) {
615 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
616 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
617 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
618 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
619 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
620 break;
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700621 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
622 // The macro returns the stride which is 4/3 times the width, hence * 3/4
623 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
624 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
625 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530626 default:
627 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
628 *aligned_w = 0;
629 *aligned_h = 0;
630 break;
631 }
632}
633
634void Allocator::GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
635 *block_width = 0;
636 *block_height = 0;
637
638 switch (bpp) {
639 case 2:
640 case 4:
641 *block_width = 16;
642 *block_height = 4;
643 break;
644 case 8:
645 *block_width = 8;
646 *block_height = 4;
647 break;
648 case 16:
649 *block_width = 4;
650 *block_height = 4;
651 break;
652 default:
653 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
654 break;
655 }
656}
657
658unsigned int Allocator::GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
659 unsigned int size = 0;
660 int meta_width, meta_height;
661 int block_width, block_height;
662
663 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
664 if (!block_width || !block_height) {
665 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
666 return size;
667 }
668
669 // Align meta buffer height to 16 blocks
670 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
671
672 // Align meta buffer width to 64 blocks
673 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
674
675 // Align meta buffer size to 4K
676 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
677
678 return size;
679}
680
681unsigned int Allocator::GetUBwcSize(int width, int height, int format, unsigned int alignedw,
682 unsigned int alignedh) {
683 unsigned int size = 0;
684 uint32_t bpp = 0;
685 switch (format) {
686 case HAL_PIXEL_FORMAT_BGR_565:
687 case HAL_PIXEL_FORMAT_RGBA_8888:
688 case HAL_PIXEL_FORMAT_RGBX_8888:
689 case HAL_PIXEL_FORMAT_RGBA_1010102:
690 case HAL_PIXEL_FORMAT_RGBX_1010102:
691 bpp = GetBppForUncompressedRGB(format);
692 size = alignedw * alignedh * bpp;
693 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
694 break;
695 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
696 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
697 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
698 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
699 break;
700 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
701 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
702 break;
703 default:
704 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
705 break;
706 }
707
708 return size;
709}
710
711int Allocator::GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
712 int err = 0;
713
714 // This api is for RGB* formats
715 if (!gralloc1::IsUncompressedRGBFormat(hnd->format)) {
716 return -EINVAL;
717 }
718
719 // linear buffer, nothing to do further
720 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
721 *rgb_data = reinterpret_cast<void *>(hnd->base);
722 return err;
723 }
724
725 unsigned int meta_size = 0;
726 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
727 switch (hnd->format) {
728 case HAL_PIXEL_FORMAT_BGR_565:
729 case HAL_PIXEL_FORMAT_RGBA_8888:
730 case HAL_PIXEL_FORMAT_RGBX_8888:
731 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
732 break;
733 default:
734 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
735 err = -EINVAL;
736 break;
737 }
738 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
739
740 return err;
741}
742
743void Allocator::GetAlignedWidthAndHeight(const BufferDescriptor &descriptor, unsigned int *alignedw,
744 unsigned int *alignedh) {
745 int width = descriptor.GetWidth();
746 int height = descriptor.GetHeight();
747 int format = descriptor.GetFormat();
748 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
749 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
750
751 // Currently surface padding is only computed for RGB* surfaces.
752 bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700753 int tile = ubwc_enabled;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530754
755 if (IsUncompressedRGBFormat(format)) {
756 adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh);
757 return;
758 }
759
760 if (ubwc_enabled) {
761 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
762 return;
763 }
764
765 if (IsCompressedRGBFormat(format)) {
766 adreno_helper_->AlignCompressedRGB(width, height, format, alignedw, alignedh);
767 return;
768 }
769
770 int aligned_w = width;
771 int aligned_h = height;
772 unsigned int alignment = 32;
773
774 // Below should be only YUV family
775 switch (format) {
776 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
777 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
778 alignment = adreno_helper_->GetGpuPixelAlignment();
779 aligned_w = ALIGN(width, alignment);
780 break;
781 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
782 aligned_w = ALIGN(width, alignment);
783 break;
784 case HAL_PIXEL_FORMAT_RAW16:
785 aligned_w = ALIGN(width, 16);
786 break;
Naseer Ahmed92998622017-03-20 12:39:17 -0400787 case HAL_PIXEL_FORMAT_RAW12:
788 aligned_w = ALIGN(width * 12 / 8, 8);
789 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530790 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmeddfd5e332017-03-20 12:36:43 -0400791 aligned_w = ALIGN(width * 10 / 8, 8);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530792 break;
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800793 case HAL_PIXEL_FORMAT_RAW8:
794 aligned_w = ALIGN(width, 8);
795 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530796 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
797 aligned_w = ALIGN(width, 128);
798 break;
799 case HAL_PIXEL_FORMAT_YV12:
800 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
801 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
802 case HAL_PIXEL_FORMAT_YCbCr_422_I:
803 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800804 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530805 aligned_w = ALIGN(width, 16);
806 break;
807 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
808 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
809 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
810 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
811 break;
812 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
813 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
814 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
815 break;
816 case HAL_PIXEL_FORMAT_BLOB:
817 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
818 break;
819 case HAL_PIXEL_FORMAT_NV21_ZSL:
820 aligned_w = ALIGN(width, 64);
821 aligned_h = ALIGN(height, 64);
822 break;
823 default:
824 break;
825 }
826
827 *alignedw = (unsigned int)aligned_w;
828 *alignedh = (unsigned int)aligned_h;
829}
830
831} // namespace gralloc1