blob: cb57e9c22a8f6317477649719bd69d749d5ec1d7 [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:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530220 case HAL_PIXEL_FORMAT_BGR_888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800221 case HAL_PIXEL_FORMAT_RGBA_1010102:
222 case HAL_PIXEL_FORMAT_ARGB_2101010:
223 case HAL_PIXEL_FORMAT_RGBX_1010102:
224 case HAL_PIXEL_FORMAT_XRGB_2101010:
225 case HAL_PIXEL_FORMAT_BGRA_1010102:
226 case HAL_PIXEL_FORMAT_ABGR_2101010:
227 case HAL_PIXEL_FORMAT_BGRX_1010102:
228 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700229 is_rgb_format = true;
230 break;
231 default:
232 break;
233 }
234
235 return is_rgb_format;
236}
237
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800238void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800239 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800240{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700241 bool ubwc_enabled = isUBwcEnabled(format, usage);
242
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800243 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700244 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700245 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700246 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800247 } else if (ubwc_enabled) {
248 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
249 } else {
250 aligned_w = width;
251 aligned_h = height;
252 int alignment = 32;
253 switch (format)
254 {
255 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
256 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
257 if (LINK_adreno_get_gpu_pixel_alignment) {
258 alignment = LINK_adreno_get_gpu_pixel_alignment();
259 }
260 aligned_w = ALIGN(width, alignment);
261 break;
262 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
263 aligned_w = ALIGN(width, alignment);
264 break;
265 case HAL_PIXEL_FORMAT_RAW16:
266 aligned_w = ALIGN(width, 16);
267 break;
268 case HAL_PIXEL_FORMAT_RAW10:
269 aligned_w = ALIGN(width * 10 / 8, 8);
270 break;
271 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
272 aligned_w = ALIGN(width, 128);
273 break;
274 case HAL_PIXEL_FORMAT_YV12:
275 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
276 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
277 case HAL_PIXEL_FORMAT_YCbCr_422_I:
278 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800279 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530280 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800281 aligned_w = ALIGN(width, 16);
282 break;
283 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
284 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
285 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
286 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
287 break;
288 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
289 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
290 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
291 break;
292 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400293 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800294 break;
295 case HAL_PIXEL_FORMAT_NV21_ZSL:
296 aligned_w = ALIGN(width, 64);
297 aligned_h = ALIGN(height, 64);
298 break;
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
307 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
308 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
309 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
310 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
311 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
312 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
313 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
314 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
315 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
316 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
317 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
318 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
319 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
320 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
321 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
322 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
323 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
324 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
325 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
326 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
327 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
328 int bytesPerPixel = 0;
329 int raster_mode = 0; //Adreno unknown raster mode.
330 int padding_threshold = 512; //Threshold for padding
331 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800332
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800333 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
334 width, height, format, 0,raster_mode, padding_threshold,
335 &aligned_w, &aligned_h, &bytesPerPixel);
336 } else {
337 ALOGW("%s: Warning!! Symbols" \
338 " compute_compressedfmt_aligned_width_and_height" \
339 " not found", __FUNCTION__);
340 }
341 break;
342 default: break;
343 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800344 }
345}
346
347void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
348 int tile_enabled, int& aligned_w, int& aligned_h)
349{
350 aligned_w = ALIGN(width, 32);
351 aligned_h = ALIGN(height, 32);
352
353 // Don't add any additional padding if debug.gralloc.map_fb_memory
354 // is enabled
355 char property[PROPERTY_VALUE_MAX];
356 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
357 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
358 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
359 return;
360 }
361
362 int bpp = 4;
363 switch(format)
364 {
365 case HAL_PIXEL_FORMAT_RGB_888:
366 bpp = 3;
367 break;
368 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600369 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800370 case HAL_PIXEL_FORMAT_RGBA_5551:
371 case HAL_PIXEL_FORMAT_RGBA_4444:
372 bpp = 2;
373 break;
374 default: break;
375 }
376
377 if (libadreno_utils) {
378 int raster_mode = 0; // Adreno unknown raster mode.
379 int padding_threshold = 512; // Threshold for padding surfaces.
380 // the function below computes aligned width and aligned height
381 // based on linear or macro tile mode selected.
382 if(LINK_adreno_compute_aligned_width_and_height) {
383 LINK_adreno_compute_aligned_width_and_height(width,
384 height, bpp, tile_enabled,
385 raster_mode, padding_threshold,
386 &aligned_w, &aligned_h);
387
388 } else if(LINK_adreno_compute_padding) {
389 int surface_tile_height = 1; // Linear surface
390 aligned_w = LINK_adreno_compute_padding(width, bpp,
391 surface_tile_height, raster_mode,
392 padding_threshold);
393 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
394 __FUNCTION__);
395 } else {
396 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
397 "compute_aligned_width_and_height not found", __FUNCTION__);
398 }
399 }
400}
401
402int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
403{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700404 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800405 if (LINK_adreno_isUBWCSupportedByGpu) {
406 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
407 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
408 }
409 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800410 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800411}
412
Sushil Chauhan082acd62015-01-14 16:49:29 -0800413ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
414{
415 switch (hal_format) {
416 case HAL_PIXEL_FORMAT_RGBA_8888:
417 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700418 case HAL_PIXEL_FORMAT_RGBX_8888:
419 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800420 case HAL_PIXEL_FORMAT_RGB_565:
421 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600422 case HAL_PIXEL_FORMAT_BGR_565:
423 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800424 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800425 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800426 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
427 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800428 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800429 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
430 return ADRENO_PIXELFORMAT_TP10;
431 case HAL_PIXEL_FORMAT_RGBA_1010102:
432 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
433 case HAL_PIXEL_FORMAT_RGBX_1010102:
434 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
435 case HAL_PIXEL_FORMAT_ABGR_2101010:
436 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800437 default:
438 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
439 break;
440 }
441 return ADRENO_PIXELFORMAT_UNKNOWN;
442}
443
Naomi Luisa44100c2013-02-08 12:42:03 -0800444//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700445IAllocController* IAllocController::sController = NULL;
446IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700447{
448 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700449 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700450 }
451 return sController;
452}
453
454
455//-------------- IonController-----------------------//
456IonController::IonController()
457{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530458 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530459
460 char property[PROPERTY_VALUE_MAX];
461 property_get("video.disable.ubwc", property, "0");
462 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530463}
464
465void IonController::allocateIonMem()
466{
467 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700468}
469
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700470int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700471{
472 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500473 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700474 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700475
476 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700477 data.allocType = 0;
478
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530479 if(usage & GRALLOC_USAGE_PROTECTED) {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400480 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
481 ionHeapId = ION_HEAP(SD_HEAP_ID);
482 /*
483 * There is currently no flag in ION for Secure Display
484 * VM. Please add it to the define once available.
485 */
486 ionFlags |= ION_SD_FLAGS;
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700487 } else if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
488 ionHeapId = ION_HEAP(SD_HEAP_ID);
489 ionFlags |= (usage & GRALLOC_USAGE_HW_COMPOSER) ? ION_SC_PREVIEW_FLAGS : ION_SC_FLAGS;
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530490 } else {
Naseer Ahmed934a4c32016-04-11 17:20:02 -0400491 ionHeapId = ION_HEAP(CP_HEAP_ID);
492 ionFlags |= ION_CP_FLAGS;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500493 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530494 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
495 //MM Heap is exclusively a secure heap.
496 //If it is used for non secure cases, fallback to IOMMU heap
497 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
498 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500499 trying to use system heap instead !!");
500 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500501 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700502
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700503 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500504 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700505
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700506 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500507 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700508
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530509 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500510 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700511
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500512 // if no ion heap flags are set, default to system heap
513 if(!ionHeapId)
514 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700515
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500516 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700517 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500518 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700520
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700521 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700522 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500523 } else {
524 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
525 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700526 }
527
528 return ret;
529}
530
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700531IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700532{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700533 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700534 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
535 memalloc = mIonAlloc;
536 } else {
537 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
538 }
539
540 return memalloc;
541}
542
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700543bool isMacroTileEnabled(int format, int usage)
544{
545 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700546 // Check whether GPU & MDSS supports MacroTiling feature
547 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530548 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700549 {
550 // check the format
551 switch(format)
552 {
553 case HAL_PIXEL_FORMAT_RGBA_8888:
554 case HAL_PIXEL_FORMAT_RGBX_8888:
555 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800556 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600557 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700558 {
559 tileEnabled = true;
560 // check the usage flags
561 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
562 GRALLOC_USAGE_SW_WRITE_MASK)) {
563 // Application intends to use CPU for rendering
564 tileEnabled = false;
565 }
566 break;
567 }
568 default:
569 break;
570 }
571 }
572 return tileEnabled;
573}
574
575// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800576unsigned int getSize(int format, int width, int height, int usage,
577 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700578
Sushil Chauhan65e26302015-01-14 10:48:57 -0800579 if (isUBwcEnabled(format, usage)) {
580 return getUBwcSize(width, height, format, alignedw, alignedh);
581 }
582
583 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700584 switch (format) {
585 case HAL_PIXEL_FORMAT_RGBA_8888:
586 case HAL_PIXEL_FORMAT_RGBX_8888:
587 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800588 case HAL_PIXEL_FORMAT_RGBA_1010102:
589 case HAL_PIXEL_FORMAT_ARGB_2101010:
590 case HAL_PIXEL_FORMAT_RGBX_1010102:
591 case HAL_PIXEL_FORMAT_XRGB_2101010:
592 case HAL_PIXEL_FORMAT_BGRA_1010102:
593 case HAL_PIXEL_FORMAT_ABGR_2101010:
594 case HAL_PIXEL_FORMAT_BGRX_1010102:
595 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700596 size = alignedw * alignedh * 4;
597 break;
598 case HAL_PIXEL_FORMAT_RGB_888:
599 size = alignedw * alignedh * 3;
600 break;
601 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600602 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700603 case HAL_PIXEL_FORMAT_RGBA_5551:
604 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700605 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700606 size = alignedw * alignedh * 2;
607 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700608 case HAL_PIXEL_FORMAT_RAW10:
609 size = ALIGN(alignedw * alignedh, 4096);
610 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700611
612 // adreno formats
613 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
614 size = ALIGN(alignedw*alignedh, 4096);
615 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
616 break;
617 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
618 // The chroma plane is subsampled,
619 // but the pitch in bytes is unchanged
620 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700621 size = ALIGN( alignedw * alignedh, 8192);
622 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
623 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700624 case HAL_PIXEL_FORMAT_YV12:
625 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
626 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800627 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700628 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400629 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700630 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700631 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700632 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800633 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
634 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400635 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800636 break;
Ramkumar Radhakrishnan3083fe92016-12-15 18:05:40 -0800637 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
638 size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, 4096);
639 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700640 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
641 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700642 case HAL_PIXEL_FORMAT_YCbCr_422_I:
643 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Rahul Sharma61173ea2017-01-05 09:15:40 +0530644 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700645 if(width & 1) {
646 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800647 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700648 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700649 size = ALIGN(alignedw * alignedh * 2, 4096);
650 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700651 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400652 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700653 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700654 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530655 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
656 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
657 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400658 case HAL_PIXEL_FORMAT_BLOB:
Naseer Ahmed6c6c58a2016-05-03 16:27:17 -0400659 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400660 if(height != 1) {
661 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
662 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800663 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400664 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400665 size = width;
666 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700667 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700668 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
669 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500670 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
672 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
673 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
674 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
675 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
676 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
677 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
678 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
679 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
680 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
681 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
682 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
683 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
684 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
685 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
686 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
687 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
688 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
689 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
690 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
691 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
692 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
693 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
694 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
695 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
696 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800697 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500698 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
699 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700700 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500701 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800702 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700703 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700704 return size;
705}
706
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700707unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700708 int& alignedw, int &alignedh)
709{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700710 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700711
712 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
713 height,
714 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800715 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700716 alignedw,
717 alignedh);
718
Sushil Chauhan65e26302015-01-14 10:48:57 -0800719 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700720
721 return size;
722}
723
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700724
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700725unsigned int getBufferSizeAndDimensions(int width, int height, int format,
726 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700727{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700728 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700729
730 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
731 height,
732 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800733 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700734 alignedw,
735 alignedh);
736
Sushil Chauhan65e26302015-01-14 10:48:57 -0800737 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700738
739 return size;
740}
741
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700742void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700743 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700744{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700745 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700746
747 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
748 height,
749 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800750 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700751 alignedw,
752 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800753 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700754}
755
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700756void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
757 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800758{
759 // UBWC buffer has these 4 planes in the following sequence:
760 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
761 unsigned int y_meta_stride, y_meta_height, y_meta_size;
762 unsigned int y_stride, y_height, y_size;
763 unsigned int c_meta_stride, c_meta_height, c_meta_size;
764 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800765
766 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
767 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
768 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
769
770 y_stride = VENUS_Y_STRIDE(color_format, width);
771 y_height = VENUS_Y_SCANLINES(color_format, height);
772 y_size = ALIGN((y_stride * y_height), alignment);
773
774 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
775 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
776 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
777
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700778 ycbcr->y = (void*)(base + y_meta_size);
779 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
780 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800781 c_meta_size + 1);
782 ycbcr->ystride = y_stride;
783 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
784}
785
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700786void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
787 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800788{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800789 unsigned int ystride, cstride;
790
791 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700792 ycbcr->y = (void*)base;
793 ycbcr->cb = (void*)(base + ystride * height);
794 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800795 ycbcr->ystride = ystride;
796 ycbcr->cstride = cstride;
797 ycbcr->chroma_step = 2 * bpp;
798}
799
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400800int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
801{
802 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700803 int width = hnd->width;
804 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700805 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700806
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700807 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700808
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400809 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700810 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
811
812 // Check if UBWC buffer has been rendered in linear format.
813 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
814 format = metadata->linearFormat;
815 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400816
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700817 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700818 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700819 int usage = 0;
820
821 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
822 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
823 }
824
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700825 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700826 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700827 }
828
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400829 // Get the chroma offsets from the handle width/height. We take advantage
830 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700831 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400832 //Semiplanar
833 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
834 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
835 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
836 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700837 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800838 break;
839
840 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700841 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400842 break;
843
Sushil Chauhan4686c972015-02-20 15:44:52 -0800844 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700845 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
846 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800847 ycbcr->chroma_step = 2;
848 break;
849
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800850 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700851 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
852 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800853 ycbcr->chroma_step = 3;
854 break;
855
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400856 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
857 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
858 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530859 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400860 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700861 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700862 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700863 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800864 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400865 break;
866
867 //Planar
868 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700869 ystride = width;
870 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400871 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700872 ycbcr->cr = (void*)(hnd->base + ystride * height);
873 ycbcr->cb = (void*)(hnd->base + ystride * height +
874 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400875 ycbcr->ystride = ystride;
876 ycbcr->cstride = cstride;
877 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400878 break;
Rahul Sharma61173ea2017-01-05 09:15:40 +0530879 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
880 ystride = width * 2;
881 cstride = 0;
882 ycbcr->y = (void*)hnd->base;
883 ycbcr->cr = NULL;
884 ycbcr->cb = NULL;
885 ycbcr->ystride = ystride;
886 ycbcr->cstride = 0;
887 ycbcr->chroma_step = 0;
888 break;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400889 //Unsupported formats
890 case HAL_PIXEL_FORMAT_YCbCr_422_I:
891 case HAL_PIXEL_FORMAT_YCrCb_422_I:
892 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
893 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700894 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400895 err = -EINVAL;
896 }
897 return err;
898
899}
900
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700901
902
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700903// Allocate buffer from width, height and format into a
904// private_handle_t. It is the responsibility of the caller
905// to free the buffer using the free_buffer function
906int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
907{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700908 alloc_data data;
909 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700910 gralloc::IAllocController* sAlloc =
911 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700912 data.base = 0;
913 data.fd = -1;
914 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700915 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
916 alignedh);
917
Naseer Ahmed29a26812012-06-14 00:56:20 -0700918 data.align = getpagesize();
919 data.uncached = useUncached(usage);
920 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700921
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700922 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700923 if (0 != err) {
924 ALOGE("%s: allocate failed", __FUNCTION__);
925 return -ENOMEM;
926 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700927
radhakrishnad7131e62015-10-13 12:32:30 +0530928 if(isUBwcEnabled(format, usage)) {
929 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
930 }
931
Naseer Ahmed29a26812012-06-14 00:56:20 -0700932 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700933 data.allocType, 0, format,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700934 alignedw, alignedh, -1, 0, 0, w, h);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700935 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700936 hnd->offset = data.offset;
937 hnd->gpuaddr = 0;
938 *pHnd = hnd;
939 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700940}
941
942void free_buffer(private_handle_t *hnd)
943{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700944 gralloc::IAllocController* sAlloc =
945 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700946 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700947 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700948 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
949 }
950 if(hnd)
951 delete hnd;
952
953}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800954
955// UBWC helper functions
956static bool isUBwcFormat(int format)
957{
958 // Explicitly defined UBWC formats
959 switch(format)
960 {
961 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800962 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800963 return true;
964 default:
965 return false;
966 }
967}
968
969static bool isUBwcSupported(int format)
970{
Sushil Chauhan01361412016-04-25 16:36:18 -0700971 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
972 // Existing HAL formats with UBWC support
973 switch(format)
974 {
975 case HAL_PIXEL_FORMAT_BGR_565:
976 case HAL_PIXEL_FORMAT_RGBA_8888:
977 case HAL_PIXEL_FORMAT_RGBX_8888:
978 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
979 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
980 case HAL_PIXEL_FORMAT_RGBA_1010102:
981 case HAL_PIXEL_FORMAT_RGBX_1010102:
982 return true;
983 default:
984 break;
985 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800986 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700987 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800988}
989
990bool isUBwcEnabled(int format, int usage)
991{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800992 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
993 if (isUBwcFormat(format))
994 return true;
995
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530996 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
997 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
998 return false;
999 }
1000
Sushil Chauhan7807d192015-08-13 10:10:48 -07001001 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
1002 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
1003 // usage flag and MDP supports the format.
1004 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
1005 bool enable = true;
1006 // Query GPU for UBWC only if buffer is intended to be used by GPU.
1007 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
1008 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
1009 }
Sushil Chauhan81594f62015-01-26 16:00:51 -08001010 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -07001011 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
1012 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -08001013 return true;
1014 }
1015 }
1016 return false;
1017}
1018
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001019static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -08001020 int& aligned_w, int& aligned_h)
1021{
1022 switch (format)
1023 {
1024 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1025 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1026 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1027 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
1028 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
1029 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001030 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Arun Kumar K.Rfc2a27f2016-09-07 18:59:46 -07001031 // The macro returns the stride which is 4/3 times the width, hence * 3/4
1032 aligned_w = (VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width) * 3) / 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001033 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
1034 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001035 default:
1036 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1037 aligned_w = 0;
1038 aligned_h = 0;
1039 break;
1040 }
1041}
1042
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001043static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001044{
1045 block_width = 0;
1046 block_height = 0;
1047
1048 switch(bpp)
1049 {
1050 case 2:
1051 case 4:
1052 block_width = 16;
1053 block_height = 4;
1054 break;
1055 case 8:
1056 block_width = 8;
1057 block_height = 4;
1058 break;
1059 case 16:
1060 block_width = 4;
1061 block_height = 4;
1062 break;
1063 default:
1064 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1065 break;
1066 }
1067}
1068
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001069static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001070{
1071 unsigned int size = 0;
1072 int meta_width, meta_height;
1073 int block_width, block_height;
1074
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001075 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001076
1077 if (!block_width || !block_height) {
1078 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1079 return size;
1080 }
1081
1082 // Align meta buffer height to 16 blocks
1083 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1084
1085 // Align meta buffer width to 64 blocks
1086 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1087
1088 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001089 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001090 return size;
1091}
1092
1093static unsigned int getUBwcSize(int width, int height, int format,
1094 const int alignedw, const int alignedh) {
1095
1096 unsigned int size = 0;
1097 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001098 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001099 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001100 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001101 break;
1102 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001103 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001104 case HAL_PIXEL_FORMAT_RGBA_1010102:
1105 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001106 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001107 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001108 break;
1109 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1110 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1111 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1112 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1113 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001114 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1115 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1116 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001117 default:
1118 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1119 break;
1120 }
1121 return size;
1122}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001123
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001124int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001125{
1126 int err = 0;
1127
1128 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001129 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001130 return -EINVAL;
1131 }
1132
1133 // linear buffer
1134 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001135 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001136 return err;
1137 }
1138
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001139 // Ubwc buffers
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001140 unsigned int meta_size = 0;
1141 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001142 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001143 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001144 break;
1145 case HAL_PIXEL_FORMAT_RGBA_8888:
1146 case HAL_PIXEL_FORMAT_RGBX_8888:
Prabhanjan Kandulaebc8ce32016-10-18 12:25:54 -07001147 case HAL_PIXEL_FORMAT_RGBA_1010102:
1148 case HAL_PIXEL_FORMAT_RGBX_1010102:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001149 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001150 break;
1151 default:
1152 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1153 err = -EINVAL;
1154 break;
1155 }
1156
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001157 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001158 return err;
1159}