blob: 87d4c1e957db85bab9611f2346a92f13d7bb14a3 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Saurabh Shahc5b2b702016-10-24 17:16:01 -07002 * Copyright (c) 2011 - 2017, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
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.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * 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>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#include <fcntl.h>
Naomi Luis01f5c8e2013-02-11 12:46:24 -080032#include <dlfcn.h>
Saurabh Shah2693a502017-01-30 10:48:00 -080033#include <media/msm_media_info.h>
34#include <qdMetaData.h>
35#include <utils/Singleton.h>
36#include <utils/Mutex.h>
37#include <algorithm>
38
Iliyan Malchev202a77d2012-06-11 14:41:12 -070039#include "gralloc_priv.h"
40#include "alloc_controller.h"
41#include "memalloc.h"
42#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043#include "gr.h"
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070044#include "qd_utils.h"
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080045
Naseer Ahmed63326f42013-12-18 02:45:48 -050046#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050047
Shalaj Jain3c490412015-04-22 16:52:03 -070048#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080049#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080050#endif
51
52#ifndef ION_FLAG_ALLOW_NON_CONTIG
53#define ION_FLAG_ALLOW_NON_CONTIG 0
54#endif
55
Sushil Chauhandfe55a22016-09-12 15:48:29 -070056#ifndef ION_FLAG_CP_CAMERA_PREVIEW
57#define ION_FLAG_CP_CAMERA_PREVIEW 0
58#endif
59
Shalaj Jain3c490412015-04-22 16:52:03 -070060#ifdef MASTER_SIDE_CP
61#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070062#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070063#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070064#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070065#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
66#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Shalaj Jain3c490412015-04-22 16:52:03 -070067#else // SLAVE_SIDE_CP
68#define CP_HEAP_ID ION_CP_MM_HEAP_ID
69#define SD_HEAP_ID CP_HEAP_ID
70#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
71#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070072#define ION_SC_FLAGS ION_SECURE
73#define ION_SC_PREVIEW_FLAGS ION_SECURE
Shalaj Jain3c490412015-04-22 16:52:03 -070074#endif
75
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070077using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070078using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079
Naomi Luisa44100c2013-02-08 12:42:03 -080080ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053081ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080082
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080083static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080084static unsigned int getUBwcSize(int, int, int, const int, const int);
85
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087
Saurabh Shah1adcafe2014-12-19 10:05:41 -080088/* The default policy is to return cached buffers unless the client explicity
89 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
90 * read or written in software. Any combination with a _RARELY_ flag will be
91 * treated as uncached. */
92static bool useUncached(const int& usage) {
Naseer Ahmedde758fd2016-05-03 15:10:23 -040093 if ((usage & GRALLOC_USAGE_PROTECTED) or
94 (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
95 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) or
96 ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Saurabh Shah1adcafe2014-12-19 10:05:41 -080097 return true;
98
99 return false;
100}
101
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530102//------------- MDPCapabilityInfo-----------------------//
103MDPCapabilityInfo :: MDPCapabilityInfo() {
Sushil Chauhan01361412016-04-25 16:36:18 -0700104 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800105 qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530106}
107
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530108//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800109AdrenoMemInfo::AdrenoMemInfo()
110{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800111 LINK_adreno_compute_aligned_width_and_height = NULL;
112 LINK_adreno_compute_padding = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800113 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800114 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700115 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800116
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800117 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
118 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800119 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700120 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
121 *(void **)&LINK_adreno_compute_padding =
122 ::dlsym(libadreno_utils, "compute_surface_padding");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800123 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
124 ::dlsym(libadreno_utils,
125 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800126 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
127 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700128 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
129 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800130 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700131
132 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
133 // that disables UBWC allocations for the graphics stack is set
134 gfx_ubwc_disable = 0;
135 char property[PROPERTY_VALUE_MAX];
136 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
137 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
138 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
139 gfx_ubwc_disable = 1;
140 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800141}
142
143AdrenoMemInfo::~AdrenoMemInfo()
144{
145 if (libadreno_utils) {
146 ::dlclose(libadreno_utils);
147 }
148}
149
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700150void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
151 int& aligned_h) {
152 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
153 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
154 int w = metadata->bufferDim.sliceWidth;
155 int h = metadata->bufferDim.sliceHeight;
156 int f = hnd->format;
157 int usage = 0;
158
159 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
160 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
161 }
162
163 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
164 } else {
165 aligned_w = hnd->width;
166 aligned_h = hnd->height;
167 }
168
169}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700170
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700171void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
172 int& unaligned_h) {
173 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
174 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
175 unaligned_w = metadata->bufferDim.sliceWidth;
176 unaligned_h = metadata->bufferDim.sliceHeight;
177 } else {
178 unaligned_w = hnd->unaligned_width;
179 unaligned_h = hnd->unaligned_height;
180 }
181}
182
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700183bool isUncompressedRgbFormat(int format)
184{
185 bool is_rgb_format = false;
186
187 switch (format)
188 {
189 case HAL_PIXEL_FORMAT_RGBA_8888:
190 case HAL_PIXEL_FORMAT_RGBX_8888:
191 case HAL_PIXEL_FORMAT_RGB_888:
192 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600193 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700194 case HAL_PIXEL_FORMAT_BGRA_8888:
195 case HAL_PIXEL_FORMAT_RGBA_5551:
196 case HAL_PIXEL_FORMAT_RGBA_4444:
197 case HAL_PIXEL_FORMAT_R_8:
198 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800199 case HAL_PIXEL_FORMAT_BGRX_8888:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530200 case HAL_PIXEL_FORMAT_BGR_888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800201 case HAL_PIXEL_FORMAT_RGBA_1010102:
202 case HAL_PIXEL_FORMAT_ARGB_2101010:
203 case HAL_PIXEL_FORMAT_RGBX_1010102:
204 case HAL_PIXEL_FORMAT_XRGB_2101010:
205 case HAL_PIXEL_FORMAT_BGRA_1010102:
206 case HAL_PIXEL_FORMAT_ABGR_2101010:
207 case HAL_PIXEL_FORMAT_BGRX_1010102:
208 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700209 is_rgb_format = true;
210 break;
211 default:
212 break;
213 }
214
215 return is_rgb_format;
216}
217
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800218void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800219 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800220{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700221 bool ubwc_enabled = isUBwcEnabled(format, usage);
222
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800223 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700224 if (isUncompressedRgbFormat(format) == true) {
Saurabh Shahc5b2b702016-10-24 17:16:01 -0700225 int tileEnabled = ubwc_enabled;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700226 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800227 } else if (ubwc_enabled) {
228 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
229 } else {
230 aligned_w = width;
231 aligned_h = height;
232 int alignment = 32;
233 switch (format)
234 {
235 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
236 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
237 if (LINK_adreno_get_gpu_pixel_alignment) {
238 alignment = LINK_adreno_get_gpu_pixel_alignment();
239 }
240 aligned_w = ALIGN(width, alignment);
241 break;
242 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
243 aligned_w = ALIGN(width, alignment);
244 break;
245 case HAL_PIXEL_FORMAT_RAW16:
246 aligned_w = ALIGN(width, 16);
247 break;
Zhu Bine2422e02016-12-21 16:24:49 +0800248 case HAL_PIXEL_FORMAT_RAW12:
249 aligned_w = ALIGN(width * 12 / 8, 8);
250 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800251 case HAL_PIXEL_FORMAT_RAW10:
252 aligned_w = ALIGN(width * 10 / 8, 8);
253 break;
254 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
255 aligned_w = ALIGN(width, 128);
256 break;
257 case HAL_PIXEL_FORMAT_YV12:
258 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
259 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
260 case HAL_PIXEL_FORMAT_YCbCr_422_I:
261 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800262 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530263 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800264 aligned_w = ALIGN(width, 16);
265 break;
266 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
267 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
268 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
269 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
270 break;
271 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
272 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
273 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
274 break;
275 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400276 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800277 break;
278 case HAL_PIXEL_FORMAT_NV21_ZSL:
279 aligned_w = ALIGN(width, 64);
280 aligned_h = ALIGN(height, 64);
281 break;
282 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
283 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
284 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
285 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
286 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
287 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
288 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
310 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
311 int bytesPerPixel = 0;
312 int raster_mode = 0; //Adreno unknown raster mode.
313 int padding_threshold = 512; //Threshold for padding
314 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800315
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800316 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
317 width, height, format, 0,raster_mode, padding_threshold,
318 &aligned_w, &aligned_h, &bytesPerPixel);
319 } else {
320 ALOGW("%s: Warning!! Symbols" \
321 " compute_compressedfmt_aligned_width_and_height" \
322 " not found", __FUNCTION__);
323 }
324 break;
325 default: break;
326 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800327 }
328}
329
330void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
331 int tile_enabled, int& aligned_w, int& aligned_h)
332{
333 aligned_w = ALIGN(width, 32);
334 aligned_h = ALIGN(height, 32);
335
336 // Don't add any additional padding if debug.gralloc.map_fb_memory
337 // is enabled
338 char property[PROPERTY_VALUE_MAX];
339 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
340 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
341 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
342 return;
343 }
344
345 int bpp = 4;
346 switch(format)
347 {
348 case HAL_PIXEL_FORMAT_RGB_888:
349 bpp = 3;
350 break;
351 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600352 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800353 case HAL_PIXEL_FORMAT_RGBA_5551:
354 case HAL_PIXEL_FORMAT_RGBA_4444:
355 bpp = 2;
356 break;
357 default: break;
358 }
359
360 if (libadreno_utils) {
361 int raster_mode = 0; // Adreno unknown raster mode.
362 int padding_threshold = 512; // Threshold for padding surfaces.
363 // the function below computes aligned width and aligned height
364 // based on linear or macro tile mode selected.
365 if(LINK_adreno_compute_aligned_width_and_height) {
366 LINK_adreno_compute_aligned_width_and_height(width,
367 height, bpp, tile_enabled,
368 raster_mode, padding_threshold,
369 &aligned_w, &aligned_h);
370
371 } else if(LINK_adreno_compute_padding) {
372 int surface_tile_height = 1; // Linear surface
373 aligned_w = LINK_adreno_compute_padding(width, bpp,
374 surface_tile_height, raster_mode,
375 padding_threshold);
376 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
377 __FUNCTION__);
378 } else {
379 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
380 "compute_aligned_width_and_height not found", __FUNCTION__);
381 }
382 }
383}
384
385int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
386{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700387 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800388 if (LINK_adreno_isUBWCSupportedByGpu) {
389 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
390 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
391 }
392 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800393 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800394}
395
Sushil Chauhan082acd62015-01-14 16:49:29 -0800396ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
397{
398 switch (hal_format) {
399 case HAL_PIXEL_FORMAT_RGBA_8888:
400 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700401 case HAL_PIXEL_FORMAT_RGBX_8888:
402 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800403 case HAL_PIXEL_FORMAT_RGB_565:
404 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600405 case HAL_PIXEL_FORMAT_BGR_565:
406 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800407 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800408 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800409 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
410 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800411 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800412 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
413 return ADRENO_PIXELFORMAT_TP10;
414 case HAL_PIXEL_FORMAT_RGBA_1010102:
415 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
416 case HAL_PIXEL_FORMAT_RGBX_1010102:
417 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
418 case HAL_PIXEL_FORMAT_ABGR_2101010:
419 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800420 default:
421 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
422 break;
423 }
424 return ADRENO_PIXELFORMAT_UNKNOWN;
425}
426
Naomi Luisa44100c2013-02-08 12:42:03 -0800427//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700428IAllocController* IAllocController::sController = NULL;
429IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700430{
431 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700432 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700433 }
434 return sController;
435}
436
437
438//-------------- IonController-----------------------//
439IonController::IonController()
440{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530441 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530442
443 char property[PROPERTY_VALUE_MAX];
444 property_get("video.disable.ubwc", property, "0");
445 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530446}
447
448void IonController::allocateIonMem()
449{
450 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700451}
452
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700453int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700454{
455 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500456 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700457 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700458
459 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700460 data.allocType = 0;
461
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530462 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400463 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
464 ionHeapId = ION_HEAP(SD_HEAP_ID);
465 /*
466 * There is currently no flag in ION for Secure Display
467 * VM. Please add it to the define once available.
468 */
469 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700470 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
471 ionHeapId = ION_HEAP(SD_HEAP_ID);
472 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530473 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400474 ionHeapId = ION_HEAP(CP_HEAP_ID);
475 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500476 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530477 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
478 //MM Heap is exclusively a secure heap.
479 //If it is used for non secure cases, fallback to IOMMU heap
480 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
481 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500482 trying to use system heap instead !!");
483 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500484 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700485
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700486 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500487 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700488
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700489 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500490 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700491
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530492 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500493 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700494
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500495 // if no ion heap flags are set, default to system heap
496 if(!ionHeapId)
497 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500499 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500501 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700502 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700503
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700504 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700505 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500506 } else {
507 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
508 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700509 }
510
511 return ret;
512}
513
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700514IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700516 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
518 memalloc = mIonAlloc;
519 } else {
520 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
521 }
522
523 return memalloc;
524}
525
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700526// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800527unsigned int getSize(int format, int width, int height, int usage,
528 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529
Sushil Chauhan65e26302015-01-14 10:48:57 -0800530 if (isUBwcEnabled(format, usage)) {
531 return getUBwcSize(width, height, format, alignedw, alignedh);
532 }
533
534 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700535 switch (format) {
536 case HAL_PIXEL_FORMAT_RGBA_8888:
537 case HAL_PIXEL_FORMAT_RGBX_8888:
538 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800539 case HAL_PIXEL_FORMAT_RGBA_1010102:
540 case HAL_PIXEL_FORMAT_ARGB_2101010:
541 case HAL_PIXEL_FORMAT_RGBX_1010102:
542 case HAL_PIXEL_FORMAT_XRGB_2101010:
543 case HAL_PIXEL_FORMAT_BGRA_1010102:
544 case HAL_PIXEL_FORMAT_ABGR_2101010:
545 case HAL_PIXEL_FORMAT_BGRX_1010102:
546 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700547 size = alignedw * alignedh * 4;
548 break;
549 case HAL_PIXEL_FORMAT_RGB_888:
550 size = alignedw * alignedh * 3;
551 break;
552 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600553 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700554 case HAL_PIXEL_FORMAT_RGBA_5551:
555 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700556 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700557 size = alignedw * alignedh * 2;
558 break;
Zhu Bine2422e02016-12-21 16:24:49 +0800559 case HAL_PIXEL_FORMAT_RAW12:
560 size = ALIGN(alignedw * alignedh, 4096);
561 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700562 case HAL_PIXEL_FORMAT_RAW10:
563 size = ALIGN(alignedw * alignedh, 4096);
564 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700565
566 // adreno formats
567 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
568 size = ALIGN(alignedw*alignedh, 4096);
569 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
570 break;
571 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
572 // The chroma plane is subsampled,
573 // but the pitch in bytes is unchanged
574 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700575 size = ALIGN( alignedw * alignedh, 8192);
576 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
577 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700578 case HAL_PIXEL_FORMAT_YV12:
579 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
580 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800581 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700582 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400583 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700585 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700586 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800587 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
588 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400589 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800590 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800591 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
592 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, 4096);
593 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
595 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700596 case HAL_PIXEL_FORMAT_YCbCr_422_I:
597 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530598 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700599 if(width & 1) {
600 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800601 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700602 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700603 size = ALIGN(alignedw * alignedh * 2, 4096);
604 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700605 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400606 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700607 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700608 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530609 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
610 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
611 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400612 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400613 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400614 if(height != 1) {
615 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
616 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800617 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400618 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400619 size = width;
620 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700621 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700622 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
623 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500624 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
625 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
626 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
627 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
628 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
629 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
630 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
631 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
632 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
633 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
634 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
635 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
636 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
637 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
638 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
639 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
640 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
641 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
642 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
643 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
644 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
645 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
646 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
647 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
648 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
649 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
650 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800651 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500652 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
653 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700654 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500655 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800656 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700657 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700658 return size;
659}
660
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700661unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700662 int& alignedw, int &alignedh)
663{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700664 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700665
666 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
667 height,
668 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800669 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700670 alignedw,
671 alignedh);
672
Sushil Chauhan65e26302015-01-14 10:48:57 -0800673 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700674
675 return size;
676}
677
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700678
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700679unsigned int getBufferSizeAndDimensions(int width, int height, int format,
680 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700681{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700682 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700683
684 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
685 height,
686 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800687 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700688 alignedw,
689 alignedh);
690
Sushil Chauhan65e26302015-01-14 10:48:57 -0800691 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700692
693 return size;
694}
695
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700696void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
697 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800698{
699 // UBWC buffer has these 4 planes in the following sequence:
700 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
701 unsigned int y_meta_stride, y_meta_height, y_meta_size;
702 unsigned int y_stride, y_height, y_size;
703 unsigned int c_meta_stride, c_meta_height, c_meta_size;
704 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800705
706 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
707 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
708 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
709
710 y_stride = VENUS_Y_STRIDE(color_format, width);
711 y_height = VENUS_Y_SCANLINES(color_format, height);
712 y_size = ALIGN((y_stride * y_height), alignment);
713
714 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
715 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
716 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
717
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700718 ycbcr->y = (void*)(base + y_meta_size);
719 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
720 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800721 c_meta_size + 1);
722 ycbcr->ystride = y_stride;
723 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
724}
725
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700726void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
727 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800728{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800729 unsigned int ystride, cstride;
730
731 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700732 ycbcr->y = (void*)base;
733 ycbcr->cb = (void*)(base + ystride * height);
734 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800735 ycbcr->ystride = ystride;
736 ycbcr->cstride = cstride;
737 ycbcr->chroma_step = 2 * bpp;
738}
739
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400740int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
741{
742 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700743 int width = hnd->width;
744 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700745 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700746
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700747 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700748
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400749 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700750 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
751
752 // Check if UBWC buffer has been rendered in linear format.
753 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
754 format = metadata->linearFormat;
755 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400756
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700757 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700758 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700759 int usage = 0;
760
761 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
762 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
763 }
764
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700765 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700766 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700767 }
768
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400769 // Get the chroma offsets from the handle width/height. We take advantage
770 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700771 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400772 //Semiplanar
773 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
774 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
775 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
776 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700777 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800778 break;
779
780 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700781 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400782 break;
783
Sushil Chauhan4686c972015-02-20 15:44:52 -0800784 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700785 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
786 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800787 ycbcr->chroma_step = 2;
788 break;
789
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800790 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700791 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
792 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800793 ycbcr->chroma_step = 3;
794 break;
795
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400796 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
797 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
798 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530799 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400800 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700801 case HAL_PIXEL_FORMAT_RAW16:
Zhu Bine2422e02016-12-21 16:24:49 +0800802 case HAL_PIXEL_FORMAT_RAW12:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700803 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700804 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800805 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400806 break;
807
808 //Planar
809 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700810 ystride = width;
811 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400812 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700813 ycbcr->cr = (void*)(hnd->base + ystride * height);
814 ycbcr->cb = (void*)(hnd->base + ystride * height +
815 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400816 ycbcr->ystride = ystride;
817 ycbcr->cstride = cstride;
818 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400819 break;
Rahul Sharma61173ea2017-01-05 09:15:40 +0530820 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
821 ystride = width * 2;
822 cstride = 0;
823 ycbcr->y = (void*)hnd->base;
824 ycbcr->cr = NULL;
825 ycbcr->cb = NULL;
826 ycbcr->ystride = ystride;
827 ycbcr->cstride = 0;
828 ycbcr->chroma_step = 0;
829 break;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400830 //Unsupported formats
831 case HAL_PIXEL_FORMAT_YCbCr_422_I:
832 case HAL_PIXEL_FORMAT_YCrCb_422_I:
833 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
834 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700835 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400836 err = -EINVAL;
837 }
838 return err;
839
840}
841
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700842
843
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700844// Allocate buffer from width, height and format into a
845// private_handle_t. It is the responsibility of the caller
846// to free the buffer using the free_buffer function
847int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
848{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700849 alloc_data data;
850 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700851 gralloc::IAllocController* sAlloc =
852 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700853 data.base = 0;
854 data.fd = -1;
855 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700856 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
857 alignedh);
858
Naseer Ahmed29a26812012-06-14 00:56:20 -0700859 data.align = getpagesize();
860 data.uncached = useUncached(usage);
861 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700862
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700863 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700864 if (0 != err) {
865 ALOGE("%s: allocate failed", __FUNCTION__);
866 return -ENOMEM;
867 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700868
radhakrishnad7131e62015-10-13 12:32:30 +0530869 if(isUBwcEnabled(format, usage)) {
870 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
871 }
872
Naseer Ahmed29a26812012-06-14 00:56:20 -0700873 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700874 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700875 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700876 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700877 hnd->offset = data.offset;
878 hnd->gpuaddr = 0;
879 *pHnd = hnd;
880 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700881}
882
883void free_buffer(private_handle_t *hnd)
884{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700885 gralloc::IAllocController* sAlloc =
886 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700887 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700888 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700889 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
890 }
891 if(hnd)
892 delete hnd;
893
894}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800895
896// UBWC helper functions
897static bool isUBwcFormat(int format)
898{
899 // Explicitly defined UBWC formats
900 switch(format)
901 {
902 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800903 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800904 return true;
905 default:
906 return false;
907 }
908}
909
910static bool isUBwcSupported(int format)
911{
Sushil Chauhan01361412016-04-25 16:36:18 -0700912 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
913 // Existing HAL formats with UBWC support
914 switch(format)
915 {
916 case HAL_PIXEL_FORMAT_BGR_565:
917 case HAL_PIXEL_FORMAT_RGBA_8888:
918 case HAL_PIXEL_FORMAT_RGBX_8888:
919 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
920 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
921 case HAL_PIXEL_FORMAT_RGBA_1010102:
922 case HAL_PIXEL_FORMAT_RGBX_1010102:
923 return true;
924 default:
925 break;
926 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800927 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700928 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800929}
930
931bool isUBwcEnabled(int format, int usage)
932{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800933 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
934 if (isUBwcFormat(format))
935 return true;
936
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530937 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
938 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
939 return false;
940 }
941
Sushil Chauhan7807d192015-08-13 10:10:48 -0700942 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
943 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
944 // usage flag and MDP supports the format.
945 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
946 bool enable = true;
947 // Query GPU for UBWC only if buffer is intended to be used by GPU.
948 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
949 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
950 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800951 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700952 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
953 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800954 return true;
955 }
956 }
957 return false;
958}
959
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800960static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800961 int& aligned_w, int& aligned_h)
962{
963 switch (format)
964 {
965 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
966 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
967 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
968 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
969 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
970 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800971 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -0700972 // The macro returns the stride which is 4/3 times the width, hence * 3/4
973 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800974 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
975 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800976 default:
977 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
978 aligned_w = 0;
979 aligned_h = 0;
980 break;
981 }
982}
983
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800984static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -0800985{
986 block_width = 0;
987 block_height = 0;
988
989 switch(bpp)
990 {
991 case 2:
992 case 4:
993 block_width = 16;
994 block_height = 4;
995 break;
996 case 8:
997 block_width = 8;
998 block_height = 4;
999 break;
1000 case 16:
1001 block_width = 4;
1002 block_height = 4;
1003 break;
1004 default:
1005 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1006 break;
1007 }
1008}
1009
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001010static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001011{
1012 unsigned int size = 0;
1013 int meta_width, meta_height;
1014 int block_width, block_height;
1015
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001016 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001017
1018 if (!block_width || !block_height) {
1019 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1020 return size;
1021 }
1022
1023 // Align meta buffer height to 16 blocks
1024 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1025
1026 // Align meta buffer width to 64 blocks
1027 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1028
1029 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001030 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001031 return size;
1032}
1033
1034static unsigned int getUBwcSize(int width, int height, int format,
1035 const int alignedw, const int alignedh) {
1036
1037 unsigned int size = 0;
1038 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001039 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001040 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001041 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001042 break;
1043 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001044 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001045 case HAL_PIXEL_FORMAT_RGBA_1010102:
1046 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001047 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001048 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001049 break;
1050 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1051 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1052 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1053 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1054 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001055 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1056 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1057 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001058 default:
1059 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1060 break;
1061 }
1062 return size;
1063}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001064
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001065int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001066{
1067 int err = 0;
1068
1069 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001070 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001071 return -EINVAL;
1072 }
1073
1074 // linear buffer
1075 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001076 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001077 return err;
1078 }
1079
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001080 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001081 unsigned int meta_size = 0;
1082 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001083 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001084 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001085 break;
1086 case HAL_PIXEL_FORMAT_RGBA_8888:
1087 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001088 case HAL_PIXEL_FORMAT_RGBA_1010102:
1089 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001090 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001091 break;
1092 default:
1093 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1094 err = -EINVAL;
1095 break;
1096 }
1097
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001098 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001099 return err;
1100}