blob: ae167baf9a1c486c01a9727ba642c26602303f31 [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:
278 aligned_w = ALIGN(width, 16);
279 break;
280 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
281 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
282 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
283 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
284 break;
285 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
286 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
287 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
288 break;
289 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400290 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800291 break;
292 case HAL_PIXEL_FORMAT_NV21_ZSL:
293 aligned_w = ALIGN(width, 64);
294 aligned_h = ALIGN(height, 64);
295 break;
296 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
310 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
311 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
312 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
313 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
314 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
315 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
316 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
317 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
318 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
319 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
320 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
321 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
322 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
323 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
324 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
325 int bytesPerPixel = 0;
326 int raster_mode = 0; //Adreno unknown raster mode.
327 int padding_threshold = 512; //Threshold for padding
328 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800329
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800330 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
331 width, height, format, 0,raster_mode, padding_threshold,
332 &aligned_w, &aligned_h, &bytesPerPixel);
333 } else {
334 ALOGW("%s: Warning!! Symbols" \
335 " compute_compressedfmt_aligned_width_and_height" \
336 " not found", __FUNCTION__);
337 }
338 break;
339 default: break;
340 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800341 }
342}
343
344void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
345 int tile_enabled, int& aligned_w, int& aligned_h)
346{
347 aligned_w = ALIGN(width, 32);
348 aligned_h = ALIGN(height, 32);
349
350 // Don't add any additional padding if debug.gralloc.map_fb_memory
351 // is enabled
352 char property[PROPERTY_VALUE_MAX];
353 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
354 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
355 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
356 return;
357 }
358
359 int bpp = 4;
360 switch(format)
361 {
362 case HAL_PIXEL_FORMAT_RGB_888:
363 bpp = 3;
364 break;
365 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600366 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800367 case HAL_PIXEL_FORMAT_RGBA_5551:
368 case HAL_PIXEL_FORMAT_RGBA_4444:
369 bpp = 2;
370 break;
371 default: break;
372 }
373
374 if (libadreno_utils) {
375 int raster_mode = 0; // Adreno unknown raster mode.
376 int padding_threshold = 512; // Threshold for padding surfaces.
377 // the function below computes aligned width and aligned height
378 // based on linear or macro tile mode selected.
379 if(LINK_adreno_compute_aligned_width_and_height) {
380 LINK_adreno_compute_aligned_width_and_height(width,
381 height, bpp, tile_enabled,
382 raster_mode, padding_threshold,
383 &aligned_w, &aligned_h);
384
385 } else if(LINK_adreno_compute_padding) {
386 int surface_tile_height = 1; // Linear surface
387 aligned_w = LINK_adreno_compute_padding(width, bpp,
388 surface_tile_height, raster_mode,
389 padding_threshold);
390 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
391 __FUNCTION__);
392 } else {
393 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
394 "compute_aligned_width_and_height not found", __FUNCTION__);
395 }
396 }
397}
398
399int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
400{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700401 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800402 if (LINK_adreno_isUBWCSupportedByGpu) {
403 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
404 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
405 }
406 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800407 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800408}
409
Sushil Chauhan082acd62015-01-14 16:49:29 -0800410ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
411{
412 switch (hal_format) {
413 case HAL_PIXEL_FORMAT_RGBA_8888:
414 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700415 case HAL_PIXEL_FORMAT_RGBX_8888:
416 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800417 case HAL_PIXEL_FORMAT_RGB_565:
418 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600419 case HAL_PIXEL_FORMAT_BGR_565:
420 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800421 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800422 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800423 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
424 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800425 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800426 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
427 return ADRENO_PIXELFORMAT_TP10;
428 case HAL_PIXEL_FORMAT_RGBA_1010102:
429 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
430 case HAL_PIXEL_FORMAT_RGBX_1010102:
431 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
432 case HAL_PIXEL_FORMAT_ABGR_2101010:
433 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800434 default:
435 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
436 break;
437 }
438 return ADRENO_PIXELFORMAT_UNKNOWN;
439}
440
Naomi Luisa44100c2013-02-08 12:42:03 -0800441//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700442IAllocController* IAllocController::sController = NULL;
443IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700444{
445 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700446 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700447 }
448 return sController;
449}
450
451
452//-------------- IonController-----------------------//
453IonController::IonController()
454{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530455 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530456
457 char property[PROPERTY_VALUE_MAX];
458 property_get("video.disable.ubwc", property, "0");
459 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530460}
461
462void IonController::allocateIonMem()
463{
464 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700465}
466
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700467int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700468{
469 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500470 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700471 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700472
473 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700474 data.allocType = 0;
475
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530476 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400477 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
478 ionHeapId = ION_HEAP(SD_HEAP_ID);
479 /*
480 * There is currently no flag in ION for Secure Display
481 * VM. Please add it to the define once available.
482 */
483 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700484 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
485 ionHeapId = ION_HEAP(SD_HEAP_ID);
486 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530487 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400488 ionHeapId = ION_HEAP(CP_HEAP_ID);
489 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500490 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530491 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
492 //MM Heap is exclusively a secure heap.
493 //If it is used for non secure cases, fallback to IOMMU heap
494 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
495 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500496 trying to use system heap instead !!");
497 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500498 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700500 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500501 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700502
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700503 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500504 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530506 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500507 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700508
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500509 // if no ion heap flags are set, default to system heap
510 if(!ionHeapId)
511 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500513 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700514 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500515 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700517
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700519 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500520 } else {
521 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
522 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700523 }
524
525 return ret;
526}
527
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700528IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700529{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700530 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700531 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
532 memalloc = mIonAlloc;
533 } else {
534 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
535 }
536
537 return memalloc;
538}
539
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700540bool isMacroTileEnabled(int format, int usage)
541{
542 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700543 // Check whether GPU & MDSS supports MacroTiling feature
544 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530545 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700546 {
547 // check the format
548 switch(format)
549 {
550 case HAL_PIXEL_FORMAT_RGBA_8888:
551 case HAL_PIXEL_FORMAT_RGBX_8888:
552 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800553 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600554 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700555 {
556 tileEnabled = true;
557 // check the usage flags
558 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
559 GRALLOC_USAGE_SW_WRITE_MASK)) {
560 // Application intends to use CPU for rendering
561 tileEnabled = false;
562 }
563 break;
564 }
565 default:
566 break;
567 }
568 }
569 return tileEnabled;
570}
571
572// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800573unsigned int getSize(int format, int width, int height, int usage,
574 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700575
Sushil Chauhan65e26302015-01-14 10:48:57 -0800576 if (isUBwcEnabled(format, usage)) {
577 return getUBwcSize(width, height, format, alignedw, alignedh);
578 }
579
580 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700581 switch (format) {
582 case HAL_PIXEL_FORMAT_RGBA_8888:
583 case HAL_PIXEL_FORMAT_RGBX_8888:
584 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800585 case HAL_PIXEL_FORMAT_RGBA_1010102:
586 case HAL_PIXEL_FORMAT_ARGB_2101010:
587 case HAL_PIXEL_FORMAT_RGBX_1010102:
588 case HAL_PIXEL_FORMAT_XRGB_2101010:
589 case HAL_PIXEL_FORMAT_BGRA_1010102:
590 case HAL_PIXEL_FORMAT_ABGR_2101010:
591 case HAL_PIXEL_FORMAT_BGRX_1010102:
592 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700593 size = alignedw * alignedh * 4;
594 break;
595 case HAL_PIXEL_FORMAT_RGB_888:
596 size = alignedw * alignedh * 3;
597 break;
598 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600599 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700600 case HAL_PIXEL_FORMAT_RGBA_5551:
601 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700602 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700603 size = alignedw * alignedh * 2;
604 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700605 case HAL_PIXEL_FORMAT_RAW10:
606 size = ALIGN(alignedw * alignedh, 4096);
607 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700608
609 // adreno formats
610 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
611 size = ALIGN(alignedw*alignedh, 4096);
612 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
613 break;
614 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
615 // The chroma plane is subsampled,
616 // but the pitch in bytes is unchanged
617 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700618 size = ALIGN( alignedw * alignedh, 8192);
619 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
620 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700621 case HAL_PIXEL_FORMAT_YV12:
622 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
623 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800624 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700625 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400626 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700627 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700628 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700629 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800630 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
631 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400632 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800633 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700634 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
635 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700636 case HAL_PIXEL_FORMAT_YCbCr_422_I:
637 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700638 if(width & 1) {
639 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800640 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700641 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700642 size = ALIGN(alignedw * alignedh * 2, 4096);
643 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700644 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400645 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700646 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700647 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530648 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
649 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
650 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400651 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400652 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400653 if(height != 1) {
654 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
655 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800656 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400657 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400658 size = width;
659 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700660 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700661 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
662 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500663 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
664 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
665 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
666 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
667 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
668 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
669 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
670 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
672 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
673 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
674 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
675 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
676 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
677 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
678 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
679 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
680 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
681 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
682 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
683 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
684 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
685 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
686 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
687 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
688 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
689 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800690 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500691 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
692 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700693 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500694 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800695 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700696 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700697 return size;
698}
699
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700700unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700701 int& alignedw, int &alignedh)
702{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700703 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700704
705 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
706 height,
707 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800708 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700709 alignedw,
710 alignedh);
711
Sushil Chauhan65e26302015-01-14 10:48:57 -0800712 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700713
714 return size;
715}
716
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700717
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700718unsigned int getBufferSizeAndDimensions(int width, int height, int format,
719 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700720{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700721 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700722
723 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
724 height,
725 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800726 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700727 alignedw,
728 alignedh);
729
Sushil Chauhan65e26302015-01-14 10:48:57 -0800730 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700731
732 return size;
733}
734
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700735void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700736 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700737{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700738 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700739
740 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
741 height,
742 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800743 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700744 alignedw,
745 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800746 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700747}
748
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700749void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
750 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800751{
752 // UBWC buffer has these 4 planes in the following sequence:
753 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
754 unsigned int y_meta_stride, y_meta_height, y_meta_size;
755 unsigned int y_stride, y_height, y_size;
756 unsigned int c_meta_stride, c_meta_height, c_meta_size;
757 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800758
759 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
760 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
761 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
762
763 y_stride = VENUS_Y_STRIDE(color_format, width);
764 y_height = VENUS_Y_SCANLINES(color_format, height);
765 y_size = ALIGN((y_stride * y_height), alignment);
766
767 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
768 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
769 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
770
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700771 ycbcr->y = (void*)(base + y_meta_size);
772 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
773 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800774 c_meta_size + 1);
775 ycbcr->ystride = y_stride;
776 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
777}
778
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700779void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
780 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800781{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800782 unsigned int ystride, cstride;
783
784 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700785 ycbcr->y = (void*)base;
786 ycbcr->cb = (void*)(base + ystride * height);
787 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800788 ycbcr->ystride = ystride;
789 ycbcr->cstride = cstride;
790 ycbcr->chroma_step = 2 * bpp;
791}
792
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400793int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
794{
795 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700796 int width = hnd->width;
797 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700798 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700799
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700800 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700801
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400802 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700803 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
804
805 // Check if UBWC buffer has been rendered in linear format.
806 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
807 format = metadata->linearFormat;
808 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400809
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700810 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700811 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700812 int usage = 0;
813
814 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
815 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
816 }
817
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700818 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700819 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700820 }
821
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400822 // Get the chroma offsets from the handle width/height. We take advantage
823 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700824 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400825 //Semiplanar
826 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
827 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
828 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
829 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700830 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800831 break;
832
833 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700834 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400835 break;
836
Sushil Chauhan4686c972015-02-20 15:44:52 -0800837 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700838 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
839 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800840 ycbcr->chroma_step = 2;
841 break;
842
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800843 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700844 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
845 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800846 ycbcr->chroma_step = 3;
847 break;
848
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400849 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
850 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
851 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530852 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400853 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700854 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700855 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700856 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800857 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400858 break;
859
860 //Planar
861 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700862 ystride = width;
863 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400864 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700865 ycbcr->cr = (void*)(hnd->base + ystride * height);
866 ycbcr->cb = (void*)(hnd->base + ystride * height +
867 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400868 ycbcr->ystride = ystride;
869 ycbcr->cstride = cstride;
870 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400871 break;
872 //Unsupported formats
873 case HAL_PIXEL_FORMAT_YCbCr_422_I:
874 case HAL_PIXEL_FORMAT_YCrCb_422_I:
875 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
876 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700877 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400878 err = -EINVAL;
879 }
880 return err;
881
882}
883
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700884
885
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700886// Allocate buffer from width, height and format into a
887// private_handle_t. It is the responsibility of the caller
888// to free the buffer using the free_buffer function
889int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
890{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700891 alloc_data data;
892 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700893 gralloc::IAllocController* sAlloc =
894 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700895 data.base = 0;
896 data.fd = -1;
897 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700898 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
899 alignedh);
900
Naseer Ahmed29a26812012-06-14 00:56:20 -0700901 data.align = getpagesize();
902 data.uncached = useUncached(usage);
903 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700904
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700905 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700906 if (0 != err) {
907 ALOGE("%s: allocate failed", __FUNCTION__);
908 return -ENOMEM;
909 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700910
radhakrishnad7131e62015-10-13 12:32:30 +0530911 if(isUBwcEnabled(format, usage)) {
912 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
913 }
914
Naseer Ahmed29a26812012-06-14 00:56:20 -0700915 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700916 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700917 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700918 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700919 hnd->offset = data.offset;
920 hnd->gpuaddr = 0;
921 *pHnd = hnd;
922 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700923}
924
925void free_buffer(private_handle_t *hnd)
926{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700927 gralloc::IAllocController* sAlloc =
928 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700929 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700930 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700931 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
932 }
933 if(hnd)
934 delete hnd;
935
936}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800937
938// UBWC helper functions
939static bool isUBwcFormat(int format)
940{
941 // Explicitly defined UBWC formats
942 switch(format)
943 {
944 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800945 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800946 return true;
947 default:
948 return false;
949 }
950}
951
952static bool isUBwcSupported(int format)
953{
Sushil Chauhan01361412016-04-25 16:36:18 -0700954 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
955 // Existing HAL formats with UBWC support
956 switch(format)
957 {
958 case HAL_PIXEL_FORMAT_BGR_565:
959 case HAL_PIXEL_FORMAT_RGBA_8888:
960 case HAL_PIXEL_FORMAT_RGBX_8888:
961 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
962 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
963 case HAL_PIXEL_FORMAT_RGBA_1010102:
964 case HAL_PIXEL_FORMAT_RGBX_1010102:
965 return true;
966 default:
967 break;
968 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800969 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700970 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800971}
972
973bool isUBwcEnabled(int format, int usage)
974{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800975 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
976 if (isUBwcFormat(format))
977 return true;
978
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530979 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
980 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
981 return false;
982 }
983
Sushil Chauhan7807d192015-08-13 10:10:48 -0700984 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
985 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
986 // usage flag and MDP supports the format.
987 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
988 bool enable = true;
989 // Query GPU for UBWC only if buffer is intended to be used by GPU.
990 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
991 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
992 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800993 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700994 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
995 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800996 return true;
997 }
998 }
999 return false;
1000}
1001
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001002static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -08001003 int& aligned_w, int& aligned_h)
1004{
1005 switch (format)
1006 {
1007 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1008 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1009 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1010 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
1011 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
1012 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001013 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -07001014 // The macro returns the stride which is 4/3 times the width, hence * 3/4
1015 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001016 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
1017 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001018 default:
1019 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1020 aligned_w = 0;
1021 aligned_h = 0;
1022 break;
1023 }
1024}
1025
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001026static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001027{
1028 block_width = 0;
1029 block_height = 0;
1030
1031 switch(bpp)
1032 {
1033 case 2:
1034 case 4:
1035 block_width = 16;
1036 block_height = 4;
1037 break;
1038 case 8:
1039 block_width = 8;
1040 block_height = 4;
1041 break;
1042 case 16:
1043 block_width = 4;
1044 block_height = 4;
1045 break;
1046 default:
1047 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1048 break;
1049 }
1050}
1051
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001052static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001053{
1054 unsigned int size = 0;
1055 int meta_width, meta_height;
1056 int block_width, block_height;
1057
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001058 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001059
1060 if (!block_width || !block_height) {
1061 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1062 return size;
1063 }
1064
1065 // Align meta buffer height to 16 blocks
1066 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1067
1068 // Align meta buffer width to 64 blocks
1069 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1070
1071 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001072 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001073 return size;
1074}
1075
1076static unsigned int getUBwcSize(int width, int height, int format,
1077 const int alignedw, const int alignedh) {
1078
1079 unsigned int size = 0;
1080 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001081 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001082 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001083 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001084 break;
1085 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001086 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001087 case HAL_PIXEL_FORMAT_RGBA_1010102:
1088 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001089 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001090 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001091 break;
1092 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1093 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1094 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1095 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1096 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001097 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1098 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1099 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001100 default:
1101 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1102 break;
1103 }
1104 return size;
1105}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001106
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001107int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001108{
1109 int err = 0;
1110
1111 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001112 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001113 return -EINVAL;
1114 }
1115
1116 // linear buffer
1117 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001118 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001119 return err;
1120 }
1121
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001122 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001123 unsigned int meta_size = 0;
1124 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001125 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001126 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001127 break;
1128 case HAL_PIXEL_FORMAT_RGBA_8888:
1129 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001130 case HAL_PIXEL_FORMAT_RGBA_1010102:
1131 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001132 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001133 break;
1134 default:
1135 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1136 err = -EINVAL;
1137 break;
1138 }
1139
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001140 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001141 return err;
1142}