blob: ddcf41dbfded82c6ff915227671c273d6bbcfcc4 [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
Rohit Kulkarni7943ec92016-12-20 18:18:46 -080072#ifndef COLOR_FMT_P010_UBWC
73#define COLOR_FMT_P010_UBWC 9
74#endif
75
Naseer Ahmede69031e2016-11-22 20:05:16 -050076using std::vector;
77using std::shared_ptr;
78
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079namespace gralloc1 {
80
81Allocator::Allocator() : ion_allocator_(NULL), adreno_helper_(NULL) {
82}
83
84bool Allocator::Init() {
85 ion_allocator_ = new IonAlloc();
86 if (!ion_allocator_->Init()) {
87 return false;
88 }
89
90 adreno_helper_ = new AdrenoMemInfo();
91 if (!adreno_helper_->Init()) {
92 return false;
93 }
94
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053095 return true;
96}
97
98Allocator::~Allocator() {
99 if (ion_allocator_) {
100 delete ion_allocator_;
101 }
102
103 if (adreno_helper_) {
104 delete adreno_helper_;
105 }
106}
107
108int Allocator::AllocateMem(AllocData *alloc_data, gralloc1_producer_usage_t prod_usage,
109 gralloc1_consumer_usage_t cons_usage) {
110 int ret;
111 alloc_data->uncached = UseUncached(prod_usage);
112
113 // After this point we should have the right heap set, there is no fallback
114 GetIonHeapInfo(prod_usage, cons_usage, &alloc_data->heap_id, &alloc_data->alloc_type,
115 &alloc_data->flags);
116
117 ret = ion_allocator_->AllocBuffer(alloc_data);
118 if (ret >= 0) {
119 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
120 } else {
121 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
122 alloc_data->heap_id, alloc_data->flags);
123 }
124
125 return ret;
126}
127
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530128int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
129 if (ion_allocator_) {
130 return ion_allocator_->MapBuffer(base, size, offset, fd);
131 }
132
133 return -EINVAL;
134}
135
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400136int Allocator::ImportBuffer(int fd) {
137 if (ion_allocator_) {
138 return ion_allocator_->ImportBuffer(fd);
139 }
140 return -EINVAL;
141}
142
Naseer Ahmede69031e2016-11-22 20:05:16 -0500143int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
144 int handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530145 if (ion_allocator_) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500146 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530147 }
148
149 return -EINVAL;
150}
151
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400152int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530153 if (ion_allocator_) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400154 return ion_allocator_->CleanBuffer(base, size, offset, handle, op);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530155 }
156
157 return -EINVAL;
158}
159
Naseer Ahmede69031e2016-11-22 20:05:16 -0500160bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
161 const vector<shared_ptr<BufferDescriptor>>& descriptors,
162 ssize_t *max_index) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530163 unsigned int cur_heap_id = 0, prev_heap_id = 0;
164 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
165 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
166 bool cur_uncached = false, prev_uncached = false;
167 unsigned int alignedw, alignedh;
168 unsigned int max_size = 0;
169
170 *max_index = -1;
171 for (uint32_t i = 0; i < num_descriptors; i++) {
172 // Check Cached vs non-cached and all the ION flags
Naseer Ahmede69031e2016-11-22 20:05:16 -0500173 cur_uncached = UseUncached(descriptors[i]->GetProducerUsage());
174 GetIonHeapInfo(descriptors[i]->GetProducerUsage(), descriptors[i]->GetConsumerUsage(),
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530175 &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
176
177 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
178 cur_ion_flags != prev_ion_flags)) {
179 return false;
180 }
181
182 // For same format type, find the descriptor with bigger size
Naseer Ahmede69031e2016-11-22 20:05:16 -0500183 GetAlignedWidthAndHeight(*descriptors[i], &alignedw, &alignedh);
184 unsigned int size = GetSize(*descriptors[i], alignedw, alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530185 if (max_size < size) {
186 *max_index = INT(i);
187 max_size = size;
188 }
189
190 prev_heap_id = cur_heap_id;
191 prev_uncached = cur_uncached;
192 prev_ion_flags = cur_ion_flags;
193 prev_alloc_type = cur_alloc_type;
194 }
195
196 return true;
197}
198
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530199// helper function
200unsigned int Allocator::GetSize(const BufferDescriptor &descriptor, unsigned int alignedw,
201 unsigned int alignedh) {
202 unsigned int size = 0;
203 int format = descriptor.GetFormat();
204 int width = descriptor.GetWidth();
205 int height = descriptor.GetHeight();
206 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
207 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
208
209 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
210 return GetUBwcSize(width, height, format, alignedw, alignedh);
211 }
212
213 if (IsUncompressedRGBFormat(format)) {
214 uint32_t bpp = GetBppForUncompressedRGB(format);
215 size = alignedw * alignedh * bpp;
216 return size;
217 }
218
219 if (IsCompressedRGBFormat(format)) {
220 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
221 return size;
222 }
223
224 // Below switch should be for only YUV/custom formats
225 switch (format) {
226 case HAL_PIXEL_FORMAT_RAW16:
227 size = alignedw * alignedh * 2;
228 break;
229 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmed92998622017-03-20 12:39:17 -0400230 case HAL_PIXEL_FORMAT_RAW12:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 size = ALIGN(alignedw * alignedh, SIZE_4K);
232 break;
Naseer Ahmedbe5c2ef2017-03-07 10:09:11 -0500233 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800234 size = alignedw * alignedh * 1;
235 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530236
237 // adreno formats
238 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
239 size = ALIGN(alignedw * alignedh, SIZE_4K);
240 size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
241 break;
242 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
243 // The chroma plane is subsampled,
244 // but the pitch in bytes is unchanged
245 // The GPU needs 4K alignment, but the video decoder needs 8K
246 size = ALIGN(alignedw * alignedh, SIZE_8K);
247 size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K);
248 break;
249 case HAL_PIXEL_FORMAT_YV12:
250 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
251 ALOGE("w or h is odd for the YV12 format");
252 return 0;
253 }
254 size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
255 size = ALIGN(size, (unsigned int)SIZE_4K);
256 break;
257 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
258 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
259 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
260 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800261 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
262 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
263 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530264 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
265 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
266 case HAL_PIXEL_FORMAT_YCbCr_422_I:
267 case HAL_PIXEL_FORMAT_YCrCb_422_I:
268 if (width & 1) {
269 ALOGE("width is odd for the YUV422_SP format");
270 return 0;
271 }
272 size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
273 break;
274 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
275 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
276 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
277 break;
278 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
279 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
280 break;
281 case HAL_PIXEL_FORMAT_BLOB:
282 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
283 if (height != 1) {
284 ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
285 return 0;
286 }
287 size = (unsigned int)width;
288 break;
289 case HAL_PIXEL_FORMAT_NV21_ZSL:
290 size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K);
291 break;
292 default:
293 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
294 return 0;
295 }
296
297 return size;
298}
299
300void Allocator::GetBufferSizeAndDimensions(int width, int height, int format, unsigned int *size,
301 unsigned int *alignedw, unsigned int *alignedh) {
302 BufferDescriptor descriptor = BufferDescriptor(width, height, format);
303 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
304
305 *size = GetSize(descriptor, *alignedw, *alignedh);
306}
307
308void Allocator::GetBufferSizeAndDimensions(const BufferDescriptor &descriptor, unsigned int *size,
309 unsigned int *alignedw, unsigned int *alignedh) {
310 GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
311
312 *size = GetSize(descriptor, *alignedw, *alignedh);
313}
314
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530315void Allocator::GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
316 int color_format, struct android_ycbcr *ycbcr) {
317 // UBWC buffer has these 4 planes in the following sequence:
318 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
319 unsigned int y_meta_stride, y_meta_height, y_meta_size;
320 unsigned int y_stride, y_height, y_size;
321 unsigned int c_meta_stride, c_meta_height, c_meta_size;
322 unsigned int alignment = 4096;
323
324 y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width));
325 y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height));
326 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
327
328 y_stride = VENUS_Y_STRIDE(color_format, INT(width));
329 y_height = VENUS_Y_SCANLINES(color_format, INT(height));
330 y_size = ALIGN((y_stride * y_height), alignment);
331
332 c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width));
333 c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height));
334 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
335
336 ycbcr->y = reinterpret_cast<void *>(base + y_meta_size);
337 ycbcr->cb = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size);
338 ycbcr->cr = reinterpret_cast<void *>(base + y_meta_size + y_size + c_meta_size + 1);
339 ycbcr->ystride = y_stride;
340 ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width));
341}
342
343void Allocator::GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp,
344 struct android_ycbcr *ycbcr) {
345 unsigned int ystride, cstride;
346
347 ystride = cstride = UINT(width) * bpp;
348 ycbcr->y = reinterpret_cast<void *>(base);
349 ycbcr->cb = reinterpret_cast<void *>(base + ystride * UINT(height));
350 ycbcr->cr = reinterpret_cast<void *>(base + ystride * UINT(height) + 1);
351 ycbcr->ystride = ystride;
352 ycbcr->cstride = cstride;
353 ycbcr->chroma_step = 2 * bpp;
354}
355
356int Allocator::GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr) {
357 int err = 0;
358 uint32_t width = UINT(hnd->width);
359 uint32_t height = UINT(hnd->height);
360 int format = hnd->format;
361 gralloc1_producer_usage_t prod_usage = hnd->GetProducerUsage();
362 gralloc1_consumer_usage_t cons_usage = hnd->GetConsumerUsage();
363 unsigned int ystride, cstride;
364
365 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
366 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
367
368 // Check if UBWC buffer has been rendered in linear format.
369 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
370 format = INT(metadata->linearFormat);
371 }
372
373 // Check metadata if the geometry has been updated.
374 if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
375 int usage = 0;
376
377 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
378 usage = GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC;
379 }
380
381 BufferDescriptor descriptor =
382 BufferDescriptor(metadata->bufferDim.sliceWidth, metadata->bufferDim.sliceHeight, format,
383 prod_usage, cons_usage);
384 GetAlignedWidthAndHeight(descriptor, &width, &height);
385 }
386
387 // Get the chroma offsets from the handle width/height. We take advantage
388 // of the fact the width _is_ the stride
389 switch (format) {
390 // Semiplanar
391 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
392 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
393 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
394 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
395 // Same as YCbCr_420_SP_VENUS
396 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
397 break;
398
399 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
400 GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
401 break;
402
403 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
404 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr);
405 ycbcr->chroma_step = 2;
406 break;
407
408 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
409 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
410 ycbcr->chroma_step = 3;
411 break;
412
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800413 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
414 GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_P010_UBWC, ycbcr);
415 ycbcr->chroma_step = 4;
416 break;
417
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530418 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
419 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
420 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
421 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
422 case HAL_PIXEL_FORMAT_NV21_ZSL:
423 case HAL_PIXEL_FORMAT_RAW16:
424 case HAL_PIXEL_FORMAT_RAW10:
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800425 case HAL_PIXEL_FORMAT_RAW8:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530426 GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
427 std::swap(ycbcr->cb, ycbcr->cr);
428 break;
429
430 // Planar
431 case HAL_PIXEL_FORMAT_YV12:
432 ystride = width;
433 cstride = ALIGN(width / 2, 16);
434 ycbcr->y = reinterpret_cast<void *>(hnd->base);
435 ycbcr->cr = reinterpret_cast<void *>(hnd->base + ystride * height);
436 ycbcr->cb = reinterpret_cast<void *>(hnd->base + ystride * height + cstride * height / 2);
437 ycbcr->ystride = ystride;
438 ycbcr->cstride = cstride;
439 ycbcr->chroma_step = 1;
440 break;
441
442 // Unsupported formats
443 case HAL_PIXEL_FORMAT_YCbCr_422_I:
444 case HAL_PIXEL_FORMAT_YCrCb_422_I:
445 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
446 default:
447 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
448 err = -EINVAL;
449 }
450
451 return err;
452}
453
454int Allocator::GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage,
455 gralloc1_consumer_usage_t cons_usage, int format) {
456 int gr_format = format;
457
458 // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
459 // the usage bits, gralloc assigns a format.
460 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
461 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
462 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) {
463 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
464 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
465 gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530466 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
Naseer Ahmed1901fa32017-03-22 20:34:45 -0400467 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
468 // Assumed ZSL if both producer and consumer camera flags set
469 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
470 } else {
471 gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21
472 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530473 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
474 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
475 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21
476 } else {
477 gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview
478 }
479 } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
480 // XXX: If we still haven't set a format, default to RGBA8888
481 gr_format = HAL_PIXEL_FORMAT_RGBA_8888;
482 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
483 // If no other usage flags are detected, default the
484 // flexible YUV format to NV21_ZSL
485 gr_format = HAL_PIXEL_FORMAT_NV21_ZSL;
486 }
487 }
488
489 return gr_format;
490}
491
492// Explicitly defined UBWC formats
493bool Allocator::IsUBwcFormat(int format) {
494 switch (format) {
495 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Arun Kumar K.R54885f02017-03-23 11:56:59 -0700496 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800497 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530498 return true;
499 default:
500 return false;
501 }
502}
503
504bool Allocator::IsUBwcSupported(int format) {
505 // Existing HAL formats with UBWC support
506 switch (format) {
507 case HAL_PIXEL_FORMAT_BGR_565:
508 case HAL_PIXEL_FORMAT_RGBA_8888:
509 case HAL_PIXEL_FORMAT_RGBX_8888:
510 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
511 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
512 return true;
513 default:
514 break;
515 }
516
517 return false;
518}
519
520/* The default policy is to return cached buffers unless the client explicity
521 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
522 * read or written in software. */
523// TODO(user) : As of now relying only on producer usage
524bool Allocator::UseUncached(gralloc1_producer_usage_t usage) {
525 if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) ||
526 (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) {
527 return true;
528 }
529
530 // CPU read rarely
531 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) &&
532 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) {
533 return true;
534 }
535
536 // CPU write rarely
537 if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) &&
538 !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) {
539 return true;
540 }
541
542 return false;
543}
544
545void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage,
546 gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id,
547 unsigned int *alloc_type, unsigned int *ion_flags) {
548 unsigned int heap_id = 0;
549 unsigned int type = 0;
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400550 uint32_t flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530551 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
552 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
553 heap_id = ION_HEAP(SD_HEAP_ID);
554 /*
555 * There is currently no flag in ION for Secure Display
556 * VM. Please add it to the define once available.
557 */
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400558 flags |= UINT(ION_SD_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700559 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
560 heap_id = ION_HEAP(SD_HEAP_ID);
561 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400562 flags |= UINT(ION_SC_PREVIEW_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700563 } else {
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400564 flags |= UINT(ION_SC_FLAGS);
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700565 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530566 } else {
567 heap_id = ION_HEAP(CP_HEAP_ID);
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400568 flags |= UINT(ION_CP_FLAGS);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530569 }
570 } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) {
571 // MM Heap is exclusively a secure heap.
572 // If it is used for non secure cases, fallback to IOMMU heap
573 ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!");
574 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
575 }
576
577 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP) {
578 heap_id |= ION_HEAP(ION_CAMERA_HEAP_ID);
579 }
580
581 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP) {
582 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
583 }
584
Naseer Ahmed7dc06d12017-03-14 11:13:27 -0400585 if (flags & UINT(ION_SECURE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530586 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
587 }
588
589 // if no ion heap flags are set, default to system heap
590 if (!heap_id) {
591 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
592 }
593
594 *alloc_type = type;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500595 *ion_flags = flags;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530596 *ion_heap_id = heap_id;
597
598 return;
599}
600
601bool Allocator::IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage,
602 gralloc1_consumer_usage_t cons_usage) {
603 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
604 if (IsUBwcFormat(format)) {
605 return true;
606 }
607
608 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
609 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
610 // usage flag and MDP supports the format.
611 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) {
612 bool enable = true;
613 // Query GPU for UBWC only if buffer is intended to be used by GPU.
614 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) ||
615 (prod_usage & GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET)) {
616 enable = adreno_helper_->IsUBWCSupportedByGPU(format);
617 }
618
619 // Allow UBWC, only if CPU usage flags are not set
620 if (enable && !(CpuCanAccess(prod_usage, cons_usage))) {
621 return true;
622 }
623 }
624
625 return false;
626}
627
628void Allocator::GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w,
629 unsigned int *aligned_h) {
630 switch (format) {
631 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
632 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
633 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
634 *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
635 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
636 break;
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700637 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
638 // The macro returns the stride which is 4/3 times the width, hence * 3/4
639 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
640 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
641 break;
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800642 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
643 // The macro returns the stride which is 2 times the width, hence / 2
644 *aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_P010_UBWC, width) / 2);
645 *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_P010_UBWC, height);
646 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530647 default:
648 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
649 *aligned_w = 0;
650 *aligned_h = 0;
651 break;
652 }
653}
654
655void Allocator::GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) {
656 *block_width = 0;
657 *block_height = 0;
658
659 switch (bpp) {
660 case 2:
661 case 4:
662 *block_width = 16;
663 *block_height = 4;
664 break;
665 case 8:
666 *block_width = 8;
667 *block_height = 4;
668 break;
669 case 16:
670 *block_width = 4;
671 *block_height = 4;
672 break;
673 default:
674 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
675 break;
676 }
677}
678
679unsigned int Allocator::GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) {
680 unsigned int size = 0;
681 int meta_width, meta_height;
682 int block_width, block_height;
683
684 GetRgbUBwcBlockSize(bpp, &block_width, &block_height);
685 if (!block_width || !block_height) {
686 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
687 return size;
688 }
689
690 // Align meta buffer height to 16 blocks
691 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
692
693 // Align meta buffer width to 64 blocks
694 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
695
696 // Align meta buffer size to 4K
697 size = (unsigned int)ALIGN((meta_width * meta_height), 4096);
698
699 return size;
700}
701
702unsigned int Allocator::GetUBwcSize(int width, int height, int format, unsigned int alignedw,
703 unsigned int alignedh) {
704 unsigned int size = 0;
705 uint32_t bpp = 0;
706 switch (format) {
707 case HAL_PIXEL_FORMAT_BGR_565:
708 case HAL_PIXEL_FORMAT_RGBA_8888:
709 case HAL_PIXEL_FORMAT_RGBX_8888:
710 case HAL_PIXEL_FORMAT_RGBA_1010102:
711 case HAL_PIXEL_FORMAT_RGBX_1010102:
712 bpp = GetBppForUncompressedRGB(format);
713 size = alignedw * alignedh * bpp;
714 size += GetRgbUBwcMetaBufferSize(width, height, bpp);
715 break;
716 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
717 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
718 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
719 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
720 break;
721 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
722 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
723 break;
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800724 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
725 size = VENUS_BUFFER_SIZE(COLOR_FMT_P010_UBWC, width, height);
726 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530727 default:
728 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
729 break;
730 }
731
732 return size;
733}
734
735int Allocator::GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) {
736 int err = 0;
737
738 // This api is for RGB* formats
739 if (!gralloc1::IsUncompressedRGBFormat(hnd->format)) {
740 return -EINVAL;
741 }
742
743 // linear buffer, nothing to do further
744 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
745 *rgb_data = reinterpret_cast<void *>(hnd->base);
746 return err;
747 }
748
749 unsigned int meta_size = 0;
750 uint32_t bpp = GetBppForUncompressedRGB(hnd->format);
751 switch (hnd->format) {
752 case HAL_PIXEL_FORMAT_BGR_565:
753 case HAL_PIXEL_FORMAT_RGBA_8888:
754 case HAL_PIXEL_FORMAT_RGBX_8888:
755 meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp);
756 break;
757 default:
758 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
759 err = -EINVAL;
760 break;
761 }
762 *rgb_data = reinterpret_cast<void *>(hnd->base + meta_size);
763
764 return err;
765}
766
767void Allocator::GetAlignedWidthAndHeight(const BufferDescriptor &descriptor, unsigned int *alignedw,
768 unsigned int *alignedh) {
769 int width = descriptor.GetWidth();
770 int height = descriptor.GetHeight();
771 int format = descriptor.GetFormat();
772 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
773 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
774
775 // Currently surface padding is only computed for RGB* surfaces.
776 bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700777 int tile = ubwc_enabled;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530778
779 if (IsUncompressedRGBFormat(format)) {
780 adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh);
781 return;
782 }
783
784 if (ubwc_enabled) {
785 GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh);
786 return;
787 }
788
789 if (IsCompressedRGBFormat(format)) {
790 adreno_helper_->AlignCompressedRGB(width, height, format, alignedw, alignedh);
791 return;
792 }
793
794 int aligned_w = width;
795 int aligned_h = height;
796 unsigned int alignment = 32;
797
798 // Below should be only YUV family
799 switch (format) {
800 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
801 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
802 alignment = adreno_helper_->GetGpuPixelAlignment();
803 aligned_w = ALIGN(width, alignment);
804 break;
805 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
806 aligned_w = ALIGN(width, alignment);
807 break;
808 case HAL_PIXEL_FORMAT_RAW16:
809 aligned_w = ALIGN(width, 16);
810 break;
Naseer Ahmed92998622017-03-20 12:39:17 -0400811 case HAL_PIXEL_FORMAT_RAW12:
812 aligned_w = ALIGN(width * 12 / 8, 8);
813 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530814 case HAL_PIXEL_FORMAT_RAW10:
Naseer Ahmeddfd5e332017-03-20 12:36:43 -0400815 aligned_w = ALIGN(width * 10 / 8, 8);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530816 break;
Prabhanjan Kandulaf48302a2017-02-10 15:46:04 -0800817 case HAL_PIXEL_FORMAT_RAW8:
818 aligned_w = ALIGN(width, 8);
819 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530820 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
821 aligned_w = ALIGN(width, 128);
822 break;
823 case HAL_PIXEL_FORMAT_YV12:
824 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
825 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
826 case HAL_PIXEL_FORMAT_YCbCr_422_I:
827 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800828 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530829 aligned_w = ALIGN(width, 16);
830 break;
831 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
832 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
833 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width));
834 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height));
835 break;
836 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
837 aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width));
838 aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height));
839 break;
840 case HAL_PIXEL_FORMAT_BLOB:
841 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
842 break;
843 case HAL_PIXEL_FORMAT_NV21_ZSL:
844 aligned_w = ALIGN(width, 64);
845 aligned_h = ALIGN(height, 64);
846 break;
847 default:
848 break;
849 }
850
851 *alignedw = (unsigned int)aligned_w;
852 *alignedh = (unsigned int)aligned_h;
853}
854
855} // namespace gralloc1