blob: 583767a70c1e9e0b3944dd44f8e7b51ac945e6db [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08002 * Copyright (c) 2011 - 2016, 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>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gralloc_priv.h"
34#include "alloc_controller.h"
35#include "memalloc.h"
36#include "ionalloc.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include "gr.h"
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070038#include "qd_utils.h"
Kaushik Kanetkar071aca62015-01-22 23:16:26 -070039#include <qdMetaData.h>
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070040#include <utils/Singleton.h>
41#include <utils/Mutex.h>
Pullakavi Srinivasc6442662016-06-22 02:20:07 +053042#include <algorithm>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080044#ifdef VENUS_COLOR_FORMAT
45#include <media/msm_media_info.h>
46#else
47#define VENUS_Y_STRIDE(args...) 0
48#define VENUS_Y_SCANLINES(args...) 0
49#define VENUS_BUFFER_SIZE(args...) 0
50#endif
51
Naseer Ahmed63326f42013-12-18 02:45:48 -050052#define ASTC_BLOCK_SIZE 16
Naseer Ahmed63326f42013-12-18 02:45:48 -050053
Shalaj Jain3c490412015-04-22 16:52:03 -070054#ifndef ION_FLAG_CP_PIXEL
Shalaj Jain1f9725a2015-03-04 17:53:49 -080055#define ION_FLAG_CP_PIXEL 0
Shalaj Jain1f9725a2015-03-04 17:53:49 -080056#endif
57
58#ifndef ION_FLAG_ALLOW_NON_CONTIG
59#define ION_FLAG_ALLOW_NON_CONTIG 0
60#endif
61
Sushil Chauhandfe55a22016-09-12 15:48:29 -070062#ifndef ION_FLAG_CP_CAMERA_PREVIEW
63#define ION_FLAG_CP_CAMERA_PREVIEW 0
64#endif
65
Shalaj Jain3c490412015-04-22 16:52:03 -070066#ifdef MASTER_SIDE_CP
67#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070068#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070069#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070070#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Sushil Chauhandfe55a22016-09-12 15:48:29 -070071#define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA)
72#define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
Shalaj Jain3c490412015-04-22 16:52:03 -070073#else // SLAVE_SIDE_CP
74#define CP_HEAP_ID ION_CP_MM_HEAP_ID
75#define SD_HEAP_ID CP_HEAP_ID
76#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
77#define ION_SD_FLAGS ION_SECURE
Sushil Chauhandfe55a22016-09-12 15:48:29 -070078#define ION_SC_FLAGS ION_SECURE
79#define ION_SC_PREVIEW_FLAGS ION_SECURE
Shalaj Jain3c490412015-04-22 16:52:03 -070080#endif
81
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070083using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070084using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085
Naomi Luisa44100c2013-02-08 12:42:03 -080086ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053087ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080088
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080089static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080090static unsigned int getUBwcSize(int, int, int, const int, const int);
91
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093
Saurabh Shah1adcafe2014-12-19 10:05:41 -080094/* The default policy is to return cached buffers unless the client explicity
95 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
96 * read or written in software. Any combination with a _RARELY_ flag will be
97 * treated as uncached. */
98static bool useUncached(const int& usage) {
Naseer Ahmedde758fd2016-05-03 15:10:23 -040099 if ((usage & GRALLOC_USAGE_PROTECTED) or
100 (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
101 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) or
102 ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800103 return true;
104
105 return false;
106}
107
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530108//------------- MDPCapabilityInfo-----------------------//
109MDPCapabilityInfo :: MDPCapabilityInfo() {
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530110 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupported);
Sushil Chauhan01361412016-04-25 16:36:18 -0700111 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800112 qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530113}
114
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530115//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800116AdrenoMemInfo::AdrenoMemInfo()
117{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800118 LINK_adreno_compute_aligned_width_and_height = NULL;
119 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700120 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800121 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800122 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700123 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800124
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800125 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
126 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800127 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700128 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
129 *(void **)&LINK_adreno_compute_padding =
130 ::dlsym(libadreno_utils, "compute_surface_padding");
131 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
132 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800133 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
134 ::dlsym(libadreno_utils,
135 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800136 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
137 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700138 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
139 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800140 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700141
142 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
143 // that disables UBWC allocations for the graphics stack is set
144 gfx_ubwc_disable = 0;
145 char property[PROPERTY_VALUE_MAX];
146 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
147 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
148 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
149 gfx_ubwc_disable = 1;
150 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800151}
152
153AdrenoMemInfo::~AdrenoMemInfo()
154{
155 if (libadreno_utils) {
156 ::dlclose(libadreno_utils);
157 }
158}
159
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700160int AdrenoMemInfo::isMacroTilingSupportedByGPU()
161{
162 if ((libadreno_utils)) {
163 if(LINK_adreno_isMacroTilingSupportedByGpu) {
164 return LINK_adreno_isMacroTilingSupportedByGpu();
165 }
166 }
167 return 0;
168}
169
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700170void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
171 int& aligned_h) {
172 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
173 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
174 int w = metadata->bufferDim.sliceWidth;
175 int h = metadata->bufferDim.sliceHeight;
176 int f = hnd->format;
177 int usage = 0;
178
179 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
180 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
181 }
182
183 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
184 } else {
185 aligned_w = hnd->width;
186 aligned_h = hnd->height;
187 }
188
189}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700190
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700191void AdrenoMemInfo::getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
192 int& unaligned_h) {
193 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
194 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
195 unaligned_w = metadata->bufferDim.sliceWidth;
196 unaligned_h = metadata->bufferDim.sliceHeight;
197 } else {
198 unaligned_w = hnd->unaligned_width;
199 unaligned_h = hnd->unaligned_height;
200 }
201}
202
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700203bool isUncompressedRgbFormat(int format)
204{
205 bool is_rgb_format = false;
206
207 switch (format)
208 {
209 case HAL_PIXEL_FORMAT_RGBA_8888:
210 case HAL_PIXEL_FORMAT_RGBX_8888:
211 case HAL_PIXEL_FORMAT_RGB_888:
212 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600213 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700214 case HAL_PIXEL_FORMAT_BGRA_8888:
215 case HAL_PIXEL_FORMAT_RGBA_5551:
216 case HAL_PIXEL_FORMAT_RGBA_4444:
217 case HAL_PIXEL_FORMAT_R_8:
218 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800219 case HAL_PIXEL_FORMAT_BGRX_8888:
220 case HAL_PIXEL_FORMAT_RGBA_1010102:
221 case HAL_PIXEL_FORMAT_ARGB_2101010:
222 case HAL_PIXEL_FORMAT_RGBX_1010102:
223 case HAL_PIXEL_FORMAT_XRGB_2101010:
224 case HAL_PIXEL_FORMAT_BGRA_1010102:
225 case HAL_PIXEL_FORMAT_ABGR_2101010:
226 case HAL_PIXEL_FORMAT_BGRX_1010102:
227 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700228 is_rgb_format = true;
229 break;
230 default:
231 break;
232 }
233
234 return is_rgb_format;
235}
236
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800237void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800238 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800239{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700240 bool ubwc_enabled = isUBwcEnabled(format, usage);
241
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800242 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700243 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700244 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700245 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800246 } else if (ubwc_enabled) {
247 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
248 } else {
249 aligned_w = width;
250 aligned_h = height;
251 int alignment = 32;
252 switch (format)
253 {
254 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
255 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
256 if (LINK_adreno_get_gpu_pixel_alignment) {
257 alignment = LINK_adreno_get_gpu_pixel_alignment();
258 }
259 aligned_w = ALIGN(width, alignment);
260 break;
261 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
262 aligned_w = ALIGN(width, alignment);
263 break;
264 case HAL_PIXEL_FORMAT_RAW16:
265 aligned_w = ALIGN(width, 16);
266 break;
267 case HAL_PIXEL_FORMAT_RAW10:
268 aligned_w = ALIGN(width * 10 / 8, 8);
269 break;
270 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
271 aligned_w = ALIGN(width, 128);
272 break;
273 case HAL_PIXEL_FORMAT_YV12:
274 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
275 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
276 case HAL_PIXEL_FORMAT_YCbCr_422_I:
277 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800278 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800279 aligned_w = ALIGN(width, 16);
280 break;
281 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
282 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
283 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
284 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
285 break;
286 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
287 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
288 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
289 break;
290 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400291 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800292 break;
293 case HAL_PIXEL_FORMAT_NV21_ZSL:
294 aligned_w = ALIGN(width, 64);
295 aligned_h = ALIGN(height, 64);
296 break;
297 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
310 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
311 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
312 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
313 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
314 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
315 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
316 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
317 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
318 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
319 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
320 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
321 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
322 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
323 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
324 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
325 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
326 int bytesPerPixel = 0;
327 int raster_mode = 0; //Adreno unknown raster mode.
328 int padding_threshold = 512; //Threshold for padding
329 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800330
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800331 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
332 width, height, format, 0,raster_mode, padding_threshold,
333 &aligned_w, &aligned_h, &bytesPerPixel);
334 } else {
335 ALOGW("%s: Warning!! Symbols" \
336 " compute_compressedfmt_aligned_width_and_height" \
337 " not found", __FUNCTION__);
338 }
339 break;
340 default: break;
341 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800342 }
343}
344
345void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
346 int tile_enabled, int& aligned_w, int& aligned_h)
347{
348 aligned_w = ALIGN(width, 32);
349 aligned_h = ALIGN(height, 32);
350
351 // Don't add any additional padding if debug.gralloc.map_fb_memory
352 // is enabled
353 char property[PROPERTY_VALUE_MAX];
354 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
355 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
356 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
357 return;
358 }
359
360 int bpp = 4;
361 switch(format)
362 {
363 case HAL_PIXEL_FORMAT_RGB_888:
364 bpp = 3;
365 break;
366 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600367 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800368 case HAL_PIXEL_FORMAT_RGBA_5551:
369 case HAL_PIXEL_FORMAT_RGBA_4444:
370 bpp = 2;
371 break;
372 default: break;
373 }
374
375 if (libadreno_utils) {
376 int raster_mode = 0; // Adreno unknown raster mode.
377 int padding_threshold = 512; // Threshold for padding surfaces.
378 // the function below computes aligned width and aligned height
379 // based on linear or macro tile mode selected.
380 if(LINK_adreno_compute_aligned_width_and_height) {
381 LINK_adreno_compute_aligned_width_and_height(width,
382 height, bpp, tile_enabled,
383 raster_mode, padding_threshold,
384 &aligned_w, &aligned_h);
385
386 } else if(LINK_adreno_compute_padding) {
387 int surface_tile_height = 1; // Linear surface
388 aligned_w = LINK_adreno_compute_padding(width, bpp,
389 surface_tile_height, raster_mode,
390 padding_threshold);
391 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
392 __FUNCTION__);
393 } else {
394 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
395 "compute_aligned_width_and_height not found", __FUNCTION__);
396 }
397 }
398}
399
400int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
401{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700402 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800403 if (LINK_adreno_isUBWCSupportedByGpu) {
404 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
405 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
406 }
407 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800408 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800409}
410
Sushil Chauhan082acd62015-01-14 16:49:29 -0800411ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
412{
413 switch (hal_format) {
414 case HAL_PIXEL_FORMAT_RGBA_8888:
415 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700416 case HAL_PIXEL_FORMAT_RGBX_8888:
417 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800418 case HAL_PIXEL_FORMAT_RGB_565:
419 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600420 case HAL_PIXEL_FORMAT_BGR_565:
421 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800422 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800423 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800424 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
425 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800426 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800427 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
428 return ADRENO_PIXELFORMAT_TP10;
429 case HAL_PIXEL_FORMAT_RGBA_1010102:
430 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
431 case HAL_PIXEL_FORMAT_RGBX_1010102:
432 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
433 case HAL_PIXEL_FORMAT_ABGR_2101010:
434 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800435 default:
436 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
437 break;
438 }
439 return ADRENO_PIXELFORMAT_UNKNOWN;
440}
441
Naomi Luisa44100c2013-02-08 12:42:03 -0800442//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700443IAllocController* IAllocController::sController = NULL;
444IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700445{
446 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700447 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700448 }
449 return sController;
450}
451
452
453//-------------- IonController-----------------------//
454IonController::IonController()
455{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530456 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530457
458 char property[PROPERTY_VALUE_MAX];
459 property_get("video.disable.ubwc", property, "0");
460 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530461}
462
463void IonController::allocateIonMem()
464{
465 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700466}
467
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700468int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700469{
470 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500471 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700472 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700473
474 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700475 data.allocType = 0;
476
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530477 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400478 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
479 ionHeapId = ION_HEAP(SD_HEAP_ID);
480 /*
481 * There is currently no flag in ION for Secure Display
482 * VM. Please add it to the define once available.
483 */
484 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700485 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
486 ionHeapId = ION_HEAP(SD_HEAP_ID);
487 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530488 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400489 ionHeapId = ION_HEAP(CP_HEAP_ID);
490 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500491 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530492 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
493 //MM Heap is exclusively a secure heap.
494 //If it is used for non secure cases, fallback to IOMMU heap
495 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
496 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500497 trying to use system heap instead !!");
498 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500499 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700500
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700501 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500502 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700503
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700504 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500505 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700506
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530507 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500508 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700509
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500510 // if no ion heap flags are set, default to system heap
511 if(!ionHeapId)
512 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700513
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500514 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500516 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700520 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500521 } else {
522 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
523 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700524 }
525
526 return ret;
527}
528
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700529IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700530{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700531 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700532 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
533 memalloc = mIonAlloc;
534 } else {
535 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
536 }
537
538 return memalloc;
539}
540
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700541bool isMacroTileEnabled(int format, int usage)
542{
543 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700544 // Check whether GPU & MDSS supports MacroTiling feature
545 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530546 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700547 {
548 // check the format
549 switch(format)
550 {
551 case HAL_PIXEL_FORMAT_RGBA_8888:
552 case HAL_PIXEL_FORMAT_RGBX_8888:
553 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800554 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600555 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700556 {
557 tileEnabled = true;
558 // check the usage flags
559 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
560 GRALLOC_USAGE_SW_WRITE_MASK)) {
561 // Application intends to use CPU for rendering
562 tileEnabled = false;
563 }
564 break;
565 }
566 default:
567 break;
568 }
569 }
570 return tileEnabled;
571}
572
573// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800574unsigned int getSize(int format, int width, int height, int usage,
575 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700576
Sushil Chauhan65e26302015-01-14 10:48:57 -0800577 if (isUBwcEnabled(format, usage)) {
578 return getUBwcSize(width, height, format, alignedw, alignedh);
579 }
580
581 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700582 switch (format) {
583 case HAL_PIXEL_FORMAT_RGBA_8888:
584 case HAL_PIXEL_FORMAT_RGBX_8888:
585 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800586 case HAL_PIXEL_FORMAT_RGBA_1010102:
587 case HAL_PIXEL_FORMAT_ARGB_2101010:
588 case HAL_PIXEL_FORMAT_RGBX_1010102:
589 case HAL_PIXEL_FORMAT_XRGB_2101010:
590 case HAL_PIXEL_FORMAT_BGRA_1010102:
591 case HAL_PIXEL_FORMAT_ABGR_2101010:
592 case HAL_PIXEL_FORMAT_BGRX_1010102:
593 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700594 size = alignedw * alignedh * 4;
595 break;
596 case HAL_PIXEL_FORMAT_RGB_888:
597 size = alignedw * alignedh * 3;
598 break;
599 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600600 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700601 case HAL_PIXEL_FORMAT_RGBA_5551:
602 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700603 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700604 size = alignedw * alignedh * 2;
605 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700606 case HAL_PIXEL_FORMAT_RAW10:
607 size = ALIGN(alignedw * alignedh, 4096);
608 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700609
610 // adreno formats
611 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
612 size = ALIGN(alignedw*alignedh, 4096);
613 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
614 break;
615 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
616 // The chroma plane is subsampled,
617 // but the pitch in bytes is unchanged
618 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700619 size = ALIGN( alignedw * alignedh, 8192);
620 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
621 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700622 case HAL_PIXEL_FORMAT_YV12:
623 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
624 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800625 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700626 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400627 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700629 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700630 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800631 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
632 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400633 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800634 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800635 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
636 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, 4096);
637 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700638 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
639 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700640 case HAL_PIXEL_FORMAT_YCbCr_422_I:
641 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700642 if(width & 1) {
643 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800644 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700645 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700646 size = ALIGN(alignedw * alignedh * 2, 4096);
647 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700648 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400649 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700650 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700651 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530652 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
653 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
654 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400655 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400656 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400657 if(height != 1) {
658 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
659 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800660 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400661 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400662 size = width;
663 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700664 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700665 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
666 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500667 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
668 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
669 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
670 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
672 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
673 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
674 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
675 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
676 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
677 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
678 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
679 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
680 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
681 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
682 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
683 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
684 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
685 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
686 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
687 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
688 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
689 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
690 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
691 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
692 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
693 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800694 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500695 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
696 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700697 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500698 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800699 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700700 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700701 return size;
702}
703
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700704unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700705 int& alignedw, int &alignedh)
706{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700707 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700708
709 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
710 height,
711 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800712 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700713 alignedw,
714 alignedh);
715
Sushil Chauhan65e26302015-01-14 10:48:57 -0800716 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700717
718 return size;
719}
720
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700721
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700722unsigned int getBufferSizeAndDimensions(int width, int height, int format,
723 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700724{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700725 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700726
727 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
728 height,
729 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800730 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700731 alignedw,
732 alignedh);
733
Sushil Chauhan65e26302015-01-14 10:48:57 -0800734 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700735
736 return size;
737}
738
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700739void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700740 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700741{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700742 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700743
744 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
745 height,
746 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800747 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700748 alignedw,
749 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800750 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700751}
752
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700753void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
754 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800755{
756 // UBWC buffer has these 4 planes in the following sequence:
757 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
758 unsigned int y_meta_stride, y_meta_height, y_meta_size;
759 unsigned int y_stride, y_height, y_size;
760 unsigned int c_meta_stride, c_meta_height, c_meta_size;
761 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800762
763 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
764 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
765 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
766
767 y_stride = VENUS_Y_STRIDE(color_format, width);
768 y_height = VENUS_Y_SCANLINES(color_format, height);
769 y_size = ALIGN((y_stride * y_height), alignment);
770
771 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
772 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
773 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
774
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700775 ycbcr->y = (void*)(base + y_meta_size);
776 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
777 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800778 c_meta_size + 1);
779 ycbcr->ystride = y_stride;
780 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
781}
782
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700783void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
784 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800785{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800786 unsigned int ystride, cstride;
787
788 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700789 ycbcr->y = (void*)base;
790 ycbcr->cb = (void*)(base + ystride * height);
791 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800792 ycbcr->ystride = ystride;
793 ycbcr->cstride = cstride;
794 ycbcr->chroma_step = 2 * bpp;
795}
796
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400797int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
798{
799 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700800 int width = hnd->width;
801 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700802 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700803
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700804 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700805
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400806 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700807 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
808
809 // Check if UBWC buffer has been rendered in linear format.
810 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
811 format = metadata->linearFormat;
812 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400813
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700814 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700815 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700816 int usage = 0;
817
818 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
819 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
820 }
821
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700822 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700823 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700824 }
825
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400826 // Get the chroma offsets from the handle width/height. We take advantage
827 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700828 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400829 //Semiplanar
830 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
831 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
832 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
833 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700834 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800835 break;
836
837 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700838 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400839 break;
840
Sushil Chauhan4686c972015-02-20 15:44:52 -0800841 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700842 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
843 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800844 ycbcr->chroma_step = 2;
845 break;
846
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800847 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700848 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
849 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800850 ycbcr->chroma_step = 3;
851 break;
852
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400853 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
854 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
855 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530856 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400857 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700858 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700859 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700860 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800861 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400862 break;
863
864 //Planar
865 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700866 ystride = width;
867 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400868 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700869 ycbcr->cr = (void*)(hnd->base + ystride * height);
870 ycbcr->cb = (void*)(hnd->base + ystride * height +
871 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400872 ycbcr->ystride = ystride;
873 ycbcr->cstride = cstride;
874 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400875 break;
876 //Unsupported formats
877 case HAL_PIXEL_FORMAT_YCbCr_422_I:
878 case HAL_PIXEL_FORMAT_YCrCb_422_I:
879 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
880 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700881 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400882 err = -EINVAL;
883 }
884 return err;
885
886}
887
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700888
889
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700890// Allocate buffer from width, height and format into a
891// private_handle_t. It is the responsibility of the caller
892// to free the buffer using the free_buffer function
893int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
894{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700895 alloc_data data;
896 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700897 gralloc::IAllocController* sAlloc =
898 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700899 data.base = 0;
900 data.fd = -1;
901 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700902 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
903 alignedh);
904
Naseer Ahmed29a26812012-06-14 00:56:20 -0700905 data.align = getpagesize();
906 data.uncached = useUncached(usage);
907 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700908
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700909 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700910 if (0 != err) {
911 ALOGE("%s: allocate failed", __FUNCTION__);
912 return -ENOMEM;
913 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700914
radhakrishnad7131e62015-10-13 12:32:30 +0530915 if(isUBwcEnabled(format, usage)) {
916 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
917 }
918
Naseer Ahmed29a26812012-06-14 00:56:20 -0700919 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700920 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700921 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700922 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700923 hnd->offset = data.offset;
924 hnd->gpuaddr = 0;
925 *pHnd = hnd;
926 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700927}
928
929void free_buffer(private_handle_t *hnd)
930{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700931 gralloc::IAllocController* sAlloc =
932 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700933 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700934 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700935 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
936 }
937 if(hnd)
938 delete hnd;
939
940}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800941
942// UBWC helper functions
943static bool isUBwcFormat(int format)
944{
945 // Explicitly defined UBWC formats
946 switch(format)
947 {
948 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800949 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800950 return true;
951 default:
952 return false;
953 }
954}
955
956static bool isUBwcSupported(int format)
957{
Sushil Chauhan01361412016-04-25 16:36:18 -0700958 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
959 // Existing HAL formats with UBWC support
960 switch(format)
961 {
962 case HAL_PIXEL_FORMAT_BGR_565:
963 case HAL_PIXEL_FORMAT_RGBA_8888:
964 case HAL_PIXEL_FORMAT_RGBX_8888:
965 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
966 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
967 case HAL_PIXEL_FORMAT_RGBA_1010102:
968 case HAL_PIXEL_FORMAT_RGBX_1010102:
969 return true;
970 default:
971 break;
972 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800973 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700974 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800975}
976
977bool isUBwcEnabled(int format, int usage)
978{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800979 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
980 if (isUBwcFormat(format))
981 return true;
982
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530983 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
984 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
985 return false;
986 }
987
Sushil Chauhan7807d192015-08-13 10:10:48 -0700988 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
989 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
990 // usage flag and MDP supports the format.
991 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
992 bool enable = true;
993 // Query GPU for UBWC only if buffer is intended to be used by GPU.
994 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
995 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
996 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800997 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700998 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
999 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -08001000 return true;
1001 }
1002 }
1003 return false;
1004}
1005
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001006static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -08001007 int& aligned_w, int& aligned_h)
1008{
1009 switch (format)
1010 {
1011 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1012 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1013 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1014 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
1015 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
1016 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001017 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -07001018 // The macro returns the stride which is 4/3 times the width, hence * 3/4
1019 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001020 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
1021 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001022 default:
1023 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1024 aligned_w = 0;
1025 aligned_h = 0;
1026 break;
1027 }
1028}
1029
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001030static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001031{
1032 block_width = 0;
1033 block_height = 0;
1034
1035 switch(bpp)
1036 {
1037 case 2:
1038 case 4:
1039 block_width = 16;
1040 block_height = 4;
1041 break;
1042 case 8:
1043 block_width = 8;
1044 block_height = 4;
1045 break;
1046 case 16:
1047 block_width = 4;
1048 block_height = 4;
1049 break;
1050 default:
1051 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1052 break;
1053 }
1054}
1055
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001056static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001057{
1058 unsigned int size = 0;
1059 int meta_width, meta_height;
1060 int block_width, block_height;
1061
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001062 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001063
1064 if (!block_width || !block_height) {
1065 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1066 return size;
1067 }
1068
1069 // Align meta buffer height to 16 blocks
1070 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1071
1072 // Align meta buffer width to 64 blocks
1073 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1074
1075 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001076 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001077 return size;
1078}
1079
1080static unsigned int getUBwcSize(int width, int height, int format,
1081 const int alignedw, const int alignedh) {
1082
1083 unsigned int size = 0;
1084 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001085 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001086 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001087 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001088 break;
1089 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001090 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001091 case HAL_PIXEL_FORMAT_RGBA_1010102:
1092 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001093 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001094 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001095 break;
1096 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1097 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1098 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1099 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1100 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001101 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1102 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1103 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001104 default:
1105 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1106 break;
1107 }
1108 return size;
1109}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001110
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001111int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001112{
1113 int err = 0;
1114
1115 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001116 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001117 return -EINVAL;
1118 }
1119
1120 // linear buffer
1121 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001122 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001123 return err;
1124 }
1125
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001126 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001127 unsigned int meta_size = 0;
1128 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001129 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001130 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001131 break;
1132 case HAL_PIXEL_FORMAT_RGBA_8888:
1133 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001134 case HAL_PIXEL_FORMAT_RGBA_1010102:
1135 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001136 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001137 break;
1138 default:
1139 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1140 err = -EINVAL;
1141 break;
1142 }
1143
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001144 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001145 return err;
1146}