blob: 147b9cbb06ce8bdd6a01ab2a784003da187cb04b [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>
42
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
Shalaj Jain3c490412015-04-22 16:52:03 -070062#ifdef MASTER_SIDE_CP
63#define CP_HEAP_ID ION_SECURE_HEAP_ID
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070064#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
Shalaj Jain3c490412015-04-22 16:52:03 -070065#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
Arun Kumar K.R7f0b24b2015-07-05 21:20:57 -070066#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
Shalaj Jain3c490412015-04-22 16:52:03 -070067#else // SLAVE_SIDE_CP
68#define CP_HEAP_ID ION_CP_MM_HEAP_ID
69#define SD_HEAP_ID CP_HEAP_ID
70#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
71#define ION_SD_FLAGS ION_SECURE
72#endif
73
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074using namespace gralloc;
Naseer Ahmeda87da602012-07-01 23:54:19 -070075using namespace qdutils;
Ramkumar Radhakrishnan29a36a52015-06-16 20:22:42 -070076using namespace android;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070077
Naomi Luisa44100c2013-02-08 12:42:03 -080078ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo);
Ramakant Singhc85ccee2016-04-01 15:25:17 +053079ANDROID_SINGLETON_STATIC_INSTANCE(MDPCapabilityInfo);
Naomi Luisa44100c2013-02-08 12:42:03 -080080
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -080081static void getYuvUBwcWidthHeight(int, int, int, int&, int&);
Sushil Chauhan65e26302015-01-14 10:48:57 -080082static unsigned int getUBwcSize(int, int, int, const int, const int);
83
Iliyan Malchev202a77d2012-06-11 14:41:12 -070084//Common functions
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085
Saurabh Shah1adcafe2014-12-19 10:05:41 -080086/* The default policy is to return cached buffers unless the client explicity
87 * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
88 * read or written in software. Any combination with a _RARELY_ flag will be
89 * treated as uncached. */
90static bool useUncached(const int& usage) {
91 if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
92 ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
93 GRALLOC_USAGE_SW_WRITE_RARELY) or
94 ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
95 GRALLOC_USAGE_SW_READ_RARELY))
96 return true;
97
98 return false;
99}
100
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530101//------------- MDPCapabilityInfo-----------------------//
102MDPCapabilityInfo :: MDPCapabilityInfo() {
103 isMacroTileSupported = false;
104 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupported);
105}
106
107int MDPCapabilityInfo :: isMacroTilingSupportedByMDP(){
108 return isMacroTileSupported;
109 }
110
111//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800112AdrenoMemInfo::AdrenoMemInfo()
113{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800114 LINK_adreno_compute_aligned_width_and_height = NULL;
115 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700116 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800117 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800118 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700119 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800120
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800121 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
122 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800123 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700124 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
125 *(void **)&LINK_adreno_compute_padding =
126 ::dlsym(libadreno_utils, "compute_surface_padding");
127 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
128 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800129 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
130 ::dlsym(libadreno_utils,
131 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800132 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
133 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700134 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
135 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800136 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700137
138 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
139 // that disables UBWC allocations for the graphics stack is set
140 gfx_ubwc_disable = 0;
141 char property[PROPERTY_VALUE_MAX];
142 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
143 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
144 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
145 gfx_ubwc_disable = 1;
146 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800147}
148
149AdrenoMemInfo::~AdrenoMemInfo()
150{
151 if (libadreno_utils) {
152 ::dlclose(libadreno_utils);
153 }
154}
155
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700156int AdrenoMemInfo::isMacroTilingSupportedByGPU()
157{
158 if ((libadreno_utils)) {
159 if(LINK_adreno_isMacroTilingSupportedByGpu) {
160 return LINK_adreno_isMacroTilingSupportedByGpu();
161 }
162 }
163 return 0;
164}
165
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700166void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
167 int& aligned_h) {
168 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
169 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
170 int w = metadata->bufferDim.sliceWidth;
171 int h = metadata->bufferDim.sliceHeight;
172 int f = hnd->format;
173 int usage = 0;
174
175 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
176 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
177 }
178
179 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
180 } else {
181 aligned_w = hnd->width;
182 aligned_h = hnd->height;
183 }
184
185}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700186
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700187bool isUncompressedRgbFormat(int format)
188{
189 bool is_rgb_format = false;
190
191 switch (format)
192 {
193 case HAL_PIXEL_FORMAT_RGBA_8888:
194 case HAL_PIXEL_FORMAT_RGBX_8888:
195 case HAL_PIXEL_FORMAT_RGB_888:
196 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600197 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700198 case HAL_PIXEL_FORMAT_BGRA_8888:
199 case HAL_PIXEL_FORMAT_RGBA_5551:
200 case HAL_PIXEL_FORMAT_RGBA_4444:
201 case HAL_PIXEL_FORMAT_R_8:
202 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800203 case HAL_PIXEL_FORMAT_BGRX_8888:
204 case HAL_PIXEL_FORMAT_RGBA_1010102:
205 case HAL_PIXEL_FORMAT_ARGB_2101010:
206 case HAL_PIXEL_FORMAT_RGBX_1010102:
207 case HAL_PIXEL_FORMAT_XRGB_2101010:
208 case HAL_PIXEL_FORMAT_BGRA_1010102:
209 case HAL_PIXEL_FORMAT_ABGR_2101010:
210 case HAL_PIXEL_FORMAT_BGRX_1010102:
211 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700212 is_rgb_format = true;
213 break;
214 default:
215 break;
216 }
217
218 return is_rgb_format;
219}
220
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800221void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800222 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800223{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700224 bool ubwc_enabled = isUBwcEnabled(format, usage);
225
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800226 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700227 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700228 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700229 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800230 } else if (ubwc_enabled) {
231 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
232 } else {
233 aligned_w = width;
234 aligned_h = height;
235 int alignment = 32;
236 switch (format)
237 {
238 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
239 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
240 if (LINK_adreno_get_gpu_pixel_alignment) {
241 alignment = LINK_adreno_get_gpu_pixel_alignment();
242 }
243 aligned_w = ALIGN(width, alignment);
244 break;
245 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
246 aligned_w = ALIGN(width, alignment);
247 break;
248 case HAL_PIXEL_FORMAT_RAW16:
249 aligned_w = ALIGN(width, 16);
250 break;
251 case HAL_PIXEL_FORMAT_RAW10:
252 aligned_w = ALIGN(width * 10 / 8, 8);
253 break;
254 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
255 aligned_w = ALIGN(width, 128);
256 break;
257 case HAL_PIXEL_FORMAT_YV12:
258 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
259 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
260 case HAL_PIXEL_FORMAT_YCbCr_422_I:
261 case HAL_PIXEL_FORMAT_YCrCb_422_I:
262 aligned_w = ALIGN(width, 16);
263 break;
264 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
265 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
266 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
267 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
268 break;
269 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
270 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
271 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
272 break;
273 case HAL_PIXEL_FORMAT_BLOB:
274 break;
275 case HAL_PIXEL_FORMAT_NV21_ZSL:
276 aligned_w = ALIGN(width, 64);
277 aligned_h = ALIGN(height, 64);
278 break;
279 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
280 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
281 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
282 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
283 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
284 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
285 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
286 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
287 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
288 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
303 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
304 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
305 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
306 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
307 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
308 int bytesPerPixel = 0;
309 int raster_mode = 0; //Adreno unknown raster mode.
310 int padding_threshold = 512; //Threshold for padding
311 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800312
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800313 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
314 width, height, format, 0,raster_mode, padding_threshold,
315 &aligned_w, &aligned_h, &bytesPerPixel);
316 } else {
317 ALOGW("%s: Warning!! Symbols" \
318 " compute_compressedfmt_aligned_width_and_height" \
319 " not found", __FUNCTION__);
320 }
321 break;
322 default: break;
323 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800324 }
325}
326
327void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
328 int tile_enabled, int& aligned_w, int& aligned_h)
329{
330 aligned_w = ALIGN(width, 32);
331 aligned_h = ALIGN(height, 32);
332
333 // Don't add any additional padding if debug.gralloc.map_fb_memory
334 // is enabled
335 char property[PROPERTY_VALUE_MAX];
336 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
337 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
338 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
339 return;
340 }
341
342 int bpp = 4;
343 switch(format)
344 {
345 case HAL_PIXEL_FORMAT_RGB_888:
346 bpp = 3;
347 break;
348 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600349 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800350 case HAL_PIXEL_FORMAT_RGBA_5551:
351 case HAL_PIXEL_FORMAT_RGBA_4444:
352 bpp = 2;
353 break;
354 default: break;
355 }
356
357 if (libadreno_utils) {
358 int raster_mode = 0; // Adreno unknown raster mode.
359 int padding_threshold = 512; // Threshold for padding surfaces.
360 // the function below computes aligned width and aligned height
361 // based on linear or macro tile mode selected.
362 if(LINK_adreno_compute_aligned_width_and_height) {
363 LINK_adreno_compute_aligned_width_and_height(width,
364 height, bpp, tile_enabled,
365 raster_mode, padding_threshold,
366 &aligned_w, &aligned_h);
367
368 } else if(LINK_adreno_compute_padding) {
369 int surface_tile_height = 1; // Linear surface
370 aligned_w = LINK_adreno_compute_padding(width, bpp,
371 surface_tile_height, raster_mode,
372 padding_threshold);
373 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
374 __FUNCTION__);
375 } else {
376 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
377 "compute_aligned_width_and_height not found", __FUNCTION__);
378 }
379 }
380}
381
382int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
383{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700384 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800385 if (LINK_adreno_isUBWCSupportedByGpu) {
386 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
387 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
388 }
389 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800390 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800391}
392
Sushil Chauhan082acd62015-01-14 16:49:29 -0800393ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
394{
395 switch (hal_format) {
396 case HAL_PIXEL_FORMAT_RGBA_8888:
397 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700398 case HAL_PIXEL_FORMAT_RGBX_8888:
399 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800400 case HAL_PIXEL_FORMAT_RGB_565:
401 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600402 case HAL_PIXEL_FORMAT_BGR_565:
403 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800404 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800405 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800406 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
407 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800408 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800409 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
410 return ADRENO_PIXELFORMAT_TP10;
411 case HAL_PIXEL_FORMAT_RGBA_1010102:
412 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
413 case HAL_PIXEL_FORMAT_RGBX_1010102:
414 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
415 case HAL_PIXEL_FORMAT_ABGR_2101010:
416 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800417 default:
418 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
419 break;
420 }
421 return ADRENO_PIXELFORMAT_UNKNOWN;
422}
423
Naomi Luisa44100c2013-02-08 12:42:03 -0800424//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700425IAllocController* IAllocController::sController = NULL;
426IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700427{
428 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700429 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700430 }
431 return sController;
432}
433
434
435//-------------- IonController-----------------------//
436IonController::IonController()
437{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530438 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530439
440 char property[PROPERTY_VALUE_MAX];
441 property_get("video.disable.ubwc", property, "0");
442 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530443}
444
445void IonController::allocateIonMem()
446{
447 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700448}
449
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700450int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700451{
452 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500453 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700454 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700455
456 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700457 data.allocType = 0;
458
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530459 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530460 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800461 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700462 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800463 /*
464 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700465 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800466 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700467 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800468 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700469 ionHeapId = ION_HEAP(CP_HEAP_ID);
470 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800471 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530472 } else {
473 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500474 // do not set ion secure flag & MM heap. Fallback to system heap.
475 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530476 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500477 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530478 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
479 //MM Heap is exclusively a secure heap.
480 //If it is used for non secure cases, fallback to IOMMU heap
481 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
482 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500483 trying to use system heap instead !!");
484 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500485 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700486
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700487 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500488 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700489
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700490 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500491 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700492
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530493 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500494 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500496 // if no ion heap flags are set, default to system heap
497 if(!ionHeapId)
498 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500500 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500502 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700503 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700504
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700505 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700506 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500507 } else {
508 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
509 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700510 }
511
512 return ret;
513}
514
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700515IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700516{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700517 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700518 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
519 memalloc = mIonAlloc;
520 } else {
521 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
522 }
523
524 return memalloc;
525}
526
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700527bool isMacroTileEnabled(int format, int usage)
528{
529 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700530 // Check whether GPU & MDSS supports MacroTiling feature
531 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530532 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700533 {
534 // check the format
535 switch(format)
536 {
537 case HAL_PIXEL_FORMAT_RGBA_8888:
538 case HAL_PIXEL_FORMAT_RGBX_8888:
539 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800540 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600541 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700542 {
543 tileEnabled = true;
544 // check the usage flags
545 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
546 GRALLOC_USAGE_SW_WRITE_MASK)) {
547 // Application intends to use CPU for rendering
548 tileEnabled = false;
549 }
550 break;
551 }
552 default:
553 break;
554 }
555 }
556 return tileEnabled;
557}
558
559// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800560unsigned int getSize(int format, int width, int height, int usage,
561 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700562
Sushil Chauhan65e26302015-01-14 10:48:57 -0800563 if (isUBwcEnabled(format, usage)) {
564 return getUBwcSize(width, height, format, alignedw, alignedh);
565 }
566
567 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700568 switch (format) {
569 case HAL_PIXEL_FORMAT_RGBA_8888:
570 case HAL_PIXEL_FORMAT_RGBX_8888:
571 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800572 case HAL_PIXEL_FORMAT_RGBA_1010102:
573 case HAL_PIXEL_FORMAT_ARGB_2101010:
574 case HAL_PIXEL_FORMAT_RGBX_1010102:
575 case HAL_PIXEL_FORMAT_XRGB_2101010:
576 case HAL_PIXEL_FORMAT_BGRA_1010102:
577 case HAL_PIXEL_FORMAT_ABGR_2101010:
578 case HAL_PIXEL_FORMAT_BGRX_1010102:
579 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700580 size = alignedw * alignedh * 4;
581 break;
582 case HAL_PIXEL_FORMAT_RGB_888:
583 size = alignedw * alignedh * 3;
584 break;
585 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600586 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700587 case HAL_PIXEL_FORMAT_RGBA_5551:
588 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700589 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700590 size = alignedw * alignedh * 2;
591 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700592 case HAL_PIXEL_FORMAT_RAW10:
593 size = ALIGN(alignedw * alignedh, 4096);
594 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700595
596 // adreno formats
597 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
598 size = ALIGN(alignedw*alignedh, 4096);
599 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
600 break;
601 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
602 // The chroma plane is subsampled,
603 // but the pitch in bytes is unchanged
604 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700605 size = ALIGN( alignedw * alignedh, 8192);
606 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
607 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700608 case HAL_PIXEL_FORMAT_YV12:
609 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
610 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800611 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700612 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400613 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700614 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700615 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700616 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800617 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
618 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400619 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800620 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700621 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
622 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700623 case HAL_PIXEL_FORMAT_YCbCr_422_I:
624 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700625 if(width & 1) {
626 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800627 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 size = ALIGN(alignedw * alignedh * 2, 4096);
630 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700631 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400632 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700633 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700634 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530635 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
636 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
637 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400638 case HAL_PIXEL_FORMAT_BLOB:
639 if(height != 1) {
640 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
641 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800642 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400643 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400644 size = width;
645 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700646 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700647 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
648 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500649 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
650 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
651 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
652 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
653 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
654 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
655 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
656 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
657 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
658 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
659 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
660 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
661 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
662 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
663 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
664 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
665 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
666 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
667 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
668 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
669 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
670 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
672 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
673 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
674 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
675 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800676 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500677 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
678 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700679 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500680 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800681 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700682 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700683 return size;
684}
685
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700686unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700687 int& alignedw, int &alignedh)
688{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700689 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700690
691 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
692 height,
693 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800694 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700695 alignedw,
696 alignedh);
697
Sushil Chauhan65e26302015-01-14 10:48:57 -0800698 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700699
700 return size;
701}
702
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700703
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700704unsigned int getBufferSizeAndDimensions(int width, int height, int format,
705 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700706{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700707 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700708
709 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
710 height,
711 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800712 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700713 alignedw,
714 alignedh);
715
Sushil Chauhan65e26302015-01-14 10:48:57 -0800716 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700717
718 return size;
719}
720
721
722void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700723 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700724{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700725 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700726
727 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
728 height,
729 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800730 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700731 alignedw,
732 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800733 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700734}
735
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700736void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
737 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800738{
739 // UBWC buffer has these 4 planes in the following sequence:
740 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
741 unsigned int y_meta_stride, y_meta_height, y_meta_size;
742 unsigned int y_stride, y_height, y_size;
743 unsigned int c_meta_stride, c_meta_height, c_meta_size;
744 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800745
746 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
747 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
748 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
749
750 y_stride = VENUS_Y_STRIDE(color_format, width);
751 y_height = VENUS_Y_SCANLINES(color_format, height);
752 y_size = ALIGN((y_stride * y_height), alignment);
753
754 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
755 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
756 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
757
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700758 ycbcr->y = (void*)(base + y_meta_size);
759 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
760 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800761 c_meta_size + 1);
762 ycbcr->ystride = y_stride;
763 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
764}
765
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700766void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
767 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800768{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800769 unsigned int ystride, cstride;
770
771 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700772 ycbcr->y = (void*)base;
773 ycbcr->cb = (void*)(base + ystride * height);
774 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800775 ycbcr->ystride = ystride;
776 ycbcr->cstride = cstride;
777 ycbcr->chroma_step = 2 * bpp;
778}
779
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400780int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
781{
782 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700783 int width = hnd->width;
784 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700785 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700786
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700787 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700788
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400789 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700790 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
791
792 // Check if UBWC buffer has been rendered in linear format.
793 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
794 format = metadata->linearFormat;
795 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400796
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700797 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700798 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700799 int usage = 0;
800
801 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
802 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
803 }
804
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700805 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700806 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700807 }
808
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400809 // Get the chroma offsets from the handle width/height. We take advantage
810 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700811 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400812 //Semiplanar
813 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
814 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
815 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
816 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700817 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800818 break;
819
820 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700821 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400822 break;
823
Sushil Chauhan4686c972015-02-20 15:44:52 -0800824 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700825 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
826 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800827 ycbcr->chroma_step = 2;
828 break;
829
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800830 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700831 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
832 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800833 ycbcr->chroma_step = 3;
834 break;
835
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400836 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
837 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
838 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530839 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400840 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700841 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700842 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700843 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800844 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400845 break;
846
847 //Planar
848 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700849 ystride = width;
850 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400851 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700852 ycbcr->cr = (void*)(hnd->base + ystride * height);
853 ycbcr->cb = (void*)(hnd->base + ystride * height +
854 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400855 ycbcr->ystride = ystride;
856 ycbcr->cstride = cstride;
857 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400858 break;
859 //Unsupported formats
860 case HAL_PIXEL_FORMAT_YCbCr_422_I:
861 case HAL_PIXEL_FORMAT_YCrCb_422_I:
862 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
863 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700864 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400865 err = -EINVAL;
866 }
867 return err;
868
869}
870
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700871
872
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700873// Allocate buffer from width, height and format into a
874// private_handle_t. It is the responsibility of the caller
875// to free the buffer using the free_buffer function
876int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
877{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700878 alloc_data data;
879 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700880 gralloc::IAllocController* sAlloc =
881 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700882 data.base = 0;
883 data.fd = -1;
884 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700885 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
886 alignedh);
887
Naseer Ahmed29a26812012-06-14 00:56:20 -0700888 data.align = getpagesize();
889 data.uncached = useUncached(usage);
890 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700891
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700892 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700893 if (0 != err) {
894 ALOGE("%s: allocate failed", __FUNCTION__);
895 return -ENOMEM;
896 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700897
radhakrishnad7131e62015-10-13 12:32:30 +0530898 if(isUBwcEnabled(format, usage)) {
899 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
900 }
901
Naseer Ahmed29a26812012-06-14 00:56:20 -0700902 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700903 data.allocType, 0, format,
904 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700905 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700906 hnd->offset = data.offset;
907 hnd->gpuaddr = 0;
908 *pHnd = hnd;
909 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700910}
911
912void free_buffer(private_handle_t *hnd)
913{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700914 gralloc::IAllocController* sAlloc =
915 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700916 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700917 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700918 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
919 }
920 if(hnd)
921 delete hnd;
922
923}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800924
925// UBWC helper functions
926static bool isUBwcFormat(int format)
927{
928 // Explicitly defined UBWC formats
929 switch(format)
930 {
931 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800932 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800933 return true;
934 default:
935 return false;
936 }
937}
938
939static bool isUBwcSupported(int format)
940{
941 // Existing HAL formats with UBWC support
942 switch(format)
943 {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600944 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800945 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -0700946 case HAL_PIXEL_FORMAT_RGBX_8888:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800947 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
948 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800949 case HAL_PIXEL_FORMAT_RGBA_1010102:
950 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800951 return true;
952 default:
953 return false;
954 }
955}
956
957bool isUBwcEnabled(int format, int usage)
958{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800959 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
960 if (isUBwcFormat(format))
961 return true;
962
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530963 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
964 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
965 return false;
966 }
967
Sushil Chauhan7807d192015-08-13 10:10:48 -0700968 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
969 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
970 // usage flag and MDP supports the format.
971 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
972 bool enable = true;
973 // Query GPU for UBWC only if buffer is intended to be used by GPU.
974 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
975 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
976 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800977 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700978 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
979 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800980 return true;
981 }
982 }
983 return false;
984}
985
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800986static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800987 int& aligned_w, int& aligned_h)
988{
989 switch (format)
990 {
991 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
992 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
993 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
994 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
995 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
996 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800997 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
998 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width);
999 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
1000 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001001 default:
1002 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1003 aligned_w = 0;
1004 aligned_h = 0;
1005 break;
1006 }
1007}
1008
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001009static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001010{
1011 block_width = 0;
1012 block_height = 0;
1013
1014 switch(bpp)
1015 {
1016 case 2:
1017 case 4:
1018 block_width = 16;
1019 block_height = 4;
1020 break;
1021 case 8:
1022 block_width = 8;
1023 block_height = 4;
1024 break;
1025 case 16:
1026 block_width = 4;
1027 block_height = 4;
1028 break;
1029 default:
1030 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1031 break;
1032 }
1033}
1034
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001035static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001036{
1037 unsigned int size = 0;
1038 int meta_width, meta_height;
1039 int block_width, block_height;
1040
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001041 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001042
1043 if (!block_width || !block_height) {
1044 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1045 return size;
1046 }
1047
1048 // Align meta buffer height to 16 blocks
1049 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1050
1051 // Align meta buffer width to 64 blocks
1052 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1053
1054 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001055 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001056 return size;
1057}
1058
1059static unsigned int getUBwcSize(int width, int height, int format,
1060 const int alignedw, const int alignedh) {
1061
1062 unsigned int size = 0;
1063 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001064 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001065 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001066 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001067 break;
1068 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001069 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001070 case HAL_PIXEL_FORMAT_RGBA_1010102:
1071 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001072 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001073 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001074 break;
1075 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1076 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1077 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1078 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1079 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001080 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1081 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1082 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001083 default:
1084 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1085 break;
1086 }
1087 return size;
1088}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001089
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001090int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001091{
1092 int err = 0;
1093
1094 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001095 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001096 return -EINVAL;
1097 }
1098
1099 // linear buffer
1100 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001101 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001102 return err;
1103 }
1104
1105 unsigned int meta_size = 0;
1106 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001107 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001108 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001109 break;
1110 case HAL_PIXEL_FORMAT_RGBA_8888:
1111 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001112 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001113 break;
1114 default:
1115 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1116 err = -EINVAL;
1117 break;
1118 }
1119
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001120 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001121 return err;
1122}