blob: 581d1126a6f33b4c40e6d5eb2e0c6ea250857ec4 [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() {
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530103 qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupported);
Sushil Chauhan01361412016-04-25 16:36:18 -0700104 qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530105}
106
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530107//------------- AdrenoMemInfo-----------------------//
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800108AdrenoMemInfo::AdrenoMemInfo()
109{
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800110 LINK_adreno_compute_aligned_width_and_height = NULL;
111 LINK_adreno_compute_padding = NULL;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700112 LINK_adreno_isMacroTilingSupportedByGpu = NULL;
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800113 LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800114 LINK_adreno_isUBWCSupportedByGpu = NULL;
Sushil Chauhan521ce352015-08-28 11:33:30 -0700115 LINK_adreno_get_gpu_pixel_alignment = NULL;
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800116
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800117 libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW);
118 if (libadreno_utils) {
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800119 *(void **)&LINK_adreno_compute_aligned_width_and_height =
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700120 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
121 *(void **)&LINK_adreno_compute_padding =
122 ::dlsym(libadreno_utils, "compute_surface_padding");
123 *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
124 ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
Jeykumar Sankaran2ba20512014-02-27 15:21:42 -0800125 *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
126 ::dlsym(libadreno_utils,
127 "compute_compressedfmt_aligned_width_and_height");
Sushil Chauhan082acd62015-01-14 16:49:29 -0800128 *(void **)&LINK_adreno_isUBWCSupportedByGpu =
129 ::dlsym(libadreno_utils, "isUBWCSupportedByGpu");
Sushil Chauhan521ce352015-08-28 11:33:30 -0700130 *(void **)&LINK_adreno_get_gpu_pixel_alignment =
131 ::dlsym(libadreno_utils, "get_gpu_pixel_alignment");
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800132 }
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700133
134 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
135 // that disables UBWC allocations for the graphics stack is set
136 gfx_ubwc_disable = 0;
137 char property[PROPERTY_VALUE_MAX];
138 property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
139 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
140 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
141 gfx_ubwc_disable = 1;
142 }
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800143}
144
145AdrenoMemInfo::~AdrenoMemInfo()
146{
147 if (libadreno_utils) {
148 ::dlclose(libadreno_utils);
149 }
150}
151
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700152int AdrenoMemInfo::isMacroTilingSupportedByGPU()
153{
154 if ((libadreno_utils)) {
155 if(LINK_adreno_isMacroTilingSupportedByGpu) {
156 return LINK_adreno_isMacroTilingSupportedByGpu();
157 }
158 }
159 return 0;
160}
161
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700162void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
163 int& aligned_h) {
164 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
165 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
166 int w = metadata->bufferDim.sliceWidth;
167 int h = metadata->bufferDim.sliceHeight;
168 int f = hnd->format;
169 int usage = 0;
170
171 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
172 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
173 }
174
175 getAlignedWidthAndHeight(w, h, f, usage, aligned_w, aligned_h);
176 } else {
177 aligned_w = hnd->width;
178 aligned_h = hnd->height;
179 }
180
181}
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700182
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700183bool isUncompressedRgbFormat(int format)
184{
185 bool is_rgb_format = false;
186
187 switch (format)
188 {
189 case HAL_PIXEL_FORMAT_RGBA_8888:
190 case HAL_PIXEL_FORMAT_RGBX_8888:
191 case HAL_PIXEL_FORMAT_RGB_888:
192 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600193 case HAL_PIXEL_FORMAT_BGR_565:
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700194 case HAL_PIXEL_FORMAT_BGRA_8888:
195 case HAL_PIXEL_FORMAT_RGBA_5551:
196 case HAL_PIXEL_FORMAT_RGBA_4444:
197 case HAL_PIXEL_FORMAT_R_8:
198 case HAL_PIXEL_FORMAT_RG_88:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800199 case HAL_PIXEL_FORMAT_BGRX_8888:
200 case HAL_PIXEL_FORMAT_RGBA_1010102:
201 case HAL_PIXEL_FORMAT_ARGB_2101010:
202 case HAL_PIXEL_FORMAT_RGBX_1010102:
203 case HAL_PIXEL_FORMAT_XRGB_2101010:
204 case HAL_PIXEL_FORMAT_BGRA_1010102:
205 case HAL_PIXEL_FORMAT_ABGR_2101010:
206 case HAL_PIXEL_FORMAT_BGRX_1010102:
207 case HAL_PIXEL_FORMAT_XBGR_2101010: // Intentional fallthrough
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700208 is_rgb_format = true;
209 break;
210 default:
211 break;
212 }
213
214 return is_rgb_format;
215}
216
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800217void AdrenoMemInfo::getAlignedWidthAndHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800218 int usage, int& aligned_w, int& aligned_h)
Naomi Luisa44100c2013-02-08 12:42:03 -0800219{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700220 bool ubwc_enabled = isUBwcEnabled(format, usage);
221
Naomi Luis01f5c8e2013-02-11 12:46:24 -0800222 // Currently surface padding is only computed for RGB* surfaces.
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700223 if (isUncompressedRgbFormat(format) == true) {
Sushil Chauhane61fac52015-04-30 17:14:37 -0700224 int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700225 getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800226 } else if (ubwc_enabled) {
227 getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
228 } else {
229 aligned_w = width;
230 aligned_h = height;
231 int alignment = 32;
232 switch (format)
233 {
234 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
235 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
236 if (LINK_adreno_get_gpu_pixel_alignment) {
237 alignment = LINK_adreno_get_gpu_pixel_alignment();
238 }
239 aligned_w = ALIGN(width, alignment);
240 break;
241 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
242 aligned_w = ALIGN(width, alignment);
243 break;
244 case HAL_PIXEL_FORMAT_RAW16:
245 aligned_w = ALIGN(width, 16);
246 break;
247 case HAL_PIXEL_FORMAT_RAW10:
248 aligned_w = ALIGN(width * 10 / 8, 8);
249 break;
250 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
251 aligned_w = ALIGN(width, 128);
252 break;
253 case HAL_PIXEL_FORMAT_YV12:
254 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
255 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
256 case HAL_PIXEL_FORMAT_YCbCr_422_I:
257 case HAL_PIXEL_FORMAT_YCrCb_422_I:
258 aligned_w = ALIGN(width, 16);
259 break;
260 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
261 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
262 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
263 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
264 break;
265 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
266 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
267 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
268 break;
269 case HAL_PIXEL_FORMAT_BLOB:
270 break;
271 case HAL_PIXEL_FORMAT_NV21_ZSL:
272 aligned_w = ALIGN(width, 64);
273 aligned_h = ALIGN(height, 64);
274 break;
275 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
276 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
277 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
278 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
279 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
280 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
281 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
282 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
283 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
284 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
285 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
286 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
287 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
288 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
289 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
290 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
291 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
292 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
293 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
294 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
295 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
296 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
297 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
298 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
299 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
300 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
301 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
302 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
303 if(LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
304 int bytesPerPixel = 0;
305 int raster_mode = 0; //Adreno unknown raster mode.
306 int padding_threshold = 512; //Threshold for padding
307 //surfaces.
Sushil Chauhan65e26302015-01-14 10:48:57 -0800308
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800309 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
310 width, height, format, 0,raster_mode, padding_threshold,
311 &aligned_w, &aligned_h, &bytesPerPixel);
312 } else {
313 ALOGW("%s: Warning!! Symbols" \
314 " compute_compressedfmt_aligned_width_and_height" \
315 " not found", __FUNCTION__);
316 }
317 break;
318 default: break;
319 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800320 }
321}
322
323void AdrenoMemInfo::getGpuAlignedWidthHeight(int width, int height, int format,
324 int tile_enabled, int& aligned_w, int& aligned_h)
325{
326 aligned_w = ALIGN(width, 32);
327 aligned_h = ALIGN(height, 32);
328
329 // Don't add any additional padding if debug.gralloc.map_fb_memory
330 // is enabled
331 char property[PROPERTY_VALUE_MAX];
332 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
333 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
334 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
335 return;
336 }
337
338 int bpp = 4;
339 switch(format)
340 {
341 case HAL_PIXEL_FORMAT_RGB_888:
342 bpp = 3;
343 break;
344 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600345 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800346 case HAL_PIXEL_FORMAT_RGBA_5551:
347 case HAL_PIXEL_FORMAT_RGBA_4444:
348 bpp = 2;
349 break;
350 default: break;
351 }
352
353 if (libadreno_utils) {
354 int raster_mode = 0; // Adreno unknown raster mode.
355 int padding_threshold = 512; // Threshold for padding surfaces.
356 // the function below computes aligned width and aligned height
357 // based on linear or macro tile mode selected.
358 if(LINK_adreno_compute_aligned_width_and_height) {
359 LINK_adreno_compute_aligned_width_and_height(width,
360 height, bpp, tile_enabled,
361 raster_mode, padding_threshold,
362 &aligned_w, &aligned_h);
363
364 } else if(LINK_adreno_compute_padding) {
365 int surface_tile_height = 1; // Linear surface
366 aligned_w = LINK_adreno_compute_padding(width, bpp,
367 surface_tile_height, raster_mode,
368 padding_threshold);
369 ALOGW("%s: Warning!! Old GFX API is used to calculate stride",
370 __FUNCTION__);
371 } else {
372 ALOGW("%s: Warning!! Symbols compute_surface_padding and " \
373 "compute_aligned_width_and_height not found", __FUNCTION__);
374 }
375 }
376}
377
378int AdrenoMemInfo::isUBWCSupportedByGPU(int format)
379{
Mohan Maiyacbeab9e2015-04-20 09:20:44 -0700380 if (!gfx_ubwc_disable && libadreno_utils) {
Sushil Chauhan082acd62015-01-14 16:49:29 -0800381 if (LINK_adreno_isUBWCSupportedByGpu) {
382 ADRENOPIXELFORMAT gpu_format = getGpuPixelFormat(format);
383 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
384 }
385 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800386 return 0;
Naomi Luisa44100c2013-02-08 12:42:03 -0800387}
388
Sushil Chauhan082acd62015-01-14 16:49:29 -0800389ADRENOPIXELFORMAT AdrenoMemInfo::getGpuPixelFormat(int hal_format)
390{
391 switch (hal_format) {
392 case HAL_PIXEL_FORMAT_RGBA_8888:
393 return ADRENO_PIXELFORMAT_R8G8B8A8;
Sushil Chauhan6686c802015-04-15 11:30:39 -0700394 case HAL_PIXEL_FORMAT_RGBX_8888:
395 return ADRENO_PIXELFORMAT_R8G8B8X8;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800396 case HAL_PIXEL_FORMAT_RGB_565:
397 return ADRENO_PIXELFORMAT_B5G6R5;
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600398 case HAL_PIXEL_FORMAT_BGR_565:
399 return ADRENO_PIXELFORMAT_R5G6B5;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800400 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800401 return ADRENO_PIXELFORMAT_NV12;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800402 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
403 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Sushil Chauhana9d47002015-02-18 14:55:03 -0800404 return ADRENO_PIXELFORMAT_NV12_EXT;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800405 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
406 return ADRENO_PIXELFORMAT_TP10;
407 case HAL_PIXEL_FORMAT_RGBA_1010102:
408 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
409 case HAL_PIXEL_FORMAT_RGBX_1010102:
410 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
411 case HAL_PIXEL_FORMAT_ABGR_2101010:
412 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
Sushil Chauhan082acd62015-01-14 16:49:29 -0800413 default:
414 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
415 break;
416 }
417 return ADRENO_PIXELFORMAT_UNKNOWN;
418}
419
Naomi Luisa44100c2013-02-08 12:42:03 -0800420//-------------- IAllocController-----------------------//
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700421IAllocController* IAllocController::sController = NULL;
422IAllocController* IAllocController::getInstance(void)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700423{
424 if(sController == NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700425 sController = new IonController();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700426 }
427 return sController;
428}
429
430
431//-------------- IonController-----------------------//
432IonController::IonController()
433{
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530434 allocateIonMem();
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530435
436 char property[PROPERTY_VALUE_MAX];
437 property_get("video.disable.ubwc", property, "0");
438 mDisableUBWCForEncode = atoi(property);
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +0530439}
440
441void IonController::allocateIonMem()
442{
443 mIonAlloc = new IonAlloc();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700444}
445
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700446int IonController::allocate(alloc_data& data, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700447{
448 int ionFlags = 0;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500449 int ionHeapId = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700450 int ret;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700451
452 data.uncached = useUncached(usage);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700453 data.allocType = 0;
454
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530455 if(usage & GRALLOC_USAGE_PROTECTED) {
Prabhanjan Kandulae8f4bec2013-10-24 16:32:51 +0530456 if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800457 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
Shalaj Jain3c490412015-04-22 16:52:03 -0700458 ionHeapId = ION_HEAP(SD_HEAP_ID);
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800459 /*
460 * There is currently no flag in ION for Secure Display
Shalaj Jain3c490412015-04-22 16:52:03 -0700461 * VM. Please add it to the define once available.
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800462 */
Shalaj Jain3c490412015-04-22 16:52:03 -0700463 ionFlags |= ION_SD_FLAGS;
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800464 } else {
Shalaj Jain3c490412015-04-22 16:52:03 -0700465 ionHeapId = ION_HEAP(CP_HEAP_ID);
466 ionFlags |= ION_CP_FLAGS;
Shalaj Jain13cdf812014-12-02 16:20:54 -0800467 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530468 } else {
469 // for targets/OEMs which do not need HW level protection
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500470 // do not set ion secure flag & MM heap. Fallback to system heap.
471 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Justin Philipd6166602014-08-12 13:42:21 +0530472 data.allocType |= private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER;
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500473 }
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530474 } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
475 //MM Heap is exclusively a secure heap.
476 //If it is used for non secure cases, fallback to IOMMU heap
477 ALOGW("GRALLOC_USAGE_PRIVATE_MM_HEAP \
478 cannot be used as an insecure heap!\
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500479 trying to use system heap instead !!");
480 ionHeapId |= ION_HEAP(ION_SYSTEM_HEAP_ID);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500481 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700482
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700483 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500484 ionHeapId |= ION_HEAP(ION_CAMERA_HEAP_ID);
Arun Kumar K.Rff78b892013-05-24 12:37:51 -0700485
Arun Kumar K.R0daaa992013-03-12 15:08:29 -0700486 if(usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP)
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500487 ionHeapId |= ION_HEAP(ION_ADSP_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700488
Prabhanjan Kandula92896b82013-05-07 19:58:24 +0530489 if(ionFlags & ION_SECURE)
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500490 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700491
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500492 // if no ion heap flags are set, default to system heap
493 if(!ionHeapId)
494 ionHeapId = ION_HEAP(ION_SYSTEM_HEAP_ID);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500496 //At this point we should have the right heap set, there is no fallback
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700497 data.flags = ionFlags;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500498 data.heapId = ionHeapId;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700499 ret = mIonAlloc->alloc_buffer(data);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700500
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700501 if(ret >= 0 ) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700502 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION;
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500503 } else {
504 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x",
505 __FUNCTION__, ionHeapId, ionFlags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700506 }
507
508 return ret;
509}
510
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700511IMemAlloc* IonController::getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700512{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700513 IMemAlloc* memalloc = NULL;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700514 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) {
515 memalloc = mIonAlloc;
516 } else {
517 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags);
518 }
519
520 return memalloc;
521}
522
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700523bool isMacroTileEnabled(int format, int usage)
524{
525 bool tileEnabled = false;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700526 // Check whether GPU & MDSS supports MacroTiling feature
527 if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
Ramakant Singhc85ccee2016-04-01 15:25:17 +0530528 MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700529 {
530 // check the format
531 switch(format)
532 {
533 case HAL_PIXEL_FORMAT_RGBA_8888:
534 case HAL_PIXEL_FORMAT_RGBX_8888:
535 case HAL_PIXEL_FORMAT_BGRA_8888:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800536 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600537 case HAL_PIXEL_FORMAT_BGR_565:
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700538 {
539 tileEnabled = true;
540 // check the usage flags
541 if (usage & (GRALLOC_USAGE_SW_READ_MASK |
542 GRALLOC_USAGE_SW_WRITE_MASK)) {
543 // Application intends to use CPU for rendering
544 tileEnabled = false;
545 }
546 break;
547 }
548 default:
549 break;
550 }
551 }
552 return tileEnabled;
553}
554
555// helper function
Sushil Chauhan65e26302015-01-14 10:48:57 -0800556unsigned int getSize(int format, int width, int height, int usage,
557 const int alignedw, const int alignedh) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700558
Sushil Chauhan65e26302015-01-14 10:48:57 -0800559 if (isUBwcEnabled(format, usage)) {
560 return getUBwcSize(width, height, format, alignedw, alignedh);
561 }
562
563 unsigned int size = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700564 switch (format) {
565 case HAL_PIXEL_FORMAT_RGBA_8888:
566 case HAL_PIXEL_FORMAT_RGBX_8888:
567 case HAL_PIXEL_FORMAT_BGRA_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800568 case HAL_PIXEL_FORMAT_RGBA_1010102:
569 case HAL_PIXEL_FORMAT_ARGB_2101010:
570 case HAL_PIXEL_FORMAT_RGBX_1010102:
571 case HAL_PIXEL_FORMAT_XRGB_2101010:
572 case HAL_PIXEL_FORMAT_BGRA_1010102:
573 case HAL_PIXEL_FORMAT_ABGR_2101010:
574 case HAL_PIXEL_FORMAT_BGRX_1010102:
575 case HAL_PIXEL_FORMAT_XBGR_2101010:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700576 size = alignedw * alignedh * 4;
577 break;
578 case HAL_PIXEL_FORMAT_RGB_888:
579 size = alignedw * alignedh * 3;
580 break;
581 case HAL_PIXEL_FORMAT_RGB_565:
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -0600582 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnan96439522014-10-09 13:37:52 -0700583 case HAL_PIXEL_FORMAT_RGBA_5551:
584 case HAL_PIXEL_FORMAT_RGBA_4444:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700585 case HAL_PIXEL_FORMAT_RAW16:
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700586 size = alignedw * alignedh * 2;
587 break;
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700588 case HAL_PIXEL_FORMAT_RAW10:
589 size = ALIGN(alignedw * alignedh, 4096);
590 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700591
592 // adreno formats
593 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
594 size = ALIGN(alignedw*alignedh, 4096);
595 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096);
596 break;
597 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
598 // The chroma plane is subsampled,
599 // but the pitch in bytes is unchanged
600 // The GPU needs 4K alignment, but the video decoder needs 8K
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700601 size = ALIGN( alignedw * alignedh, 8192);
602 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192);
603 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700604 case HAL_PIXEL_FORMAT_YV12:
605 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) {
606 ALOGE("w or h is odd for the YV12 format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800607 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700608 }
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400609 size = alignedw*alignedh +
Naseer Ahmed29a26812012-06-14 00:56:20 -0700610 (ALIGN(alignedw/2, 16) * (alignedh/2))*2;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700611 size = ALIGN(size, (unsigned int)4096);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700612 break;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800613 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
614 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Naseer Ahmed2c215292013-09-18 23:47:42 -0400615 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2 + 1, 4096);
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800616 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700617 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
618 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700619 case HAL_PIXEL_FORMAT_YCbCr_422_I:
620 case HAL_PIXEL_FORMAT_YCrCb_422_I:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700621 if(width & 1) {
622 ALOGE("width is odd for the YUV422_SP format");
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800623 return 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700624 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700625 size = ALIGN(alignedw * alignedh * 2, 4096);
626 break;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700627 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmedce0c9502013-08-15 13:07:24 -0400628 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
Sushil Chauhane8a01792012-11-01 16:25:45 -0700629 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700630 break;
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530631 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
632 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
633 break;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400634 case HAL_PIXEL_FORMAT_BLOB:
635 if(height != 1) {
636 ALOGE("%s: Buffers with format HAL_PIXEL_FORMAT_BLOB \
637 must have height==1 ", __FUNCTION__);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800638 return 0;
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400639 }
Naseer Ahmed7669dae2013-04-17 20:23:53 -0400640 size = width;
641 break;
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700642 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700643 size = ALIGN((alignedw*alignedh) + (alignedw* alignedh)/2, 4096);
644 break;
Naseer Ahmed63326f42013-12-18 02:45:48 -0500645 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
646 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
647 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
648 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
649 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
650 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
651 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
652 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
653 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
654 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
655 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
656 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
657 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
658 case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
659 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
660 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
661 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
662 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
663 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
664 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
665 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
666 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
667 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
668 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
669 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
670 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
671 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
Jeykumar Sankaran8f4585f2014-02-05 15:23:40 -0800672 case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
Naseer Ahmed63326f42013-12-18 02:45:48 -0500673 size = alignedw * alignedh * ASTC_BLOCK_SIZE;
674 break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700675 default:
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500676 ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
Saurabh Shahd0b0d8f2014-01-31 11:45:56 -0800677 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700678 }
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700679 return size;
680}
681
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700682unsigned int getBufferSizeAndDimensions(int width, int height, int format,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700683 int& alignedw, int &alignedh)
684{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700685 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700686
687 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
688 height,
689 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800690 0,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700691 alignedw,
692 alignedh);
693
Sushil Chauhan65e26302015-01-14 10:48:57 -0800694 size = getSize(format, width, height, 0 /* usage */, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700695
696 return size;
697}
698
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700699
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700700unsigned int getBufferSizeAndDimensions(int width, int height, int format,
701 int usage, int& alignedw, int &alignedh)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700702{
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700703 unsigned int size;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700704
705 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
706 height,
707 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800708 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700709 alignedw,
710 alignedh);
711
Sushil Chauhan65e26302015-01-14 10:48:57 -0800712 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700713
714 return size;
715}
716
717
718void getBufferAttributes(int width, int height, int format, int usage,
Sushil Chauhane61fac52015-04-30 17:14:37 -0700719 int& alignedw, int &alignedh, int& tiled, unsigned int& size)
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700720{
Sushil Chauhane61fac52015-04-30 17:14:37 -0700721 tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700722
723 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
724 height,
725 format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800726 usage,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700727 alignedw,
728 alignedh);
Sushil Chauhan65e26302015-01-14 10:48:57 -0800729 size = getSize(format, width, height, usage, alignedw, alignedh);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700730}
731
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700732void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
733 int color_format, struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800734{
735 // UBWC buffer has these 4 planes in the following sequence:
736 // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane
737 unsigned int y_meta_stride, y_meta_height, y_meta_size;
738 unsigned int y_stride, y_height, y_size;
739 unsigned int c_meta_stride, c_meta_height, c_meta_size;
740 unsigned int alignment = 4096;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800741
742 y_meta_stride = VENUS_Y_META_STRIDE(color_format, width);
743 y_meta_height = VENUS_Y_META_SCANLINES(color_format, height);
744 y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment);
745
746 y_stride = VENUS_Y_STRIDE(color_format, width);
747 y_height = VENUS_Y_SCANLINES(color_format, height);
748 y_size = ALIGN((y_stride * y_height), alignment);
749
750 c_meta_stride = VENUS_UV_META_STRIDE(color_format, width);
751 c_meta_height = VENUS_UV_META_SCANLINES(color_format, height);
752 c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment);
753
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700754 ycbcr->y = (void*)(base + y_meta_size);
755 ycbcr->cb = (void*)(base + y_meta_size + y_size + c_meta_size);
756 ycbcr->cr = (void*)(base + y_meta_size + y_size +
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800757 c_meta_size + 1);
758 ycbcr->ystride = y_stride;
759 ycbcr->cstride = VENUS_UV_STRIDE(color_format, width);
760}
761
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700762void getYuvSPPlaneInfo(uint64_t base, int width, int height, int bpp,
763 struct android_ycbcr* ycbcr)
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800764{
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800765 unsigned int ystride, cstride;
766
767 ystride = cstride = width * bpp;
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700768 ycbcr->y = (void*)base;
769 ycbcr->cb = (void*)(base + ystride * height);
770 ycbcr->cr = (void*)(base + ystride * height + 1);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800771 ycbcr->ystride = ystride;
772 ycbcr->cstride = cstride;
773 ycbcr->chroma_step = 2 * bpp;
774}
775
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400776int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
777{
778 int err = 0;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700779 int width = hnd->width;
780 int height = hnd->height;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700781 int format = hnd->format;
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700782
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700783 unsigned int ystride, cstride;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700784
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400785 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700786 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
787
788 // Check if UBWC buffer has been rendered in linear format.
789 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
790 format = metadata->linearFormat;
791 }
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400792
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700793 // Check metadata if the geometry has been updated.
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700794 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700795 int usage = 0;
796
797 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
798 usage = GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
799 }
800
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700801 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(metadata->bufferDim.sliceWidth,
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700802 metadata->bufferDim.sliceHeight, format, usage, width, height);
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700803 }
804
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400805 // Get the chroma offsets from the handle width/height. We take advantage
806 // of the fact the width _is_ the stride
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700807 switch (format) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400808 //Semiplanar
809 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
810 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
811 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
812 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: //Same as YCbCr_420_SP_VENUS
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700813 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800814 break;
815
816 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700817 getYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400818 break;
819
Sushil Chauhan4686c972015-02-20 15:44:52 -0800820 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700821 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
822 COLOR_FMT_NV12_UBWC, ycbcr);
Sushil Chauhan4686c972015-02-20 15:44:52 -0800823 ycbcr->chroma_step = 2;
824 break;
825
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800826 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700827 getYuvUbwcSPPlaneInfo(hnd->base, width, height,
828 COLOR_FMT_NV12_BPP10_UBWC, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800829 ycbcr->chroma_step = 3;
830 break;
831
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400832 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
833 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
834 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
Raj Kamal8bb3b8f2015-03-24 16:22:17 +0530835 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400836 case HAL_PIXEL_FORMAT_NV21_ZSL:
Ajay Dudani4dc06492015-03-26 07:28:11 -0700837 case HAL_PIXEL_FORMAT_RAW16:
Mansoor Aftabe9912a62014-07-15 01:40:26 -0700838 case HAL_PIXEL_FORMAT_RAW10:
Ramkumar Radhakrishnan790357e2016-03-24 17:03:41 -0700839 getYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr);
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800840 std::swap(ycbcr->cb, ycbcr->cr);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400841 break;
842
843 //Planar
844 case HAL_PIXEL_FORMAT_YV12:
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700845 ystride = width;
846 cstride = ALIGN(width/2, 16);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400847 ycbcr->y = (void*)hnd->base;
Kaushik Kanetkar071aca62015-01-22 23:16:26 -0700848 ycbcr->cr = (void*)(hnd->base + ystride * height);
849 ycbcr->cb = (void*)(hnd->base + ystride * height +
850 cstride * height/2);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400851 ycbcr->ystride = ystride;
852 ycbcr->cstride = cstride;
853 ycbcr->chroma_step = 1;
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400854 break;
855 //Unsupported formats
856 case HAL_PIXEL_FORMAT_YCbCr_422_I:
857 case HAL_PIXEL_FORMAT_YCrCb_422_I:
858 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
859 default:
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700860 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400861 err = -EINVAL;
862 }
863 return err;
864
865}
866
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700867
868
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700869// Allocate buffer from width, height and format into a
870// private_handle_t. It is the responsibility of the caller
871// to free the buffer using the free_buffer function
872int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
873{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700874 alloc_data data;
875 int alignedw, alignedh;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700876 gralloc::IAllocController* sAlloc =
877 gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700878 data.base = 0;
879 data.fd = -1;
880 data.offset = 0;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700881 data.size = getBufferSizeAndDimensions(w, h, format, usage, alignedw,
882 alignedh);
883
Naseer Ahmed29a26812012-06-14 00:56:20 -0700884 data.align = getpagesize();
885 data.uncached = useUncached(usage);
886 int allocFlags = usage;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700887
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700888 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700889 if (0 != err) {
890 ALOGE("%s: allocate failed", __FUNCTION__);
891 return -ENOMEM;
892 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700893
radhakrishnad7131e62015-10-13 12:32:30 +0530894 if(isUBwcEnabled(format, usage)) {
895 data.allocType |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
896 }
897
Naseer Ahmed29a26812012-06-14 00:56:20 -0700898 private_handle_t* hnd = new private_handle_t(data.fd, data.size,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700899 data.allocType, 0, format,
900 alignedw, alignedh);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700901 hnd->base = (uint64_t) data.base;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700902 hnd->offset = data.offset;
903 hnd->gpuaddr = 0;
904 *pHnd = hnd;
905 return 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700906}
907
908void free_buffer(private_handle_t *hnd)
909{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700910 gralloc::IAllocController* sAlloc =
911 gralloc::IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700912 if (hnd && hnd->fd > 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700913 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700914 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd);
915 }
916 if(hnd)
917 delete hnd;
918
919}
Sushil Chauhan65e26302015-01-14 10:48:57 -0800920
921// UBWC helper functions
922static bool isUBwcFormat(int format)
923{
924 // Explicitly defined UBWC formats
925 switch(format)
926 {
927 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800928 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
Sushil Chauhan65e26302015-01-14 10:48:57 -0800929 return true;
930 default:
931 return false;
932 }
933}
934
935static bool isUBwcSupported(int format)
936{
Sushil Chauhan01361412016-04-25 16:36:18 -0700937 if (MDPCapabilityInfo::getInstance().isUBwcSupportedByMDP()) {
938 // Existing HAL formats with UBWC support
939 switch(format)
940 {
941 case HAL_PIXEL_FORMAT_BGR_565:
942 case HAL_PIXEL_FORMAT_RGBA_8888:
943 case HAL_PIXEL_FORMAT_RGBX_8888:
944 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
945 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
946 case HAL_PIXEL_FORMAT_RGBA_1010102:
947 case HAL_PIXEL_FORMAT_RGBX_1010102:
948 return true;
949 default:
950 break;
951 }
Sushil Chauhan65e26302015-01-14 10:48:57 -0800952 }
Sushil Chauhan01361412016-04-25 16:36:18 -0700953 return false;
Sushil Chauhan65e26302015-01-14 10:48:57 -0800954}
955
956bool isUBwcEnabled(int format, int usage)
957{
Sushil Chauhan81594f62015-01-26 16:00:51 -0800958 // Allow UBWC, if client is using an explicitly defined UBWC pixel format.
959 if (isUBwcFormat(format))
960 return true;
961
Manikanta Kanamarlapudicb44d972016-02-03 17:49:55 +0530962 if ((usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
963 gralloc::IAllocController::getInstance()->isDisableUBWCForEncoder()) {
964 return false;
965 }
966
Sushil Chauhan7807d192015-08-13 10:10:48 -0700967 // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP
968 // support the format. OR if a non-OpenGL client like Rotator, sets UBWC
969 // usage flag and MDP supports the format.
970 if ((usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) && isUBwcSupported(format)) {
971 bool enable = true;
972 // Query GPU for UBWC only if buffer is intended to be used by GPU.
973 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER)) {
974 enable = AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format);
975 }
Sushil Chauhan81594f62015-01-26 16:00:51 -0800976 // Allow UBWC, only if CPU usage flags are not set
Sushil Chauhan7807d192015-08-13 10:10:48 -0700977 if (enable && !(usage & (GRALLOC_USAGE_SW_READ_MASK |
978 GRALLOC_USAGE_SW_WRITE_MASK))) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800979 return true;
980 }
981 }
982 return false;
983}
984
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800985static void getYuvUBwcWidthHeight(int width, int height, int format,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800986 int& aligned_w, int& aligned_h)
987{
988 switch (format)
989 {
990 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
991 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
992 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
993 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width);
994 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height);
995 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -0800996 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
997 aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_BPP10_UBWC, width);
998 aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_BPP10_UBWC, height);
999 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001000 default:
1001 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1002 aligned_w = 0;
1003 aligned_h = 0;
1004 break;
1005 }
1006}
1007
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001008static void getRgbUBwcBlockSize(int bpp, int& block_width, int& block_height)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001009{
1010 block_width = 0;
1011 block_height = 0;
1012
1013 switch(bpp)
1014 {
1015 case 2:
1016 case 4:
1017 block_width = 16;
1018 block_height = 4;
1019 break;
1020 case 8:
1021 block_width = 8;
1022 block_height = 4;
1023 break;
1024 case 16:
1025 block_width = 4;
1026 block_height = 4;
1027 break;
1028 default:
1029 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1030 break;
1031 }
1032}
1033
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001034static unsigned int getRgbUBwcMetaBufferSize(int width, int height, int bpp)
Sushil Chauhan65e26302015-01-14 10:48:57 -08001035{
1036 unsigned int size = 0;
1037 int meta_width, meta_height;
1038 int block_width, block_height;
1039
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001040 getRgbUBwcBlockSize(bpp, block_width, block_height);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001041
1042 if (!block_width || !block_height) {
1043 ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp);
1044 return size;
1045 }
1046
1047 // Align meta buffer height to 16 blocks
1048 meta_height = ALIGN(((height + block_height - 1) / block_height), 16);
1049
1050 // Align meta buffer width to 64 blocks
1051 meta_width = ALIGN(((width + block_width - 1) / block_width), 64);
1052
1053 // Align meta buffer size to 4K
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001054 size = ALIGN((meta_width * meta_height), 4096);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001055 return size;
1056}
1057
1058static unsigned int getUBwcSize(int width, int height, int format,
1059 const int alignedw, const int alignedh) {
1060
1061 unsigned int size = 0;
1062 switch (format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001063 case HAL_PIXEL_FORMAT_BGR_565:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001064 size = alignedw * alignedh * 2;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001065 size += getRgbUBwcMetaBufferSize(width, height, 2);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001066 break;
1067 case HAL_PIXEL_FORMAT_RGBA_8888:
Sushil Chauhan6686c802015-04-15 11:30:39 -07001068 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001069 case HAL_PIXEL_FORMAT_RGBA_1010102:
1070 case HAL_PIXEL_FORMAT_RGBX_1010102:
Sushil Chauhan65e26302015-01-14 10:48:57 -08001071 size = alignedw * alignedh * 4;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001072 size += getRgbUBwcMetaBufferSize(width, height, 4);
Sushil Chauhan65e26302015-01-14 10:48:57 -08001073 break;
1074 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1075 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
1076 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
1077 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height);
1078 break;
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001079 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1080 size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height);
1081 break;
Sushil Chauhan65e26302015-01-14 10:48:57 -08001082 default:
1083 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format);
1084 break;
1085 }
1086 return size;
1087}
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001088
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001089int getRgbDataAddress(private_handle_t* hnd, void** rgb_data)
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001090{
1091 int err = 0;
1092
1093 // This api is for RGB* formats
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001094 if (!isUncompressedRgbFormat(hnd->format)) {
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001095 return -EINVAL;
1096 }
1097
1098 // linear buffer
1099 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) {
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001100 *rgb_data = (void*)hnd->base;
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001101 return err;
1102 }
1103
1104 unsigned int meta_size = 0;
1105 switch (hnd->format) {
Kaushik Kanetkar4cb337c2015-08-29 20:16:54 -06001106 case HAL_PIXEL_FORMAT_BGR_565:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001107 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 2);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001108 break;
1109 case HAL_PIXEL_FORMAT_RGBA_8888:
1110 case HAL_PIXEL_FORMAT_RGBX_8888:
Ramkumar Radhakrishnandb89d1f2016-03-07 20:15:52 -08001111 meta_size = getRgbUBwcMetaBufferSize(hnd->width, hnd->height, 4);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001112 break;
1113 default:
1114 ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format);
1115 err = -EINVAL;
1116 break;
1117 }
1118
Sushil Chauhanc85b65b2015-04-30 11:05:36 -07001119 *rgb_data = (void*)(hnd->base + meta_size);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -07001120 return err;
1121}