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